Presentation is loading. Please wait.

Presentation is loading. Please wait.

Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161.

Similar presentations


Presentation on theme: "Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161."— Presentation transcript:

1 Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161

2 Introduction to Chapter 6 In this chapter you will learn how to write user defined functions and use them in your programs. User defined functions behave just like built in functions. User defined functions help you write more compact programs and structure long complex programs into shorter ones that are easier to debug and get running. Completing Chapter 6 will enable you to write your own functions.

3 User Defined Functions We’ve already become familiar with a number of MATLAB built in functions like sin (x) or plot (x,y) for example. Now it’s time to learn how to write user defined functions and use them in your programs just as you would use MATLAB’s built in functions. Functions allow us to reuse computer code for calculations that are performed frequently as part of other programs. For example, suppose there were no built in sine function in MATLAB and you were doing a lot of trigonometric calculations involving the sine of an angle. You might want to write a chunk of MATLAB statements that you could use over and over again without having to reenter the statements each time. This is the value of being able to write and use “user defined functions”.

4 User Defined Functions II You write user defined functions using the MATLAB editor and save them in the folder you created along with all your MATLAB M-files. User defined functions require a special format, consider the simple example, function result = POLY(x); % This simple function returns the value of a 3 rd order % polynomial for the input value of x. (x may be a vector) result = 3*x.^3 + 5*x.^2 -2*x + 1; Let’s look at the elements that make up a user defined function. These lines of code define a function whose name is POLY. The first word in the first statement defining a function is the word function.

5 User Defined Functions III Continuing with the elements making up a user defined function Next, the output variable result is assigned to the function POLY(x) in the first statement of the user defined function. x is the input variable provided by the user when the function is used in a program. The output is assigned to the variable result at the completion of the function for use in the main program that called the function. Once the function is written and debugged it should be stored in your MATLAB files folder pointed to in the Current Directory with file_name POLY, the name of the function. Function names need to be legal MATLAB variable names.

6 User Defined Function IV Consider the following, >> A = [1, 2, 5];% A a 3 element row vector >> y = 2 * POLY(A) y = 14 82 982 In the statement y = 2* POLY(A) above, the user defined function POLY is called which evaluates the polynomial at each point in the input row vector. Then, each value is multiplied by 2 resulting in the row vector called y having the values [14, 82, 982].

7 User Defined Functions V User defined functions may Have more than one input variable, function s = f (x, y, z). This function has three input variables. Have more than on output variable, function [out1, out2] = f (x) is a function with one input variable and two output variables. Comments entered at the beginning of a function will be echoed back in the Command Window in response to the command, >>help f, to help you remember what your function does. Input and output variables can be scalars, vectors or matrices. Within the body of the function, there needs to be one assignment statement for each output variable. You can use your user defined functions from the Command Window interactively or from within a MATLAB program.

8 User Defined Functions VI Any variables used within a function are called local variables. These variables do not appear in the Workspace Window and are not available for the main program to access. The output variable names used in the function statement and in the body of the function are also local variables. The input variables are local variables also. Two built in functions let you determine the number of input and output variables in any function, for function s = f (x) >>nargin (‘f’) returns in our case, ans =1 since f has one input argument, and >>nargout(‘f’) also returns ans = 1 in our case because f has only one output variable.

9 Commenting user defined functions As with all computer programs, commenting your code is essential, comment liberally… Recall that for built in functions, using the help feature will give you some information about the function, help sin, for example. When you place comments after the first line of the function and before your code, the help feature will return those comments to you to help you remember how to use the function. help POLY(entered in the Command Window) This is a simple function returns the value of a 3 rd order polynomial for the input value of x. (x may be a vector)

10 Global Variables Remember variable names used within a function are local variables, they do not appear in the Workspace window of the main program. It is possible to define a Global Variable that is then common to both the main program and the function. It is recommended not to do this, read section 6.1.7 for more information.

11 Anonymous Functions In many cases user defined function are single functions to evaluate like our previous example ploy. One can employ an anonymous function in the main program. The anonymous function needs to be defined before it is used. For example, ploy = @(x) 3*x.^3 + 5*x.^2 – 2*x + 1 poly(2) would return the value of 41.

12 Other Topics As your confidence grows using MATLAB review the use of Function Handles and SubFunctions described at the end of this chapter.

13 Chapter 6 Assignments The following assignments let you write and use several simple functions. Assignments 6.2, 6.6, 6.8 (these are the problem number from the 3rd edition book).


Download ppt "Chapter 6 Review: User Defined Functions Introduction to MATLAB 7 Engineering 161."

Similar presentations


Ads by Google