35
Library Functions... 1. Old functions 2. Vocabulary 3. Rounding numbers 4. Generating random numbers 5. mod() 6. Properties of mod() 7. Ex1: even or odd? 8. Ex2: error when not a whole number 1

Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:

Embed Size (px)

Citation preview

Page 1: Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:

1

Library Functions...

1. Old functions2. Vocabulary3. Rounding numbers4. Generating random numbers5. mod()6. Properties of mod()7. Ex1: even or odd?8. Ex2: error when not a whole number

Page 2: Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:

2

1. Remember these functions?

clcclear

sin(), sind() …sqrt(), abs() …

input(), fprintf(), disp()

MATLAB’s Core System has ~2300 functionsThis doesn’t include any of the toolboxes

Page 3: Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:

3

But what is a function?

• A function is like a box with holes in it.

Input Output

The _________ function

Magic

sinsqrtfloorrandbazingawhy

Page 4: Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:

4

2. Official vocabulary

variable = functions_name( argument list );

• Example:

hypotenuse = sqrt(a^2+b^2);

1. This is a “function call”. MATLAB “calls upon the execution” of the code behind the keyword.

3. MATLAB “collects” the “return-value” inside this variable.

2. MATLAB is “passing” inputs to the function.

1. MATLAB “calls upon the execution” of sqrt()

2. MATLAB “passes” the result of a^2+b^2”

3. MATLAB “collects” the “return-value”

Page 5: Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:

5

Various uses

• While the function’s name is ALWAYS needed, the call may/may not require either one of the other 2 parts.

variable = functions_name( arguments);

• For example…clc and clear require neitherfprintf() requires at least 1 argument (the format string), but typically we do not collect the result.

Page 6: Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:

6

Arguments? Collecting return values?

• 1 or many arguments:– Some functions are versatile in how many arguments they need– When there is a list of arguments, separate each with a comma: ,

1 argument: a stringage = input(‘Enter your age: ’);

2 arguments: both stringsusername = input(‘Username: ’, ‘s’);

3 arguments: 1 string and 2 variablesfprintf(‘Hello %s! You are %d years old!\n’,…

username, age);

Page 7: Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:

7

Rounding functions

• Rounding floats to integer

*w.r.t = with respect to

Function Definitions Examples

2.453 12.56 -6.67

round() Rounds *w.r.t 0.5 __?__ 13 -7

ceil() Rounds towards +infinity 3 __?__ -6

floor() Rounds towards -infinity 2 12 __?__

+

-

NEW

Page 8: Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:

8

Examples

How many bags of concrete mix are needed to build stairs?

Step1:-Givens needed:

- Dimensions of one step- How many stairs- How much concrete does one

bag of concrete mix make?-Find:

- Number of bags needed

Civil Eng.

Page 9: Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:

9

Examples

depthwidth

height

Step3

Step4- Assume there is a support system underneath. Only the steps need to be built.- Assume units are inches for the thickness and depth, and feet for the width- Each 80lbs bag allows for a coverage of 2 sq.ft over a 4 inch height (so 2*4/12 ft^3)

Civil Eng.

Step2

Page 10: Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:

10

Examples

How many bags of concrete are needed to build stairs?

Step5:Assuming 6 stairs: 3ft wide, 6in tall, 11in deep

totVolume(ft3) = Nb_stairs * width * depth * thick = 6 * 3* 6/12 * 11/12 = 8.25 ft^3

Number of bags = totVolume(ft3)/ volume1bag = 8.25/0.66 = 12.38

There is a need for ______ bags.

Civil Eng.

Page 11: Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:

11

Try This

Convert 5632 seconds to a format hrs:min:sec!

5632 secd = 1.56444444 hours3600 (secd/hr)

•Round down: 1 full hour

5623 sec – 1* 3600 sec = 2023 seconds

2023 secd = 33.71666 minutes60(secd/min)

•Round down: 33 full minutes

Tonight!

Page 12: Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:

12

Example2 Hrs/Min/Sec

2023 – 33*60 = 43 seconds

Conclusion:

5632seconds is also: 01:33:43

The function used to round down is: ________

Best practice: code this mini-example tonight. Allow the user to enter the initial number of seconds.

Page 13: Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:

13

4. Generating Random Numbers

• Generating random numbers

• rand() is another one of those versatile functionsx=rand;x=rand(); %some keep the () to remind themselves it is a function-call vs. a variable name.

