Functions and Models 1. New Functions from Old Functions 1.3.
Functions...
-
Upload
darrel-pitts -
Category
Documents
-
view
17 -
download
1
description
Transcript of Functions...
1
Functions...
1. Refresh on old ones2. Refresh on the vocabulary3. Some new ones!
2
1. Remember these functions?
clcclear
sin(), sind() …sqrt(), abs() …
input(), fprintf(), disp()
3
2. Official vocabulary
variable = functions_name( argument list )
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.
4
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( argument list )
• For example…clc and clear require neithersind() requires 1 argument, and usually has a variable collecting the resultfprintf() requires at least 1 argument (the format string), but typically we do not collect the result.
5
Arguments? Collecting return values?
• 1 or many arguments:– Some keywords 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);
6
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 __?__
+
-
7
Examples
How many bags of concrete are needed to build stairs?
depthw
thick
Step1:- 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 0.6 sq.ft over a 2inch thickness (so 2/12*.6 ft^3)
Step2:
Civil Eng.
8
Examples
How many bags of concrete are needed to build stairs?
Step3:Assuming 4 stairs: 3ft wide, 7in tall, 11in deep
totVolume(ft3) = Nb_stairs * width * depth * thick = 4 * 3* 7/12 * 11/12 = 6.42 ft^3
Number of bags = totVolume(ft3)/ volume1bag = 6.42/ (2/12*.6) = 64.166 bags
Civil Eng.
9
Example2 Hrs/Min/Sec
Convert 5632seconds 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
10
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: go code this mini-example. Allow the user to enter the initial number of seconds.
11
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)
12
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
13
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
14
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
15
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!
16
Example1 Thermometer
A new thermometer is being built. In addition to showing the temperature, the manufacturer also wants to indicate a warning if the temperature is above or equal to 104 degrees Fahrenheit.
You are being paid ($$$) to develop the program that will eventually be within the thermometer.
It’s been a year, and still no thermometer.. How long are you going to wait???
17
Example2 Rockets 0/1’s?
How about rockets??? How does software get written? Do we waste a rocket each time?
During launch, so many sensors give back information!!!
A couple of them are….-Doors locked? True/False-Oxygen flowing properly? True/False-Fuel being delivered to engine? True/False
18
Wrapping up
• 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!!