Presentation is loading. Please wait.

Presentation is loading. Please wait.

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.

Similar presentations


Presentation on theme: "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."— Presentation transcript:

1 Lecture 26: Reusable Methods: Enviable Sloth

2 Creating Function M-files User defined functions are stored as M- files To use them, they must be in the current directory

3 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

4 A simple function In the new version, the file name could be different from the function name.

5 The function is available from the command window or from other M-file programs

6 Comments You should comment functions liberally, just as you would any computer code The comment lines immediately after the first line are returned when you query the help function

7 Functions with Multiple Inputs A user defined function with multiple inputs

8 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

9 Local Variables Variables defined in an M-file function, only have meaning inside that program if set x=1 in the command window, it is not equal to 1 in the function If set y=2 in a function, it is not equal to 2 in the workspace window The only way to communicate between functions and the workspace, is through the function input and output arguments

10 User Defined Input To this point we have “hard coded” the values of variables into our M-file programs The input function allows us to prompt the user to enter a value

11 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

12

13 Input accepts a variety of data Scalars Matrices enter inside square brackets Character strings enter inside single quotes Or… specify string input with ‘s’

14 Run this program twice – once with numeric input and once with character input

15 Indicates that the input should be interpreted as a string

16 Output Options Enter the name of a variable Use the disp function Use the fprintf function

17 disp The display (disp) function can be used to display the contents of a matrix without printing the matrix name

18 The disp function can also be used to display a string

19 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.

20 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)])

21

22 Structures Sequence Selection Repetition SequenceSelectionRepetition (Loop)

23 Selection and Repetition structures require comparisons to work Relational operators make those comparisons Logical operators allow us to combine the comparisons

24 Relational Operators < Less than <= Less than or equal to > Greater than >= Greater than or equal to == Equal to ~= Not equal to

25 Logical Operators & and ~ not | or xor exclusive or

26 Selection Structures find A family of if structures

27 find The find command searches a matrix and identifies which elements in that matrix meet a given criteria.

28 For example… The US Naval Academy requires applicants to be at least 66” tall Consider this list of applicant heights 63”, 67”, 65”, 72”, 69”, 78”, 75” Which applicants meet the criteria?

29 The find function returns the index number for elements that meet a criteria

30 index numbers element values

31 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

32 Simple if if comparison statements end For example…. if G<50 count = count +1; disp(G); end

33 If statements Easy to interpret for scalars What 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!!

34 The if/else structure The simple if triggers the execution of a block of code if a condition is true If it is false that block of code is skipped, and the program continues without doing anything What if instead you want to execute an alternate set of code if the condition is false?

35 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

36 The if/else/elseif structure Use the elseif for multiple selection criteria For example Write a program to determine if an applicant is eligible to drive

37 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

38 When to use loops In general loops are best used with scalars Many 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

39 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.

40 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

41 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

42 While Loops while criterion commands to be executed end 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.

43 This loop creates the matrix a, one element at a time

44 Improving the Efficiency of Loops In general, using a for loop (or a while loop) is less efficient in MATLAB than using array operations.

45 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

46 This code accomplishes the same thing with a for loop


Download ppt "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."

Similar presentations


Ads by Google