1 / 18

Functions...

Functions. Refresh on old ones Refresh on the vocabulary Some new ones!. 1. Remember these functions ?. clc clear sin(), sind () … sqrt (), abs() … input(), fprintf(), disp (). 2. Official vocabulary. variable = functions_name ( argument list ).

Download Presentation

Functions...

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Functions... Refresh on old ones Refresh on the vocabulary Some new ones!

  2. 1. Remember these functions? clc clear sin(), sind() … sqrt(), abs() … input(), fprintf(), disp()

  3. 2. Official vocabulary variable = functions_name( argument list ) 2. MATLAB is “passing” inputs to the function. 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.

  4. Various uses • While the function’s name is ALWAYSneeded, the call may/may not require either one of the other 2 parts. variable = functions_name( argument list ) • For example… clc and clear require neither sind() requires 1 argument, and usually has a variable collecting the result fprintf() 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 string age = input(‘Enter your age: ’); 2 arguments: both strings username = input(‘Username: ’, ‘s’); 3 arguments: 1 string and 2 variables fprintf(‘Hello %s! You are %d years old!\n’,… username, age); 

  6. Rounding functions • Rounding floats to integer *w.r.t = with respect to + -

  7. Examples Civil Eng. How many bags of concrete are needed to build stairs? • 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: thick depth w

  8. Examples Civil Eng. 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

  9. Example2 Hrs/Min/Sec • Convert 5632seconds to a format hrs:min:sec! • 5632 secd = 1.56444444 hours • 3600 (secd/hr) • Round down: 1 full hour • 5623 sec – 1* 3600 sec = 2023 seconds • 2023 secd = 33.71666 minutes • 60(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 functions x=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 matrix x=rand(2,5); %a 2-rows by 5-columns matrix 

  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. k 1 0 k 0 2 1 3

  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. k 0 1 k 0 5

  14. End of algebra • So.. Using a combination of arithmetic operators, how would you generate these values (both excluded): k1 = rand_______________________; k2 = rand_______________________; k1 k2 15 -5.5 20 5.5

  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.t0.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!!

More Related