1 / 85

User-Defined Functions in MATLAB

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.

sabo
Download Presentation

User-Defined Functions in MATLAB

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 6 User defined functions

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

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

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

  5. Why? • Elevates the understanding by hiding the details • Facilitates the top-down design • Software management

  6. 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);

  7. 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); :

  8. Facilitates top down design • Consider the problem of designing

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

  10. 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’.

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

  12. 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);

  13. Software management Suppose today: One writes a function EPerimeter(a,b) which computes the perimeter of the ellipse

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

  15. 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)

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

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

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

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

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

  21. 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)

  22. 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)

  23. 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)

  24. Practical matters The code sits in a separate file. cylinder.m .

  25. 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)

  26. Most of the following slides are taken from www.cs.cornell.edu/courses/cs100m/2007fa

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

  28. Understanding Function Calls • There is a substitution mechanism • Local variables are used to carry out the computations

  29. Let us execute the script line-by-line and see what happens during the call to function f.

  30. x, y, z serve as local variables during the process. X is referred to as an input parameter.

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

  32. Repeat to Stress the distinction between local variables and variables in the calling program

More Related