Documente Academic
Documente Profesional
Documente Cultură
Chp3 MATLAB
Functions: Part3
Bruce Mayer, PE
Licensed Electrical & Mechanical Engineer
BMayer@ChabotCollege.edu
Engineering/Math/Physics 25: Computational Methods
1
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
Learning Goals
Understand the difference Built-In and User-Defined MATLAB Functions Write User Defined Functions Describe Global and Local Variables When to use SUBfunctions as opposed to NESTED-Functions Import Data from External Data-Files
As generated, for example, by an Electronic Data-Acquisition System
Engineering/Math/Physics 25: Computational Methods
2
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
Functions (ReDeaux)
MATLAB Has Two Types of Functions 1. Built-In Functions Provided by the Softeware
e.g.; max, min, median
2. User-Defined Functions are .m-files that can accept InPut Arguments/Parameters and Return OutPut Values
Engineering/Math/Physics 25: Computational Methods
3
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
In either case, a function handle is used to pass the function as an input argument to a function function.
Engineering/Math/Physics 25: Computational Methods
4
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
Function Handles
You can create a function handle to any function by using the at sign, @, before the function name. You can then name the handle if you wish, and use the handle to reference the function. For example, to create a handle to the sine function, you type
>> sine_handle = @sin;
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
The Result
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
Calling Functions
There are four ways to invoke, or call, a function into action. These are:
1. As a character string identifying the appropriate function .m-file 2. As a function handle
Examples of these ways follow for the fzero Built-in fcn which acts on the user-defined function parab, which computes y = x2 4
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
parab.m Graphed
25 20
15
parab(x)
10
-5 -5
10
-4
-3
-2
-1
0 x
4
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
The function may be called as follows, to compute the zero over the range: 0 x 3
>> [x, value] = fzero('parab', [0 3]) x = 2 value = 0
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
Or as
>> [u, zero_u] = fzero('x.^2-4',[0, 3]) u = 2 zero_u =
0
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
PRIMARY Function
Usually the primary function is the ONLY function in a .m-file that we can call from the MATLAB Command Window or from another .m-file function You invoke the Primary function using the name of the .m-file in which it is defined.
We normally use the same name for the function and its file, but if the function name differs from the file name, you must use the FILE name to invoke the function.
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
Thus, anonymous functions provide a quick way of making a function from any MATLAB expression withOUT the need to create, name, and save a file.
More on anonymous fcns in a few slides
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
The difference between nested functions and subfunctions is that subfunctions normally cannot be accessed outside of their primary function file
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
PRIVATE functions enable you to restrict access to a function. They can be called only from a .m-file function in the parent directory
Search PATHs will NOT work.
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
Function Function
The term function function is not a separate function type but refers to any function that accepts another function as an input argument, such as the function fzero or fminbnd You can pass a function to another function using a function handle.
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
Anonymous Functions
The syntax for creating an anonymous function from an expression is
fhandle = @(arglist) expr
Where
The Term arglist is a comma-separated list of input arguments to be passed to the function The Term expr is any single, valid MATLAB expression.
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
xB , y B , z B
x A , y A , z A
yB y A j z B z A k
Or using Notation
y AB xABi AB j z AB z
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
xB , y B , z B
AB AB LAB
2 2 2 x AB y AB z AB
x A , y A , z A
xB , y B , z B
AB AB u LAB AB y xi j zk
2 2 2 x AB y AB z AB
x A , y A , z A
the s for this case Create MATLAB x AB 40m y AB 80m Anonymous Fcn: uV(x, y, z) z AB 30m
Engineering/Math/Physics 25: Computational Methods
24
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
xB , y B , z B
x A , y A , z A
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
>> uV2 = @(dx, dy, dz) [dx dy dz]/sqrt(sum(dx^2 + dy^2 + dz^2)) uV2 = @(dx,dy,dz)[dx,dy,dz]/sqrt(sum(dx^2+dy^2+dz^2)) >> uVb = uV2(13,-29,17)
uVb = 0.3607
-0.8046
0.4717
61.8568
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
7 cos x ln x 1
Collect Terms on One Side, and use fzero to find x that satisfies eqn near x=2
7 cos x ln x 1 0
Engineering/Math/Physics 25: Computational Methods
27
1.4429
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
-2 -4 -6 -8 -10
>> u = linspace(0, 2.5*pi, 300); >> v = cos_ln(u); >> xZ = [0,8]; yZ = [0, 0]; >> plot(u,v, xZ,yZ, 'LineWidth',3), grid, xlabel('u'), ylabel('v'); >> Z1 = fzero(cos_ln,2) Z1 = 1.4429 >> Z2 = fzero(cos_ln,5) Z2 = 4.9705 >> Z3 = fzero(cos_ln,8) Z3 = 7.5425
4 u
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
zB
>> nth_root = @(B,n) B.^(1./n); >> z = nth_root(31,3) z = 3.1414 >> z1 = nth_root(-99,4.3) z1 = 2.1683 + 1.9428i >> nth_root(10, [2:6]) ans = 3.1623 2.1544 1.7783 1.5849
Engineering/Math/Physics 25: Computational Methods
29
1.4678
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
1. As variables specified in the argument list, as for example f = @(x) x.^3; 2. As variables PreDefined in the body of the expression, as for example with the variables A and B in plane = @(x,y) A*x + B*y
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
SubFunctions
A function .m-file may contain more than one user-defined function.
The first defined function in the file is called the primary function, whose name is the same as the m-file name. All other functions in the file are called subfunctions.
Subfunctions are normally visible only to the primary function and other subfunctions in the same file;
that is, they normally cannot be called by programs or functions outside the primary fcn file
this limitation can be removed with fcn handles
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
SubFunctions cont
Create the primary function first with a function definition line and its defining code, and name the .m-file with this function name as usual. Next, create within the .m-file each subfunction with its own function definition line and defining code
The order of the subfunctions does not matter, but function names must be unique within the primary function .m-file.
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
SubFunction Example
function [avg, med] = newstatsSF(u) % Primary function % Bruce Mayer, PE * 06Feb12 * ENGR25 % file = newstatsSF.m (SHOULD match fcn name) % NEWSTATS Find mean and median with internal functions. n = length(u); avg = mean(u, n); med = median(u, n);
function a = mean(v, n) % Calculate average. a = sum(v)/n; % Subfunction
function m = median(v, n) % Calculate median. w = sort(v); if rem(n, 2) == 1 m = w((n+1) / 2); else m = (w(n/2) + w(n/2+1)) / 2; end
Engineering/Math/Physics 25: Computational Methods
36
% Subfunction
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
zmed = 2.1168
Engineering/Math/Physics 25: Computational Methods
37
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
Function-Call Precedence
The order in which MATLAB checks for functions is very important. When a function is called from within an .m-file, MATLAB
1. first checks to see if the function is a built-in function such as sin.
2. Next it checks to see if it is a subfunction in a primary function .m-file 3. then checks to see if it is a private function
which is a function m-file residing in the PRIVATE subdirectory of the calling function
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
SubFunction Example
This example shows how the MATLAB m-function mean can be superceded by our own definition of the mean, one which gives the Root-Mean Square (RMS) value.
The function mean is a subfunction.
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
The ReDefined mean does NOT change the standard MATLAB version
>> r2 = v - mean(v) r2 = 4 -4
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
SubFunction Assessment
The use of subfunctions enables you to reduce the no. of files that define your fcns
For example, if it were not for the subfunction mean in the previous example, we would have had to define a separate .m-file for our mean function and give it a different name so as not to confuse it with the MATLAB fcn of the same name
Subfunctions are normally visible only to the primary function and other subfunctions in the same file
However, we can use a function handle to allow access to the subfunction from outside the m-file
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
Engr/Math/Physics 25
Appendix
Bruce Mayer, PE
Licensed Electrical & Mechanical Engineer
f x 2 x 7 x 9 x 6
3 2
Engineering/Math/Physics 25: Computational Methods
44
BMayer@ChabotCollege.edu
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
y = cos n(x)
0 -2
cos_ln = @(x) 7*cos(x) - log(x+1) xplt = linspace(0,3); yplt = cos_ln(xplt); -6 plot(xplt,yplt) grid -8 xlabel('x') ylabel('y = cos_ln(x)')
-4 -10 0 0.5 1 1.5 x 2 2.5
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
>> uV2 = @(delX, delY, delZ) [delX delY delZ]/norm([delX delY delZ]); >> uVtest2 = uV2(7, -9, 4) uVtest2 = 0.5793 -0.7448 0.3310
Engineering/Math/Physics 25: Computational Methods
46
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
z AB 30m
Use uv2 to find
uT = -0.4240 0.8480 0.3180
71.4580
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt
Bruce Mayer, PE
BMayer@ChabotCollege.edu ENGR-25_Functions-3.ppt