user-defined functions in matlab. what is a function? from the conceptual standpoint, a...
TRANSCRIPT
![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](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bfe91a28abf838cb6e31/html5/thumbnails/1.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bfe91a28abf838cb6e31/html5/thumbnails/2.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bfe91a28abf838cb6e31/html5/thumbnails/3.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bfe91a28abf838cb6e31/html5/thumbnails/4.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bfe91a28abf838cb6e31/html5/thumbnails/5.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bfe91a28abf838cb6e31/html5/thumbnails/6.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bfe91a28abf838cb6e31/html5/thumbnails/7.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bfe91a28abf838cb6e31/html5/thumbnails/8.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bfe91a28abf838cb6e31/html5/thumbnails/9.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bfe91a28abf838cb6e31/html5/thumbnails/10.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bfe91a28abf838cb6e31/html5/thumbnails/11.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bfe91a28abf838cb6e31/html5/thumbnails/12.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bfe91a28abf838cb6e31/html5/thumbnails/13.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bfe91a28abf838cb6e31/html5/thumbnails/14.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bfe91a28abf838cb6e31/html5/thumbnails/15.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bfe91a28abf838cb6e31/html5/thumbnails/16.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bfe91a28abf838cb6e31/html5/thumbnails/17.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bfe91a28abf838cb6e31/html5/thumbnails/18.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bfe91a28abf838cb6e31/html5/thumbnails/19.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bfe91a28abf838cb6e31/html5/thumbnails/20.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bfe91a28abf838cb6e31/html5/thumbnails/21.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022062309/5697bfe91a28abf838cb6e31/html5/thumbnails/22.jpg)
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