850 likes | 859 Views
Learn how to create and use user-defined functions in MATLAB, including single and multiple inputs and outputs, storing functions in a toolbox, creating anonymous functions, and using function handles.
E N D
Chapter 6 User defined functions
In this chapter we will ... • Create and use MATLAB functions with both single and multiple inputs and outputs • Learn how to store and access functions in a user defined toolbox • Create anonymous functions • Create and use function handles • Create and use subfunctions and nested subfunctions
Section 6.1Creating Function M-files • User defined functions are stored as separate M-files • To use them, they must be in the current directory
Defining a function • Create an “M-file” • Use File New from the Matlab menu • Design your function • Save the file contents -- File name must be the same as the function name -- File extension is “.m” -- By default, Matlab automatically suggests the correct name for saving.
Why? • Elevates the understanding by hiding the details • Facilitates the top-down design • Software management
Elevates understanding • Suppose we are interested in finding the roots of a quadratic equation ax2 + bx +c = 0. We know that the roots r1 and r2 are given by r1 = (-b + sqrt(b^2 - 4*a*c))/(2*a); r2 = (-b – sqrt(b^2 - 4*a*c))/(2*a);
Elevates understanding • It is easier to understand the quadratic equation code once the burden of computing the square root is tackled separately. : r1 = (-b + sqrt(b^2 - 4*a*c))/(2*a); r2 = (-b – sqrt(b^2 - 4*a*c))/(2*a); :
Facilitates top down design • Consider the problem of designing
Facilitates top down design • We focus on how to draw the figure using two functions DrawRect and DrawStar. We just need to provide specifications to the functions. • Figure out how to implement DrawRect and DrawStar.
To specify a function You describe how to use it: function DrawRect(a,b,L,W,c) % Adds rectangle to the current window. % Assume hold is on. (a,b) is the center of the rectangle, and % L is the length and W is the breadth. Vertices of the % rectangle are: % (a-L/2,b-W/2), (a+L/2,b-W/2), (a+L/2,b+W/2), (a-L/2,b+W/2). % The fill color c is one of ‘r’, ‘g’, ‘y’, ‘b’, ‘w’, ‘k’, ‘c’, or ‘m’.
To specify a function • You write the code that the function works. x=[a-L/2 a+L/2 a+L/2 a-L/2]; y=[b-W/2 b-W/2 b+W/2 b+W/2]; fill(x,y,c); % %fill(x,y,c) fills the 2-d polygon defined by the %vectors x and yith the color specified by c. The %vertices of the polygon are specified by pairs of %components of x and y.
Complete description function DrawRect(a,b,L,W,c) % Adds rectangle to the current window. % Assume hold is on. (a,b) is the center of the rectangle, and % L is the length and W is the breadth. Vertices of the % rectangle are: % (a-L/2,b-W/2), (a+L/2,b-W/2), (a+L/2,b+W/2), (a-L/2,b+W/2). % The fill color c is one of ‘r’, ‘g’, ‘y’, ‘b’, ‘w’, ‘k’, ‘c’, or ‘m’. x=[a-L/2 a+L/2 a+L/2 a-L/2]; y=[b-W/2 b-W/2 b+W/2 b+W/2]; fill(x,y,c);
Software management Suppose today: One writes a function EPerimeter(a,b) which computes the perimeter of the ellipse
Software management During the next 10 years: • One writes softwares that makes extensive use of EPerimeter(a,b). • Imagine hundreds of programs with several lines of code use EPerimeter as a function. After 10 years, a new method is discovered to estimate the perimeter of an ellipse. The function EPerimeter(a,b) is updated. • No change in the main software is needed.
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)
The function is available from the command window or from other M-file programs
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
Example 1 Recall that the surface area and volume of a cylinder of radius r and height h is given by • Surface_area= 2*pi*radius*height+ 2*pi*radius^2 • Volume=pi*radius^2*h
A user-defined function function [surface_area, volume]=cylinder(radius,height) % Function to compute the surface_area and volume of % of a cylinder. radius and height could be vectors. % usage: [surface_area, volume]=cylinder(radius,height) base=pi*radius.^2; volume=base.*height; surface_area=2*pi*radius.*height+2*base;
A function begins with a header % Function to compute the surface_area and volume of % of a cylinder. radius and height could be vectors. % usage: [surface_area, volume]=cylinder(radius,height) base=pi*radius.^2; volume=base.*height; surface_area=2*pi*radius.*height+2*base; function [surface_area, volume]=cylinder(radius,height)
A function has a name % Function to compute the surface_area and volume of % of a cylinder. radius and height could be vectors. % usage: [surface_area, volume]=cylinder(radius,height) base=pi*radius.^2; volume=base.*height; surface_area=2*pi*radius.*height+2*base; function [surface_area, volume]=cylinder(radius,height)
Input arguments/parameters % Function to compute the surface_area and volume of % of a cylinder. radius and height could be vectors. % usage: [surface_area, volume]=cylinder(radius,height) base=pi*radius.^2; volume=base.*height; surface_area=2*pi*radius.*height+2*base; function [surface_area, volume]=cylinder(radius,height)
Output arguments/parameters % Function to compute the surface_area and volume of % of a cylinder. radius and height could be vectors. % usage: [surface_area, volume]=cylinder(radius,height) base=pi*radius.^2; volume=base.*height; surface_area=2*pi*radius.*height+2*base; function [surface_area, volume]=cylinder(radius,height)
Practical matters The code sits in a separate file. cylinder.m .
Practical matters • The .m file has the same name as the function • Thus in cylinder.m you will find an implementation of function cylinder. • The first non-comment in the file must be the function statement: [surface_area,volume]=cylinder(radius,height)
Most of the following slides are taken from www.cs.cornell.edu/courses/cs100m/2007fa
Practical matters We assume that scripts and other functions are stored in the current directory: Current Directory Script1.m cylinder.m Script2.m otherF.m Script3.m
Understanding Function Calls • There is a substitution mechanism • Local variables are used to carry out the computations
Let us execute the script line-by-line and see what happens during the call to function f.
x, y, z serve as local variables during the process. X is referred to as an input parameter.
Local Variables • Variables defined in an M-file function, only have meaning inside that program • if the user sets x=1 in the command window, it is not equal to 1 in the function • If the user sets 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
Repeat to Stress the distinction between local variables and variables in the calling program