You are on page 1of 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

o

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) .

ntu.ntu.aspx  IT experts will be in the computer labs on first lab sessions in order to assist you with the setup .sg  User guide for set up is available in NTU learn  Use VPN connection if you want to access from outside NTU: http://www.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.edu.sg/cits/itnetworking/remoteaccess/Page s/quickstartguide.edu.spms.

Expressions. Constants  Types  Random Numbers  Lessons Learned .17/08/15 Lecture1: Basics L1-7 MH 1401 Algorithms & Computing I Outline  MATLAB Desktop Environment  Variables.

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 .Lecture1: Basics 17/08/15 L1-8 MATLAB Desktop Environment !!!Help Browser!!! Menu bar Change Current Folder here In the Command Window.

Expressions.17/08/15 Lecture1: Basics L1-9 MH 1401 Algorithms & Computing I Outline  MATLAB Desktop Environment  Variables. Constants  Types  Random Numbers  Lessons Learned .

• 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-10 Variables • To store a value in a MATLAB session. or in a program. • The workspace window shows variables that have been created. a variable is used.

17/08/15 Lecture1: Basics L1-11 Variables • Putting a colon (.) at the end of a statement suppresses the output. Example: >> mynum = 6. Example: >> 6+3 ans = 9 . >> • MATLAB uses a default variable named ans if an expression is typed at the prompt and it is not assigned to a variable.

. • Adding to a variable is called incrementing. which will go back to the previously typed commands.17/08/15 Lecture1: Basics L1-12 Variables • A shortcut for retyping commands is to hit the up arrow. • Subtracting from a variable is called decrementing. • Very useful for debugging! • Some common programming terms: • Putting the first or initial value in a variable is called initializing the variable.

17/08/15 Lecture1: Basics L1-13 Variables The name of a variable: • Must start with a letter • After that it can contain letters. value_1) • No space is allowed! • namelengthmax tells you what is the maximum length of the name (63 on apple. MyNum and MYNUM are three different variables • Reserved words or key words cannot be used as a variable name .sg) • Is case-sensitive.edu. digits and the underscore character (e.g.spms. that is mynum.ntu.

• If nothing happens if you enter whos it means there aren’t 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 L1-14 Variables • whos show variables that have been defined in this Command Window.

4) ans = 1.9709 L1-15 .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.

5\115=23) • ^ exponentiation (e.g.g. 23^2=529) • Scientific or exponential notation >> 2*10^4 >> 2e4 20000 . e. • Binary operators operate on two values or operands.Lecture1: Basics 17/08/15 L1-16 Operators • Unary operators operate on a single value or operand.negation. • Common operators for numerical expressions: • + addition • . 115/5=23) • \ division (divided into.g. subtraction • * multiplication • / division (divided by. e.

negation • *. expressions are evaluated from left to right.addition and subtraction • Note: if in doubt.\ all multiplication and division • +. >> 4+5*3 = 19 >> (4+5)*3 = 27 • Within a given precedence level.17/08/15 Lecture1: Basics L1-17 Operator precedence rule • Some operators have precedence over others. • Order (from the highest to the lowest): • () parentheses • ^ exponentiation • . . use parentheses ./.

17 64 3. 15 2. 64 .17/08/15 Lecture1: Basics L1-18 Clicker Question 1 • What is the result of >> 4^(2--1) 1. 4 0% 0% 17 0% 4 0% 15 4.

17/08/15 Lecture1: Basics L1-19 Clicker Question 1 • What is the result of >> 4^(2--1) 1. 17 64 3. 4 0% 0% 17 0% 4 0% 15 4. 64 . 15 2.

17/08/15 Lecture1: Basics L1-20 Clicker Question 2 • What is the result of >> 4^2--1 1. 17 64 3. 64 . 15 2. 4 0% 0% 17 0% 4 0% 15 4.

4 0% 0% 17 0% 4 0% 15 4.17/08/15 Lecture1: Basics L1-21 Clicker Question 2 • What is the result of >> 4^2--1 1. 64 . 17 64 3. 15 2.

• Most functions return a value. Example: function argument >> abs(-4) = 4 Function call return value L1-22 . the name of the function is given followed by the argument(s) that are passed to the function in parentheses.Lecture1: Basics 17/08/15 Built-in functions • To call a function.

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.7183 . • 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.

17/08/15 Lecture1: Basics L1-25 MH 1401 Algorithms & Computing I Outline  MATLAB Desktop Environment  Variables. Expressions. Constants  Types  Random Numbers  Lessons Learned .

11111110. • GigaByte = 1024*1024*1024 Byte • Read this text: http://kb.iu.html 32 GB 25 movies 7000 songs 125. 11111111 • KiloByte = 1024 Byte.…. 00000001.17/08/15 Lecture1: Basics L1-26 Digital information • Bit = 0 or 1 • Byte = 8 bits: 00000000.000 ebooks . 00000010.edu/data/ackw. • MegaByte = 1024*1024 Byte.

e. uint16. • A class is a combination of a type and the operations that can be performed on it.g. ‘cat’) • Both characters and strings are enclosed in single quotes ‘ (without them a letter would be interpreted as a variable) .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.g. 2. real (e. ‘w’) • Used to store strings (= sequence of characters. uint32.g. int16.17/08/15 Lecture1: Basics L1-27 Types • Every expression or variable has a type associated with it. int32. • float. int64 int8 • Unsigned integer (sign not encoded): uint8. uint64 • logical: either true or false uint8 data (0 to 255) • char • Used to store single characters (e.

>> double(‘b’) = 98. >> char(98) = b.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(‘c’) = 99 >> char(97) = a. … >> 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. indices in for loops etc.g.) Example: >> num = 6+3 >> numi = int32(num) .

Constants  Types  Random Numbers  Lessons Learned .17/08/15 Lecture1: Basics L1-31 MH 1401 Algorithms & Computing I Outline  MATLAB Desktop Environment  Variables. Expressions.

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 Random numbers • Random numbers are • useful e.g.

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 .

Constants  Types  Random Numbers  Lessons Learned .17/08/15 Lecture1: Basics L1-35 MH 1401 Algorithms & Computing I Outline  MATLAB Desktop Environment  Variables. Expressions.

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.g. to find the remainder of dividing 3 into 10 using rem(3. 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.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.3) • Forgetting to use parentheses to pass arguments to a function (e.10) instead of rem(10. set the seed for the rand function .3 instead of fix(2. fix 2.

etc.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. • Generate random numbers • Lab assignment 1 available in NTU learn • Please bring a thumb drive to store your data ! .