Você está na página 1de 39

MH 1400

ALGORITHMS &
COMPUTING I
Ivica NIKOLIC & Thomas PEYRIN
AY 2013/14 Semester 1

16.08.2013 Lecture 1: Basics

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

Introduction to Scientific Computing 3

Experimental Mathematics

Semester Name

AU

Algorithm & Computing I

Algorithm & Computing II

Algorithm & Computing III

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

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!

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

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 edveNTUre
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

16/8/13

Lecture1: Basics

L2-8

MH 1400 Algorithms & Computing I


Outline
MATLAB Desktop Environment
Variables, Expressions, Constants
Types
Random Numbers
Lessons Learned

Lecture1: Basics

16/8/13

L2-9

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

16/8/13

Lecture1: Basics

L2-10

MH 1400 Algorithms & Computing I


Outline
MATLAB Desktop Environment
Variables, Expressions, Constants
Types
Random Numbers
Lessons Learned

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;
>>

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

16/8/13

Lecture1: Basics

L2-13

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.

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

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

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

list of variables (separate the names with space)

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.

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

16/8/13

Lecture1: Basics

L2-18

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

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

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

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?

>> exp(1) = 2.7183

16/8/13

Lecture1: Basics

L2-26

MH 1400 Algorithms & Computing I


Outline
MATLAB Desktop Environment
Variables, Expressions, Constants
Types
Random Numbers
Lessons Learned

16/8/13

Lecture1: Basics

L3-27

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 (also in edventure):

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

uint8, uint16, uint32, uint64

logical: either true or false


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.

16/8/13

Lecture1: Basics

L2-29

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

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

MH 1400 Algorithms & Computing I


Outline
MATLAB Desktop Environment
Variables, Expressions, Constants
Types
Random Numbers
Lessons Learned

16/8/13

Lecture1: Basics

L2-33

Random numbers
Random numbers are
useful e.g. for testing

often required in cryptography

Random Number Generators (RNG) or functions


are not truly random
use a seed (pre-determined or derived from built-in clock)
result is used as the next seed

are also called Pseudo Random Number Generators

(PRNG) because a process determines the outcome

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)

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

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

Random integers can be produced


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

L2-35

16/8/13

Lecture1: Basics

L2-36

MH 1400 Algorithms & Computing I


Outline
MATLAB Desktop Environment
Variables, Expressions, Constants
Types
Random Numbers
Lessons Learned

16/8/13

Lecture1: Basics

L2-37

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

16/8/13

Lecture1: Basics

L2-38

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

16/8/13

Lecture1: Basics

L2-39

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 manual available in edveNTUre after this lecture
Please bring a thumb drive to store your data!

Você também pode gostar