x=rand(1); %avoid, it’s overdoing it…x=rand(2); %a 2-rows by 2-columns matrixx=rand(2,5); %a 2-rows by 5-columns matrix

rand Generates one float between 0 and 1 both excluded.

rand(n) Generates a matrix with n^2 floats between 0 and 1 both excluded. (used in 2 weeks from now)

rand(n,m) Generates an n-row by m-column matrix with floats between 0 and 1 both excluded. (used in 2 weeks from now)

Page 14: Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:

14

rand() and a little bit of algebra: +-

• What happens to a number k between 0 and 1 if it is added to another number? For example:

What can we say about: 2+k ?

What can we say about: k-4 ?

>> The interval shifts left/right.

0 1

k

2 3

k

0 1

Page 15: Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:

15

rand() and a little bit of algebra

• What happens to a number k between 0 and 1 if it is multiplied by another number? For example:

What can we say about: 5*k ?

What can we say about: k/2 ?

>> The interval grows/shrinks.

0 1

k

0 5

k

Page 16: Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:

16

rand() and a little bit of algebra

• What is the range of values K lies within?

K = rand*6;

K = rand*45-6;

K = 2+rand*3.3;

K = -6.5+rand/2;

K = (rand*3)/2-2;

? ?

K

1) Plug 0 into the formula2) Plug 1 into the formula3) Remember that all numbers between those 2 values

could be generated, but NOT those 2 values

Page 17: Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:

17

End of algebra

• So.. Using a combination of arithmetic operators, how would you generate these values (both excluded):

k1 = rand_______________________;

k2 = rand_______________________;

15 20

k1

-5.5 5.5

k2

Page 18: Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:

18

Conclusion

• To generate 1 float in the interval : (a,b)

k = rand*(b-a)+a; This is not a formula worth remembering.. Just remember

algebra!

(a, b) means the numbers a through b EXCLUDING a and b[a, b] means the numbers a through b INCLUDING a and bSometimes, square brackets are used and the direction it points also

