Download presentation
Presentation is loading. Please wait.
Published byWhitney Hill Modified over 9 years ago
1
User-Defined Functions in MATLAB
2
What is a function? From the conceptual standpoint, a “function” is a blackbox that transforms “input arguments” to “output arguments” So far, we have been using Matlab’s “built-in” functions such as sqrt( ) or max( ) or even plot( ). We “invoke” the function with appropriate values for its arguments and the function “returns” to us the result(s)
3
Function Invocation For the sqrt function, we invoke it with a single numerical argument and it returns a single numerical result: >> sqrt(14) %the invocation ans = 3.7417 %the value returned >> x=99; y=sqrt(x) %the invocation y = 9.9499 %the value returned >> z=sqrt(x+1) %the invocation z = 10 %the value returned For the max function, we invoke it with an array argument and it can return one or two results: >> test=[3,7,4,-5]; max(test) %the invocation ans = 7 %the result >> [val,pos]=max(test) %the invocation, requesting two outputs val = 7 %the first output value pos = 2 %the second output value
4
User Defined Functions But what do we do if there is not a “built-in” function to accomplish a particular task? Define our own! myfun input output
5
Function Definition Header function [output vars] = function_name(input vars) “function_name” should begin with a “small” letter if the function is “Matlab” defined (quad). “Function_name” should begin with a “capital” letter if the function is “User” defined (Square).
6
Input Parameters The input parameters (or variables) for a function are variables which are to be furnished by the user when the function is invoked. They are NOT values which the function itself obtains thru “load” or “input”. They are also called “dummy” arguments, because they have no value until the function is invoked— they simply serve as “placeholders” in the function definition
7
Allowable variations for Function Headers function [area_square] = Square(side) function area_square = Square(side) function [volume_box] = … Box(height, width, length) function [area_circle, circumference]= Circle(radius) function Sqplot(side)
8
File name The function definition is placed in an.m-file with the same name as the function, e.g., Square.m Box.m Circle.m Sqplot.m
9
Rectarea.m function [area_rect] = Rectarea(width, height) %RECTAREA Computes the area of a rectangle. % Input Arguments are width and height. area_rect = width*height; %output returned %note the semicolon—as a general rule, a function should not produce anything in the command window when invoked—that is the job of the invoking script
10
Calling Rectarea() >> Rectarea(3,5) ans = 15 >> Rectarea(3,1:4) ans = 3 6 9 12 >>
11
Calling Rectarea() (cont’d) >> Rectarea([1;2;3],3) ans = 3 6 9 >> Rectarea([1,2],[1,2]) ??? Error using ==> * Inner matrix dimensions must agree.
12
Rectarea2.m This version does array (element by element) multiplication to allow vectors of both width and height: function [area_rect] = Rectarea2(width, height) %RECTAREA2 Computes the area of a rectangle. % Arguments are width and height. area_rect = width.*height;
13
Calling Rectarea2() >> Rectarea2(3,5) ans = 15 >> Rectarea2([1,2],[1,2]) ans = 1 4
14
Comment lines Comment lines following the function line and up to first blank or executable line are what the help function returns. >> help Rectarea RECTAREA Computes the area of a rectangle. Arguments are width and height.
15
Comment lines (cont’d) The first comment line is what lookfor sees. >> lookfor Rectangle RECTAREA Computes the area of a rectangle. RECTINT Rectangle intersection area. RECTANGLE Create rectangle, rounded- rectangle, or ellipse. DRAGRECT Drag XOR rectangles with mouse.
16
Circle.m function [area, circumf] = Circle(radius) %CIRCLE Computes area & circumference of a circle, given the radius. area = pi*radius.^2; circumf = 2*pi*radius; %the body of a function definition must % always assign values to all of the % output arguments
17
Calling Circle.m >> [a, c] = Circle(1) a = 3.1416 c = 6.2832 >> Circle(1) %returns the 1 st output ans = 3.1416
18
Ncount.m You can return an array as well as scalars: function [array] = Ncount(n) %NCOUNT Returns array of 1 to n. array = [1:n];
19
Calling Ncount.m >> x = Ncount(5) x = 1 2 3 4 5 >> x(3) ans = 3
20
Local variables The input variables & output variables in the function definition are “local” to the function and are NOT the same variables as similarly named variables in the calling program. The similarly named variables in the calling program are not changed by a call to the function. This allows you to call functions whose local variable names are unknown to you, without fear of unexpected changes in your program’s variables
21
Local variables (cont’d) In the Rectarea() function, area_rect, width, and height are local variables: function [area_rect] = Rectarea(width, height); %RECTAREA Computes the area of a rectangle. % Arguments are width and height. area_rect = width*height;
22
Local variables (cont’d) Variables similarly named in the calling program are not changed by a call to Rectarea() : >> area_rect = 0; >> width = 0; >> Rectarea(2,3) ans = 6 >> area_rect area_rect = 0 >> width width = 0
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.