![Page 1: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/1.jpg)
Lecture 26: Reusable Methods: Enviable Sloth
![Page 2: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/2.jpg)
Creating Function M-files
User defined functions are stored as M-filesTo use them, they must be in the current directory
![Page 3: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/3.jpg)
User-defined functions must start with a function definition line
The line contains… The word function A variable that defines the function
output A function name A variable used for the input argument
function output = poly(x) xz
![Page 4: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/4.jpg)
A simple function
In the new version, the file name could be different from the function name.
![Page 5: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/5.jpg)
The function is available from the command window or from other M-file programs
![Page 6: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/6.jpg)
Comments
You should comment functions liberally, just as you would any computer codeThe comment lines immediately after the first line are returned when you query the help function
![Page 7: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/7.jpg)
Functions with Multiple Inputs
A user defined function with multiple inputs
![Page 8: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/8.jpg)
Functions with Multiple Outputs
This function return 3 output values
If you don’t ask for all three results, the program just returns the first value
![Page 9: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/9.jpg)
Local Variables
Variables defined in an M-file function, only have meaning inside that programif set x=1 in the command window, it is not equal to 1 in the functionIf set y=2 in a function, it is not equal to 2 in the workspace windowThe only way to communicate between functions and the workspace, is through the function input and output arguments
![Page 10: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/10.jpg)
User Defined Input
To this point we have “hard coded” the values of variables into our M-file programsThe input function allows us to prompt the user to enter a value
![Page 11: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/11.jpg)
The input function is used in an M-file program to prompt the user to enter a value
The prompt is displayed in the command window
![Page 12: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/12.jpg)
![Page 13: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/13.jpg)
Input accepts a variety of data
ScalarsMatrices enter inside square brackets
Character strings enter inside single quotes Or… specify string input with ‘s’
![Page 14: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/14.jpg)
Run this program twice – once with numeric input and once with character input
![Page 15: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/15.jpg)
Indicates that the input should be interpreted as a string
![Page 16: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/16.jpg)
Output Options
Enter the name of a variableUse the disp functionUse the fprintf function
![Page 17: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/17.jpg)
disp
The display (disp) function can be used to display the contents of a matrix without printing the matrix name
![Page 18: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/18.jpg)
The disp function can also be used to display a string
![Page 19: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/19.jpg)
You can combine disp functions to create meaningful output from an M-file program, but the result of each disp function is on a separate line.
![Page 20: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/20.jpg)
Use the num2str(x) function to change numeric information to a string
Since the disp function only takes one input, you must combine arrays to make more complicated output
disp(['The values in the x array are: ' num2str(x)])
![Page 21: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/21.jpg)
![Page 22: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/22.jpg)
StructuresSequenceSelectionRepetition
Sequence Selection Repetition (Loop)
![Page 23: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/23.jpg)
Selection and Repetition structures require comparisons to work
Relational operators make those comparisonsLogical operators allow us to combine the comparisons
![Page 24: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/24.jpg)
Relational Operators
< Less than<= Less than or equal to> Greater than>= Greater than or equal to== Equal to~= Not equal to
![Page 25: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/25.jpg)
Logical Operators
& and~ not| orxor exclusive or
![Page 26: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/26.jpg)
Selection Structures
findA family of if structures
![Page 27: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/27.jpg)
find
The find command searches a matrix and identifies which elements in that matrix meet a given criteria.
![Page 28: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/28.jpg)
For example…
The US Naval Academy requires applicants to be at least 66” tallConsider this list of applicant heights63”, 67”, 65”, 72”, 69”, 78”, 75”Which applicants meet the criteria?
![Page 29: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/29.jpg)
The find function returns the index number for elements that meet a criteria
![Page 30: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/30.jpg)
index numbers
element values
![Page 31: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/31.jpg)
find used with a 2D matrix
x =[1 2 3; 10 5 1; 12 3 2; 8 3 1] element = find(x > 9) [row, column] = find(x > 9)
Returns a single element number
Returns the row and column designation of an element
![Page 32: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/32.jpg)
Simple if
if comparison statementsend
For example….
if G<50 count = count +1; disp(G);end
![Page 33: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/33.jpg)
If statementsEasy to interpret for scalarsWhat does an if statement mean if the comparison includes a matrix? The comparison is only true if it is true
for every member of the array
G=[30,55,10]if G<50
count = count +1; disp(G);end
The code inside the if statement is not executed, because the comparison is not true!!
![Page 34: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/34.jpg)
The if/else structure
The simple if triggers the execution of a block of code if a condition is trueIf it is false that block of code is skipped, and the program continues without doing anythingWhat if instead you want to execute an alternate set of code if the condition is false?
![Page 35: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/35.jpg)
Use an if structure to calculate a natural log
Check to see if the input is positive If it is, calculate the natural log If it isn’t, send an error message to the
screen
![Page 36: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/36.jpg)
The if/else/elseif structureUse the elseif for multiple selection criteriaFor example Write a program to determine if an
applicant is eligible to drive
![Page 37: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/37.jpg)
Repetition Structures - Loops
Loops are used when you need to repeat a set of instructions multiple times MATLAB supports two types of loops for while
![Page 38: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/38.jpg)
When to use loops
In general loops are best used with scalarsMany of the problems you may want to attempt with loops can be better solved by vectorizing your code or with MATLAB’s logical functions such as find
![Page 39: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/39.jpg)
For Loops
for index = [matrix]commands to be executed
end
•The loop starts with a for statement, and ends with the word end.•The first line in the loop defines the number of times the loops will repeat, using an index number. The loop is executed once for each element of the index matrix identified in the first line •The index of a for loop must be a variable.
![Page 40: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/40.jpg)
Here’s a simple example
In this case k is the index – the loop is repeated once for each value of k
the index can be defined using any of the techniques we’ve learned
![Page 41: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/41.jpg)
Here’s a simple example
In this case k is the index – the loop is repeated once for each value of k
the index can be defined using any of the techniques we’ve learned
![Page 42: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/42.jpg)
While Loops
while criterion commands to be executedend
While loops are very similar to for loops. The big difference is the way MATLAB decides how many times to repeat the loop. While loops continue until some criterion is met.
![Page 43: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/43.jpg)
This loop creates the matrix a, one element at a time
![Page 44: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/44.jpg)
Improving the Efficiency of Loops
In general, using a for loop (or a while loop) is less efficient in MATLAB than using array operations.
![Page 45: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/45.jpg)
The amount of time it takes to run this code will depend on your computer
Here’s an example. This code creates a 40,000 element matrix of ones, then multiplies each element in the matrix by pi
These two lines of code start a timer to measure the elapsed time required to run the lines of MATLAB code between them
![Page 46: Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the](https://reader035.vdocuments.us/reader035/viewer/2022070413/5697bfa61a28abf838c9858c/html5/thumbnails/46.jpg)
This code accomplishes the same thing with a for loop