indicates inclusion or exclusion. Ex: ]a, b[ is the same as (a,b)

Page 19: Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:

19

What about generating whole numbers?

• If rand generates one float, how do we generate random numbers?– like dice values: 1-6? (included of course)

%roll the diedie = ____________;

Page 20: Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:

20

Why not round?

• What happens with we do this:

DiceValue = round(6*rand)

(0, 1) becomes (0, 6). Think of this as 0.0001 to 5.9999.Then the number is rounded...

0 6

( )

0.5 1.5 2.5 3.5 4.5 5.5

0 1 2 3 4 5 6

Page 21: Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:

21

Rounding functions

• Rounding floats to integer

*w.r.t = with respect to

floor( rand*6 + 1 )% (0-1) (0-6) (1-7) = [1.0001-6.9999] [1 – 6]

ceil( rand * 6 )% (0-1) (0-6) = [0.0001 – 5.9999] [1 – 6]

Function Definitions Examples

2.453 12.56 -6.67

round() Rounds *w.r.t 0.5 __?__ 13 -7

ceil() Rounds towards +infinity 3 __?__ -6

floor() Rounds towards -infinity 2 12 __?__

+

-

NEW

Page 22: Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:

1. Modulus

• The modulus-function calculates the remainder of a long division

>> doc mod

22

Page 23: Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:

1. Modulus

• The modulus-function calculates the remainder of a long division

>> doc mod

• For example:

23

>>result = 77/3result = 25.6667>>result = mod(77,3)result = 2>>

7 7

2 5

3 -6

1 7

-1 5

2

Page 24: Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:

1. Modulus

• The modulus-function calculates the remainder of a long division

>> doc mod

• For example:

24

>>result = 77/3result = 25.6667>>result = mod(77,3)result = 2>>

mod(..) is a function that REQUIRES TWO ARGUMENTS.(mod(77) is an invalid statement…)

Page 25: Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:

1. Modulus

• The modulus-function calculates the remainder of a long division

>> doc mod

• For example:

25

>>result = 77/3result = 25.6667>>result = mod(77,3)result = 2>>

7 7

2 5

3 -6

1 7

-1 5

2

How is this ever useful…?

Page 26: Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:

2. Properties of mod()

• If x is evenly divisible by y (i.e no left-overs), mod(x,y) will return 0

• “mod” any number with another one “N”, the return-value will be a whole number from 0 to N-1. For example:

26

Mod by 2mod(2,2) 0

mod(3,2) 1

mod(4,2) 0

mod(5,2) 1

mod(6,2) 0

mod(15,2) ?

Mod by 3mod(3,3) 0

mod(4,3) 1

mod(5,3) 2

mod(6,3) 0

mod(7,3) 1

mod(26,3) ?

Mod by 5mod(2,5) 0

mod(5,5) 0

mod(6,5) 1

mod(7,5) 2

mod(8,5) 3

mod(9,5) 4mod(10,5) ?

Page 27: Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:

2. Properties of mod()

• If x is evenly divisible by y (i.e no left-overs), mod(x,y) will return 0

• “mod” any number with another one “N”, the return-value will be a whole number from 0 to N-1. For example:

27

Mod by 2mod(2,2) 0

mod(3,2) 1

mod(4,2) 0

mod(5,2) 1

mod(6,2) 0

mod(15,2) ?

Mod by 3mod(3,3) 0

mod(4,3) 1

mod(5,3) 2

mod(6,3) 0

mod(7,3) 1

mod(26,3) ?

Mod by 5mod(2,5) 0

mod(5,5) 0

mod(6,5) 1

mod(7,5) 2

mod(8,5) 3

mod(9,5) 4mod(10,5) ?

Page 28: Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:

2. Properties of mod()

• If x is evenly divisible by y (i.e no left-overs), mod(x,y) will return 0

• “mod” any number with another one “N”, the return-value will be a whole number from 0 to N-1. For example:

28

Mod by 2mod(2,2) 0

mod(3,2) 1

mod(4,2) 0

mod(5,2) 1

mod(6,2) 0

mod(15,2) ?

Mod by 3mod(3,3) 0

mod(4,3) 1

mod(5,3) 2

mod(6,3) 0

mod(7,3) 1

mod(26,3) ?

Mod by 5mod(2,5) 2

mod(5,5) 0

mod(6,5) 1

mod(7,5) 2

mod(8,5) 3

mod(9,5) 4mod(10,5) ?

Page 29: Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:

Ex1. Even or Odd?

• Prompt the user for a whole number, then display whether that number is even or odd.

• Algorithm is rather straightforward!% prompt the user for whole number% mod the number by 2% if the result is 1

% Display ‘odd’

% if the result is 0% Display ‘even’

% if the result is something else% Display ‘ERROR’

29

Page 30: Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:

Ex2: Check for integers

• Remember “Who Should Start?”% prompt how many players totaltotalPlayers = input('How many players (WHOLE number only): ');

% generate the one who starts (0-max)startPlayer = ceil(rand*totalPlayers);

% continue with game…fprintf('Player #%d will start.\n', startPlayer);

• Since there are no error-check, the following can happen!

30Let’s add an error message when an float is entered!...

Page 31: Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:

Check for integers, algorithm

%prompt user for total players%if invalid (negative, zero, or not integer)

%error message%else

%generate 1st player%continue with game

31

Page 32: Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:

Check for integers, code

%prompt user for total playerstotalPlayers = input('How many players (WHOLE number only): ');

% if mod( totalPlayers, 1 ) isn’t 0, totalPlayers isn’t a whole number

32

Using mod() in your answer, what does it mean for a number to not-be-an-integer?

Page 33: Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:

33

Key Ideas

• Vocabulary– Function call– Arguments– Collecting– Return-values– Versatile

• New notions– Rounding up/down/ or w.r.t 0.5– Generating random numbers– Generating 1 random float value

• Manipulating it to desire random range wanted– Generating a zero/one to simulate false/true

• Examples– Cement for stairs: ceil()– Time formatting: floor()– Temperature: rand()– Rocket: all of the above!!

Page 34: Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:

Key Ideas

• mod() is a built-in function that calculates the remainder of a division

• >> doc mod <enter> to see help window

• Commonly used to check if a number is divisible by another.– In other word, mod can be used to check if a number is a

multiple of another.

• mod(.., 2) is used to check even/odd• mod(.., 1) is used to check whole/decimal number• mod(.., N) is used to check if a number is divisible by N

34

Page 35: Library Functions... 1.Old functions 2.Vocabulary 3.Rounding numbers 4.Generating random numbers 5.mod() 6.Properties of mod() 7.Ex1: even or odd? 8.Ex2:

35

Exam 1

• Review on Thursday• Exam on Friday in lab

• ~10 multiple choice, true false, short answer questions• Programming problem

– Open book, open note, open resource. Closed “other people”.