User Tools

Site Tools


gibson:teaching:fall-2014:math445:lecture7-diary

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
gibson:teaching:fall-2014:math445:lecture7-diary [2014/10/07 11:15]
gibson
gibson:teaching:fall-2014:math445:lecture7-diary [2014/10/07 11:44] (current)
gibson [A function with an if statement and fprintf]
Line 1: Line 1:
 ====== Math 445 lecture 7 diary: functions, if statements ====== ====== Math 445 lecture 7 diary: functions, if statements ======
  
 +=====Today'​s topics=====
 +  * functions
 +  * ''​if''​ statements
 +  * ''​fprintf'':​ the formatted printing function
 +
 +=====Today'​s vocabulary=====
 +<​code>​
 +function
 +if-end
 +fprintf
 +argument
 +return value
 +function definition
 +function declaration
 +function body
 +</​code>​
 +
 +=====Lecture synopsis=====
 +
 +====An exceedingly simple function====
 The simplest way to define you own function in Matlab is to write it in a text file whose filename matches the function name, with a '​.m.'​ suffix. For example, we can define a ''​faren2celsius''​ function by creating a text file ''​faren2celsius.m''​ with the following contents The simplest way to define you own function in Matlab is to write it in a text file whose filename matches the function name, with a '​.m.'​ suffix. For example, we can define a ''​faren2celsius''​ function by creating a text file ''​faren2celsius.m''​ with the following contents
  
Line 14: Line 34:
 </​code>​ </​code>​
  
-Note the following:​ +Note the following:  
-  ​* the **function definition** begins with the word ''​function''​ and ends with ''​end''​. +  ​-The //function definition// begins with the word ''​function''​ and ends with ''​end''​. ​  
-  ​* the **function declaration** in the first line specifies that the function ''​faren2celsius''​ will take one **argument** (or input) ''​tempF''​ and produce one **return value** (or output) ''​tempC''​.  +  ​-The //function declaration// in the first line specifies that the function ''​faren2celsius''​ will take one //argument// (or input variable) ''​tempF''​ and produce one //return value// (or output ​variable) ''​tempC''​.  
-  ​* the comment lines immediately following the function declaration are a **help string**, which Matlab will print in response to ''​help faren2celsius''​. +  ​-The comment lines immediately following the function declaration are a //help string//, which Matlab will print in response to ''​help faren2celsius''​. 
-  ​* the **body** of the function lies between the help string and the ''​end''​. Here it is a single line of code that calculates ''​tempC''​ from ''​tempF''​.+  ​-The //body// of the function lies between the help string and the ''​end''​. Here the function body is a single line of code that calculates ''​tempC''​ from ''​tempF''​
 + 
 +====A function with an if statement and fprintf==== 
 + 
 +That was an exceedingly simple function. Let's write the inverse function ''​celsius2faren''​ with a few more bells and whistles. Specifically,​ let's print a warning message if the input temperature is below absolute zero, -273.15 C.  
 + 
 +<code matlab>​ 
 +function tempF = celsius2faren(tempC) 
 +% convert temperature in Celsius to Farenheit 
 + 
 +  if tempC < -273.15 
 +    fprintf('​%d C is below -273.15, i.e. absolute zero', tempC); 
 +  end 
 + 
 +  tempF = 9/5*tempC + 32; 
 + 
 +end 
 +</​code>​ 
 + 
 +This code includes an ''​if''​ statement that checks for temperatures below absolute zero. If the condition of the ''​if''​ statement evaluates to true, then the body of the ''​if''​ statement will be executed. If it evaluates to false, the body will be skipped, and execution will resume immediately after the ''​end''​. Thus here the warning message will be printed only if ''​tempC''​ is less than -273.15.  
 + 
 + 
 +The warning message is printed using the //formatted print function// ''​fprintf''​. ''​fprintf''​ takes a variable number of arguments. The first argument is always the ''​format string''​. The format string is simply a message to be printed with a number of slots into which values of variables will be substituted. The slots are marked with percent signs ''​%'',​ and following the percents signs is a letter code that tells Matlab what type of variable to expect for the slot. Here ''​%d''​ tells Matlab to expect a decimal-valued variable (i.e. a floating-point number). After the format string, the remaining arguments to ''​fprintf''​ are the variables whose values you want substituted into those slots
  
gibson/teaching/fall-2014/math445/lecture7-diary.1412705750.txt.gz · Last modified: 2014/10/07 11:15 by gibson