Академический Документы
Профессиональный Документы
Культура Документы
ALGORITHMS &
COMPUTING I
Thomas PEYRIN
AY 2015/16 Semester 1
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
MATLAB
Learn how to do elementary computations to
Visualize data
Write simple programs
No need to be scared!
17/08/15
Lecture1: Basics
L1-5
Why MATLAB?
MATLAB:
Is super easy to learn!
Lecture1: Basics
17/08/15
L1-6
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
17/08/15
Lecture1: Basics
L1-7
Random Numbers
Lessons Learned
Lecture1: Basics
17/08/15
L1-8
In the Command
Window, MATLAB
can be used
intuitively and
immediately
responds with a
result.
Prompt
Content of
Current
Folder
Workspace
Command
History
17/08/15
Lecture1: Basics
L1-9
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;
>>
17/08/15
Lecture1: Basics
L1-12
Variables
A shortcut for retyping commands is to hit the up arrow,
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
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
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
>> 4+5*3 = 19
>> (4+5)*3 = 27
Within a given precedence level, expressions are evaluated
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
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?
17/08/15
Lecture1: Basics
L1-25
Random Numbers
Lessons Learned
17/08/15
Lecture1: Basics
L1-26
Digital information
Bit = 0 or 1
Byte = 8 bits: 00000000, 00000001,
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
int8
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
17/08/15
Lecture1: Basics
L1-28
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
Random Numbers
Lessons Learned
17/08/15
Lecture1: Basics
Random numbers
Random numbers are
useful e.g. for testing
often required in cryptography
L1-32
17/08/15
Lecture1: Basics
L1-33
Random numbers
The seed is always the same => the random numbers
17/08/15
Lecture1: Basics
Random numbers
randn can be used to generate
normally distributed
random
real numbers
L1-34
17/08/15
Lecture1: Basics
L1-35
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
17/08/15
Lecture1: Basics
L1-37
rand function
17/08/15
Lecture1: Basics
L1-38