Вы находитесь на странице: 1из 38

MH 1401

ALGORITHMS &
COMPUTING I
Thomas PEYRIN
AY 2015/16 Semester 1

17.08.2015 Lecture 1: Basics

17/08/15

Lecture1: Basics

L1-2

Instructor

Thomas PEYRIN
School of Physical & Mathematical Sciences
Division of Mathematical Sciences
Office: SPMS-MAS-05-14
Email: thomas.peyrin@ntu.edu.sg

17/08/15

Lecture1: Basics

L1-3

Your Background
Do you have any kind of programming experience? (e.g.
classes in high school, hobby, community center classes, etc.)

1. Yes
2. No

0%
N

Ye
s

0%

Lecture1: Basics

17/08/15

L1-4

Goals of This Course


Develop basic computing/programming skills using

MATLAB
Learn how to do elementary computations to
Visualize data
Write simple programs

First ideas about algorithms and their analysis

No need to be scared!

Have fun achieving high marks!

17/08/15

Lecture1: Basics

L1-5

Why MATLAB?
MATLAB:
Is super easy to learn!

Is a mathematical and graphical software package


Has many built-in functions
Toolboxes can be added (e.g. for signal processing)

Lecture1: Basics

17/08/15

L1-6

2 ways to use MATLAB


Our Computer Labs
Software as a Service

Your Mac/Windows/Linux Laptop


Windows Remote Desktop to apple.spms.ntu.edu.sg
User guide for set up is available in NTU learn
Use VPN connection if you want to access from outside

NTU:
http://www.ntu.edu.sg/cits/itnetworking/remoteaccess/Page
s/quickstartguide.aspx
IT experts will be in the computer labs on first lab sessions

in order to assist you with the setup

17/08/15

Lecture1: Basics

L1-7

MH 1401 Algorithms & Computing I


Outline
MATLAB Desktop Environment
Variables, Expressions, Constants
Types

Random Numbers
Lessons Learned

Lecture1: Basics

17/08/15

L1-8

MATLAB Desktop Environment


!!!Help Browser!!!
Menu bar

Change Current Folder


here

In the Command
Window, MATLAB
can be used
intuitively and
immediately
responds with a
result.

Prompt

Content of
Current
Folder

Workspace

Command
History

FIGURE 1.1 MATLAB Command Window

17/08/15

Lecture1: Basics

L1-9

MH 1401 Algorithms & Computing I


Outline
MATLAB Desktop Environment
Variables, Expressions, Constants
Types

Random Numbers
Lessons Learned

17/08/15

Lecture1: Basics

L1-10

Variables
To store a value in a MATLAB session, or in a program, a

variable is used.
The workspace window shows variables that have been
created.
Assignment statement is used to create a variable:
variablename = expression
= does NOT mean equality!!!

Example:
>> mynum = 6
mynum =
6

17/08/15

Lecture1: Basics

L1-11

Variables
Putting a colon (;) at the end of a statement suppresses

the output.
Example:
>> mynum = 6;
>>

MATLAB uses a default variable named ans if an

expression is typed at the prompt and it is not assigned to


a variable.
Example:
>> 6+3
ans = 9

17/08/15

Lecture1: Basics

L1-12

Variables
A shortcut for retyping commands is to hit the up arrow,

which will go back to the previously typed commands.


Very useful for debugging!

Some common programming terms:


Putting the first or initial value in a variable is called initializing the

variable.
Adding to a variable is called incrementing.
Subtracting from a variable is called decrementing.

17/08/15

Lecture1: Basics

L1-13

Variables
The name of a variable:
Must start with a letter
After that it can contain letters, digits and the underscore

character (e.g. value_1)


No space is allowed!

namelengthmax tells you what is the maximum length of the

name (63 on apple.spms.ntu.edu.sg)


Is case-sensitive, that is mynum, MyNum and MYNUM are three

different variables
Reserved words or key words cannot be used as a variable

name

17/08/15

Lecture1: Basics

L1-14

Variables
whos show variables that have been defined in this

Command Window.
If nothing happens if you enter whos it means there arent

any variables.
clear clears out all variables so they no longer exist

clear variablename1 variablename2 clears out a

list of variables (separate the names with space)

17/08/15

Lecture1: Basics

Expressions
Expressions can be created using:
Values
Variables that have been already created
Operators
Built-in functions
Parentheses
Example:
>> 2*sin(1.4)
ans =
1.9709

L1-15

Lecture1: Basics

17/08/15

L1-16

Operators
Unary operators operate on a single value or operand.
Binary operators operate on two values or operands.
Common operators for numerical expressions:
+ addition
- negation, subtraction
* multiplication
/ division (divided by, e.g. 115/5=23)
\ division (divided into, e.g. 5\115=23)
^ exponentiation (e.g. 23^2=529)
Scientific or exponential notation

>> 2*10^4
>> 2e4

20000

17/08/15

Lecture1: Basics

L1-17

Operator precedence rule


Some operators have precedence over others.

>> 4+5*3 = 19
>> (4+5)*3 = 27
Within a given precedence level, expressions are evaluated

from left to right.


Order (from the highest to the lowest):
() parentheses
^ exponentiation
- negation
*,/,\ all multiplication and division
+, - addition and subtraction
Note: if in doubt, use parentheses

17/08/15

Lecture1: Basics

L1-18

Clicker Question 1
What is the result of

>> 4^(2--1)

1. 15
2. 4

0%

0%
17

0%
4

0%
15

4. 17

64

3. 64

17/08/15

Lecture1: Basics

L1-19

Clicker Question 1
What is the result of

>> 4^(2--1)

1. 15
2. 4

0%

0%
17

0%
4

0%
15

4. 17

64

3. 64

17/08/15

Lecture1: Basics

L1-20

