22
User-Defined Functions in MATLAB

User-Defined Functions in MATLAB. What is a function? From the conceptual standpoint, a “function” is a blackbox that transforms “input arguments” to

Embed Size (px)

Citation preview

Page 1: User-Defined Functions in MATLAB. What is a function? From the conceptual standpoint, a “function” is a blackbox that transforms “input arguments” to

User-Defined Functionsin MATLAB

Page 2: User-Defined Functions in MATLAB. What is a function? From the conceptual standpoint, a “function” is a blackbox that transforms “input arguments” to

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)

Page 3: User-Defined Functions in MATLAB. What is a function? From the conceptual standpoint, a “function” is a blackbox that transforms “input arguments” to

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

Page 4: User-Defined Functions in MATLAB. What is a function? From the conceptual standpoint, a “function” is a blackbox that transforms “input arguments” to

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

Page 5: User-Defined Functions in MATLAB. What is a function? From the conceptual standpoint, a “function” is a blackbox that transforms “input arguments” to

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

Page 6: User-Defined Functions in MATLAB. What is a function? From the conceptual standpoint, a “function” is a blackbox that transforms “input arguments” to

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

Page 7: User-Defined Functions in MATLAB. What is a function? From the conceptual standpoint, a “function” is a blackbox that transforms “input arguments” to

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)

Page 8: User-Defined Functions in MATLAB. What is a function? From the conceptual standpoint, a “function” is a blackbox that transforms “input arguments” to

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

Page 9: User-Defined Functions in MATLAB. What is a function? From the conceptual standpoint, a “function” is a blackbox that transforms “input arguments” to

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

Page 10: User-Defined Functions in MATLAB. What is a function? From the conceptual standpoint, a “function” is a blackbox that transforms “input arguments” to

Calling Rectarea()

>> Rectarea(3,5)ans = 15

>> Rectarea(3,1:4)ans = 3 6 9 12>>

Page 11: User-Defined Functions in MATLAB. What is a function? From the conceptual standpoint, a “function” is a blackbox that transforms “input arguments” to

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.

Page 12: User-Defined Functions in MATLAB. What is a function? From the conceptual standpoint, a “function” is a blackbox that transforms “input arguments” to

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;

Page 13: User-Defined Functions in MATLAB. What is a function? From the conceptual standpoint, a “function” is a blackbox that transforms “input arguments” to

Calling Rectarea2()

>> Rectarea2(3,5)

ans =

15

>> Rectarea2([1,2],[1,2])

ans =

1 4

Page 14: User-Defined Functions in MATLAB. What is a function? From the conceptual standpoint, a “function” is a blackbox that transforms “input arguments” to

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.

Page 15: User-Defined Functions in MATLAB. What is a function? From the conceptual standpoint, a “function” is a blackbox that transforms “input arguments” to

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.

Page 16: User-Defined Functions in MATLAB. What is a function? From the conceptual standpoint, a “function” is a blackbox that transforms “input arguments” to

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

Page 17: User-Defined Functions in MATLAB. What is a function? From the conceptual standpoint, a “function” is a blackbox that transforms “input arguments” to

Calling Circle.m

>> [a, c] = Circle(1)a = 3.1416c = 6.2832

>> Circle(1) %returns the 1st outputans = 3.1416

Page 18: User-Defined Functions in MATLAB. What is a function? From the conceptual standpoint, a “function” is a blackbox that transforms “input arguments” to

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];

Page 19: User-Defined Functions in MATLAB. What is a function? From the conceptual standpoint, a “function” is a blackbox that transforms “input arguments” to

Calling Ncount.m

>> x = Ncount(5)

x =

1 2 3 4 5

>> x(3)

ans =

3

Page 20: User-Defined Functions in MATLAB. What is a function? From the conceptual standpoint, a “function” is a blackbox that transforms “input arguments” to

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

Page 21: User-Defined Functions in MATLAB. What is a function? From the conceptual standpoint, a “function” is a blackbox that transforms “input arguments” to

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;

Page 22: User-Defined Functions in MATLAB. What is a function? From the conceptual standpoint, a “function” is a blackbox that transforms “input arguments” to

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