Escolar Documentos
Profissional Documentos
Cultura Documentos
ALGORITHMS &
COMPUTING I
Ivica NIKOLIC & Thomas PEYRIN
AY 2013/14 Semester 1
16/8/13
Lecture1: Basics
L1-2
Instructors
Ivica NIKOLIC
School of Physical & Mathematical Sciences
Division of Mathematical Sciences
Office: SPMS-MAS-04-01
Email: inikolic@ntu.edu.sg
Thomas PEYRIN
School of Physical & Mathematical Sciences
Division of Mathematical Sciences
Office: SPMS-MAS-05-14
Email: thomas.peyrin@ntu.edu.sg
Lecture1: Basics
16/8/13
L1-3
Course Overview
OLD
NEW
Semester
Name
AU
Experimental Mathematics
Semester Name
AU
16/8/13
Lecture1: Basics
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
16/8/13
L1-5
MATLAB
Learn how to do elementary computations to
Visualize data
Write simple programs
No need to be scared!
16/8/13
Lecture1: Basics
L1-6
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
16/8/13
L1-7
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
16/8/13
Lecture1: Basics
L2-8
Lecture1: Basics
16/8/13
L2-9
Menu bar
In the Command
Window, MATLAB
can be used
intuitively and
immediately
responds with a
result.
Prompt
Content of
Current
Folder
Workspace
Command
History
16/8/13
Lecture1: Basics
L2-10
16/8/13
Lecture1: Basics
L2-11
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
16/8/13
Lecture1: Basics
L2-12
Variables
Putting a colon (;) at the end of a statement suppresses
the output.
Example:
>> mynum = 6;
>>
16/8/13
Lecture1: Basics
L2-13
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.
16/8/13
Lecture1: Basics
L2-14
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
16/8/13
Lecture1: Basics
L2-15
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
16/8/13
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
L2-16
Lecture1: Basics
16/8/13
L2-17
Operators
Unary operators operate on a single value or operand.
Binary operators operate on two values or operands.
>> 2*10^4
>> 2e4
20000
16/8/13
Lecture1: Basics
L2-18
>> 4+5*3 = 19
>> (4+5)*3 = 27
Within a given precedence level, expressions are evaluated
16/8/13
19
Lecture1: Basics
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
16/8/13
20
Lecture1: Basics
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
16/8/13
21
Lecture1: Basics
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
16/8/13
22
Lecture1: Basics
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
16/8/13
Built-in functions
To call a function, the name of the function is given
Example:
function argument
>> abs(-4) = 4
Function call
return value
L2-23
16/8/13
Lecture1: Basics
Built-in functions
Rounding and remainder functions:
fix
floor
ceil
round
rem
sign
L2-24
16/8/13
Lecture1: Basics
L2-25
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?
16/8/13
Lecture1: Basics
L2-26
16/8/13
Lecture1: Basics
L3-27
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
16/8/13
Lecture1: Basics
L2-28
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
int8, int16, int32, int64
16/8/13
Lecture1: Basics
L2-29
16/8/13
ASCII Table
Lecture1: Basics
L2-30
16/8/13
Lecture1: Basics
L2-31
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)
16/8/13
Lecture1: Basics
L2-32
16/8/13
Lecture1: Basics
L2-33
Random numbers
Random numbers are
useful e.g. for testing
16/8/13
Lecture1: Basics
L2-34
Random numbers
rand can be used to generate
uniformly distributed
random
real numbers
in the range from 0 to 1
The seed is always the same
Change the seed once at the beginning of each session
using rng(shuffle)
Range:
0 to N:
Low to high:
>> rand*N
>> rand*(high-low)+low
16/8/13
Lecture1: Basics
Random numbers
randn can be used to generate
normally distributed
random
real numbers
L2-35
16/8/13
Lecture1: Basics
L2-36
16/8/13
Lecture1: Basics
L2-37
Lessons learned
Common Pitfalls:
Putting space in a variable name
expression = variablename
rather than
variablename = expression
Using a built-in function name as a variable name, and then trying
16/8/13
Lecture1: Basics
L2-38
rand function
16/8/13
Lecture1: Basics
L2-39