Clicker Question 2
What is the result of

>> 4^2--1

1. 15
2. 4

0%

0%
17

0%
4

0%
15

4. 17

64

3. 64

17/08/15

Lecture1: Basics

L1-21

Clicker Question 2
What is the result of

>> 4^2--1

1. 15
2. 4

0%

0%
17

0%
4

0%
15

4. 17

64

3. 64

Lecture1: Basics

17/08/15

Built-in functions
To call a function, the name of the function is given

followed by the argument(s) that are passed to the


function in parentheses.
Most functions return a value.

Example:
function argument

>> abs(-4) = 4
Function call

return value

L1-22

17/08/15

Lecture1: Basics

Built-in functions
Rounding and remainder functions:
fix
floor
ceil
round
rem
sign

L1-23

17/08/15

Lecture1: Basics

L1-24

Constants
Variables are used to store values that might change.
Constants are used to store values that cannot possibly

change.
Examples of constants are:
pi
3.14159
-1
i or j
inf

NaN
0/0 (not a number)
How do you get e?

>> exp(1) = 2.7183

17/08/15

Lecture1: Basics

L1-25

MH 1401 Algorithms & Computing I


Outline
MATLAB Desktop Environment
Variables, Expressions, Constants
Types

Random Numbers
Lessons Learned

17/08/15

Lecture1: Basics

L1-26

Digital information
Bit = 0 or 1
Byte = 8 bits: 00000000, 00000001,

00000010,, 11111110, 11111111


KiloByte = 1024 Byte;
MegaByte = 1024*1024 Byte;

GigaByte = 1024*1024*1024 Byte


Read this text:

http://kb.iu.edu/data/ackw.html

32 GB
25 movies
7000 songs
125,000 ebooks

17/08/15

Lecture1: Basics

L1-27

Types
Every expression or variable has a type associated with it.
A class is a combination of a type and the operations that can be

performed on it.
float, real (e.g. 2.3)
double (for double precision, default) stores larger numbers than single

integer

data (0 to 127)

sign

Signed integer (the first bit encodes the sign):


int8, int16, int32, int64

int8

Unsigned integer (sign not encoded):


uint8, uint16, uint32, uint64

logical: either true or false

uint8
data (0 to 255)

char
Used to store single characters (e.g. w)
Used to store strings (= sequence of characters, e.g. cat)
Both characters and strings are enclosed in single quotes (without them a letter

would be interpreted as a variable)

17/08/15

Lecture1: Basics

L1-28

Characters and encoding


ASCII: American Standard Code for Information Interchange
is the most common character encoding
Specifies 128 characters with integer values 0 to 127 (see next slide)
>> double(a) = 97, >> double(b) = 98, >> double(c) = 99
>> char(97) = a, >> char(98) = b,
>> double(abcd) = 97 98 99 100

Math can be done on characters


>> char(abcd+1) = bcde

17/08/15

ASCII Table

Lecture1: Basics

L1-29

17/08/15

Lecture1: Basics

L1-30

Type casting
Type casting is the conversion of a value from one type to

another
The name of the casting function is the name of the target

type
Type casting is useful to save memory (e.g. indices in for

loops etc.)

Example:
>> num = 6+3
>> numi = int32(num)

17/08/15

Lecture1: Basics

L1-31

MH 1401 Algorithms & Computing I


Outline
MATLAB Desktop Environment
Variables, Expressions, Constants
Types

Random Numbers
Lessons Learned

17/08/15

Lecture1: Basics

Random numbers
Random numbers are
useful e.g. for testing
often required in cryptography

rand can be used to generate


uniformly distributed
random
real numbers
in the range from 0 to 1

L1-32

17/08/15

Lecture1: Basics

L1-33

Random numbers
The seed is always the same => the random numbers

sequence will be the same if you restart MATLAB


Solution: change the seed once at the beginning of each

session using rng(shuffle)


(old version: rand(shuffle,sum(100*clock)) )

Change the range of the random numbers:


0 to N:
>> rand*N
low to high:
>> rand*(high-low)+low

17/08/15

Lecture1: Basics

Random numbers
randn can be used to generate
normally distributed
random
real numbers

Random integers can be produced


using the round function:
>> round(rand*N)
using the built-in function: >> randi(N)

L1-34

17/08/15

Lecture1: Basics

L1-35

MH 1401 Algorithms & Computing I


Outline
MATLAB Desktop Environment
Variables, Expressions, Constants
Types

Random Numbers
Lessons Learned

17/08/15

Lecture1: Basics

L1-36

Lessons learned
Common Pitfalls:
Putting space in a variable name
Confusing the format of an assignment statement as

expression = variablename
rather than
variablename = expression
Using a built-in function name as a variable name, and then trying

to use the function


Confusing the two division operators / and \
Forgetting the operator precedence rules

17/08/15

Lecture1: Basics

L1-37

Lessons learned (ctd)


Common Pitfalls (ctd):
Confusing the order of arguments passed to functions for example,

to find the remainder of dividing 3 into 10 using rem(3,10) instead


of rem(10,3)

Forgetting to use parentheses to pass arguments to a function (e.g.,

fix 2.3 instead of fix(2.3))

Programming Style guidelines:


Use mnemonic variable names (names that make sense, such as

radius instead of xyz)


Although variable named result and RESULT are different, using the

same word(s) for different variables would be confusing


Do not use the names of built-in functions as variable names
If different sets of random numbers are desired, set the seed for the

rand function

17/08/15

Lecture1: Basics

L1-38

Topics of First Lab Session


How to use MATLAB IDE
Create simple MATLAB scripts using basic operations like

addition, multiplication, etc.


Generate random numbers

Lab assignment 1 available in NTU learn


Please bring a thumb drive to store your data !

Вам также может понравиться