User Tools

Site Tools


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

This is an old revision of the document!


A PCRE internal error occured. This might be caused by a faulty plugin

====== Math 445 lecture 7 diary: functions, if statements ====== 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 <code matlab> function tempC = faren2celsius(tempF) % convert celsius temperature to farenheit % input argument tempC % output value tempF tempC = 5/9*(tempF-32); end </code> Note the following: * 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 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''.

gibson/teaching/fall-2014/math445/lecture7-diary.1412705739.txt.gz · Last modified: 2014/10/07 11:15 by gibson