Date post: | 29-Jan-2016 |
Category: |
Documents |
Upload: | whitney-hill |
View: | 222 times |
Download: | 0 times |
User-Defined Functionsin MATLAB
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)
Function Invocation
• For the sqrt function, we invoke it with a single numerical argument and it returns a single numerical result:>> sqrt(14) %the invocationans = 3.7417 %the value returned>> x=99; y=sqrt(x) %the invocationy = 9.9499 %the value returned >> z=sqrt(x+1) %the invocationz = 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 invocationans = 7 %the result>> [val,pos]=max(test) %the invocation, requesting two outputsval = 7 %the first output valuepos = 2 %the second output value
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
inputoutput
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).
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
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)
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
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
Calling Rectarea()
>> Rectarea(3,5)ans = 15
>> Rectarea(3,1:4)ans = 3 6 9 12>>
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.
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;
Calling Rectarea2()
>> Rectarea2(3,5)
ans =
15
>> Rectarea2([1,2],[1,2])
ans =
1 4
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.
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.
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
Calling Circle.m
>> [a, c] = Circle(1)a = 3.1416c = 6.2832
>> Circle(1) %returns the 1st outputans = 3.1416
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];
Calling Ncount.m
>> x = Ncount(5)
x =
1 2 3 4 5
>> x(3)
ans =
3
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
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;
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_rectarea_rect = 0>> widthwidth = 0