engr-25 lec-07 sp12 functions-3 handles types
TRANSCRIPT
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
1/48
[email protected] ENGR-25_Functions-3.ppt1Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Bruce Mayer, PELicensed Electrical & Mechanical Engineer
Engr/Math/Physics 25
Chp3 MATLAB
Functions: Part3
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
2/48
[email protected] ENGR-25_Functions-3.ppt2Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Learning Goals
Understand the difference Built-In andUser-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
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
3/48
[email protected] ENGR-25_Functions-3.ppt3Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
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 InPutArguments/Parameters and
Return OutPut Values
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
4/48
[email protected] ENGR-25_Functions-3.ppt4Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Function Handle (MATLAB help)
MATLAB functions are written toNAMED files or are produced at the
command line as ANONYMOUS
functions. In either case, a function handle is
used to pass the function as an
input argument to a functionfunction.
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
5/48
[email protected] ENGR-25_Functions-3.ppt5
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Function Handles
You can create a function handle to anyfunction 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 handleto the sine function, you type
>> sine_handle = @sin;
where sine_handle is a user-selectedname for the handle (or NickName).
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
6/48
[email protected] ENGR-25_Functions-3.ppt6
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Function Handles cont
A common use of a function handle is to passthe function as an argument to another
function. For example, plot sinx over 0 x 6
as follows:
>> plot([0:0.01:6], sine_handle([0:0.01:6]))
The Result
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
7/48
[email protected] ENGR-25_Functions-3.ppt7
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Function Handles cont
This is a rather cumbersome way to plot thesine function, but the concept can be
extended to create, say, a general purpose
plotting function that accepts a built-in
function as an input. For example,function p = gen_plot(fcn_handle, interval)
plot(interval, fcn_handle(interval))
Create a handle to
the natural log (ln)
function
Plot over 199
>> ln_handle = @log;>>gen_plot(ln_handle,1:99)
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
8/48
[email protected] ENGR-25_Functions-3.ppt8
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Function Handles cont
Can Pass a Function with @ sign handle
>> gen_plot(@sech,0:.02:10)
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
9/48
[email protected] ENGR-25_Functions-3.ppt9
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Calling Functions
There are four ways to invoke, or call, afunction into action. These are:
1. As a character string identifying the
appropriate function .m-file
2. As a function handle3. As an inline function object
4. As a string expression.
Examples of these ways follow for thefzeroBuilt-in fcn which acts on the user-defined
function parab, which computes y = x2 4
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
10/48
[email protected] ENGR-25_Functions-3.ppt10
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
parab.mGraphed
-5 -4 -3 -2 -1 0 1 2 3 4 5-5
0
5
10
15
20
25
x
parab(x)
xplt = linspace(-5,5);
yplt = parab(xplt)plot(xplt,yplt)gridxlabel('x')ylabel('parab(x)')
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
11/48
[email protected] ENGR-25_Functions-3.ppt11
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Calling Functions cont
1. As a character stringidentifying the
appropriate function
.m-file, which is
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
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
12/48
[email protected] ENGR-25_Functions-3.ppt12
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Calling Functions cont
2. As a function handle to an existingfunction .m-file:
>> [z, val0] = fzero(@parab,[0, 3])
3. As an inline function object:>> parab1 = 'x.^2-4';>> parab_inline = inline(parab1);>> [w, val_0] = fzero(parab_inline,[0, 3])
w =2
val_0 =0
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
13/48
[email protected] ENGR-25_Functions-3.ppt13
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Calling Functions cont
4. As a string expression:>> parab2 = 'x.^2-4';>> [u, zero_u] = fzero(parab2,[0, 3])
Or as>> [u, zero_u] = fzero('x.^2-4',[0, 3])u =
2
zero_u =
0
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
14/48
[email protected] ENGR-25_Functions-3.ppt14
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Calling Functions cont
The function handle method (method 2) isthe fastest method, followed by method 1.
In addition to speed improvement, another
advantage of using a function handle is that it
provides access to subfunctions, which arenormally not visible outside of their defining
.m-file.
If we give a SubFunction a handle, or nickname,
then we can reach into the function file to
engage the useful SubFcn withOUT using the
main function
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
15/48
[email protected] ENGR-25_Functions-3.ppt15
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Types of User Defined Fcns
The PRIMARY function is the first function ina .m-file and typically contains the main
program. Following the primary function in
the same file can be any number of
subfunctions, which can serve assubroutines that support the primary function.
That is, .m-files can contain code for more
than one function. Additional functions within
the file are called subfunctions, and these
are only visible to the primary function or to
other subfunctions in the same file.
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
16/48
[email protected] ENGR-25_Functions-3.ppt16
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
PRIMARY Function
Usually the primary function is the ONLYfunction 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 thename 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 toinvoke the function.
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
17/48
[email protected] ENGR-25_Functions-3.ppt17
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Types of User Defined Fcns
ANONYMOUS functions enable creation of asimple function withOUT needing to write a
separate a .m-file for it.
You can construct an anonymous function either
at the MATLAB command line or from withinanother function or script.
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
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
18/48
[email protected] ENGR-25_Functions-3.ppt18
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Types of User Defined Fcns
SUBFUNCTIONS are placed in the primary
function and are called by the primary fcn You can use multiple functions within a
single primary function m-file
NESTED functions are functions defined
within another function. They can help to improve the readability of your
program and also give you more flexible access tovariables in the .m-file. Produce Nesting Levels
The difference between nested functionsand subfunctions is that subfunctionsnormally cannot be accessed outside oftheir primary function file
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
19/48
[email protected] ENGR-25_Functions-3.ppt19
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Types of User Defined Fcns
OVERLOADED functions are functions thatrespond differently to different types of input
arguments. They are similar to overloaded
functions in any object-oriented language.
For example, an overloaded function can becreated to treat integer inputs differently than
inputs of class double (precision).
PRIVATE functions enable you to restrict
access to a function. They can be called onlyfrom a .m-file function in the parent directory
Search PATHs will NOT work.
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
20/48
[email protected] ENGR-25_Functions-3.ppt20
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Function Function
The term function function is not aseparate function type but refers to any
function that accepts another function
as an input argument, such as thefunction fzero orfminbnd
You can pass a function to another
function using a function handle.
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
21/48
[email protected] ENGR-25_Functions-3.ppt21
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Anonymous Functions
The syntax for creating an anonymousfunction 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 Termexpr
is any single, valid MATLAB
expression.
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
22/48
[email protected] ENGR-25_Functions-3.ppt22
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Anonymous Fcn Unit Vector
Consider a Directionin 3D space defined
by LoA running from
Pt-A to Pt-B with
CoOrds as shown Define DIRECTION
Vector AB
kzz
jyyixxAB
AB
AB
AB
Or using Notation
zzjyixAB ABABAB
BBB zyx ,,
AAA zyx ,,
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
23/48
[email protected] ENGR-25_Functions-3.ppt23
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Anonymous Fcn Unit Vector
Next Calculate theGeometric Length,
AB, of The Direction
Vector AB using
Pythagorus
Normalizing AB toits Length will
produce a vector of
unit Length; i.e., u
222 ABABAB
AB
zyx
LABAB
Now AB has the
same Direction as u,
but a different length
BBB zyx ,,
AAA zyx ,,
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
24/48
[email protected] ENGR-25_Functions-3.ppt24
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Anonymous Fcn Unit Vector
Normalize AB toProduce
222
ABABAB
AB
zyx
kzjyix
AB
AB
L
ABu
thes for this case
mz
mymx
AB
ABAB
30
8040
BBB zyx ,,
AAA zyx ,,
Create MATLABAnonymous Fcn:uV(x, y, z)
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
25/48
[email protected] ENGR-25_Functions-3.ppt25
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Anonymous Fcn Unit Vector
Write Anon Fcn uVcompletely in theCOMMAND Window
Test using
conditions Given atRight
BBB zyx ,,
AAA zyx ,,
Also find Space Anglesby acosd(uAB)
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
26/48
[email protected] ENGR-25_Functions-3.ppt26
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
TWO versions of uV anon fcn>> uV1 = @(delX, delY, delZ) [delX delY delZ]/norm([delX delY delZ])uV1 =
@(delX,delY,delZ)[delX,delY,delZ]/norm([delX,delY,delZ])
>> uVa = uV1(13,-29,17)uVa =
0.3607 -0.8046 0.4717
>> 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
>> SpcQ = acosd(uVa)SpcQ =
68.8572 143.5740 61.8568
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
27/48
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
28/48
[email protected] ENGR-25_Functions-3.ppt28
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
@Cos_ln (x) Graphed: 0-2.5
0 1 2 3 4 5 6 7 8-10
-8
-6
-4
-2
0
2
4
6
u
v
>> 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
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
29/48
[email protected] ENGR-25_Functions-3.ppt29
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
ANONYMOUS Function example
Build, completely in the Command Window,a function that returns the
nth Root of a Scalar Base
nBz 1>> 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 1.4678
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
30/48
[email protected] ENGR-25_Functions-3.ppt30
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Anonymous Functions cont
You can pass the handle of an anonymousfunction to other functions.
For example, to find the minimum of the polynomial
4x2 50x + 5 over the interval [10, 10]
>>poly1 = @(x) 4*x.^2 - 50*x + 5;>>fminbnd(poly1, -10, 10)ans =
6.2500
Omit the Handle for a One-Time fcn usefminbnd(@(x) 4*x.^2 - 50*x + 5, -10, 10)
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
31/48
[email protected] ENGR-25_Functions-3.ppt31
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Calling One Fcn within Another
One anonymous function can call another toimplement function composition.
Consider the function h(x) = 5sin(x3). It is
composed of the fcns g(y) = 5sin(y)
and y = f(z) = z3 h(x) = g(f(x)) In the following session
the function whose
handle is h calls the
functions whosehandles are f and g.
>>f = @(x) x.^3;>>g = @(x) 5*sin(x);>>h = @(x) g(f(x));
>>h(2)ans =
4.9468
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
32/48
[email protected] ENGR-25_Functions-3.ppt32
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Variables and Anonymous Fcns
Variables can appear in anonymousfunctions in two ways:
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 variablesA and B in plane = @(x,y) A*x + B*y
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
33/48
[email protected] ENGR-25_Functions-3.ppt33
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Vars & Anonymous-Fcns cont
When the function is created MATLABcaptures the values of these variables and
retains those values for the lifetime of the
function handle.
If the values of A or Bare changed after the
handle is created,
their values associated
with the handle doNOT change.
>> A = 3; B = 7;
>> plane = @(x,y)A*x + B*y>> P1 = plane (1,2)P1 =
17>> A = 9; B = 14;>> P2 = plane (1,2)P2 =
17
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
34/48
[email protected] ENGR-25_Functions-3.ppt34
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
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 them-file name.
All other functions in the file are calledsubfunctions.
Subfunctions are normally visible only tothe 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
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
35/48
[email protected] ENGR-25_Functions-3.ppt35
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
SubFunctions cont
Create the primary function first with afunction definition line and its defining code,
and name the .m-file with this function
name as usual.
Next, create within the .m-file eachsubfunction with its own function definition
line and defining code
The orderof the subfunctions does not matter,
but function names must be unique within
the primary function .m-file.
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
36/48
[email protected] ENGR-25_Functions-3.ppt36
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
SubFunction Examplefunction [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) % Subfunction% Calculate average.a = sum(v)/n;
function m = median(v, n) % Subfunction% Calculate median.w = sort(v);if rem(n, 2) == 1m = w((n+1) / 2);
elsem = (w(n/2) + w(n/2+1)) / 2;
end
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
37/48
[email protected] ENGR-25_Functions-3.ppt37
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
SubFunction Example Results
>> w = [12:0.37:209];>> z = log(w).*(cos(w)).^2;>> [zavg, zmed] = newstatsSF(z)
zavg =
2.2590
zmed =
2.1168
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
38/48
[email protected] ENGR-25_Functions-3.ppt38
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Function-Call Precedence
The order in which MATLAB checks forfunctions 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 PRIVATEsubdirectory of the calling function
4. Then MATLAB checks for normal
.m-files on your search path
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
39/48
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
40/48
[email protected] ENGR-25_Functions-3.ppt40
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
SubFunction Example
This example shows how the MATLABm-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. The function residual_x is the primary function.
function r = residual_x(a)r = a - mean(a);%
function w = mean(z)w = sqrt(sum(z.^2))/length(z);
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
41/48
[email protected] ENGR-25_Functions-3.ppt41
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
SubFunction Example cont
Note thatmean
is a std MATLAB .m-File
Suggest checking mean in MATLAB Help
Use RMS residual fcn on [4,-4]
>> v = [ 4 -4];
>> r1 = residual_x(v)r1 =
1.1716 -6.8284
The ReDefined mean
does NOT changethe standard
MATLAB version
>> r2 = v - mean(v)r2 =
4 -4
Note:
RMS mean = 2.8284
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
42/48
[email protected] ENGR-25_Functions-3.ppt42
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
SubFunction Assessment
The use ofsubfunctions enables you toreduce 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 meanfunction 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 inthe same file
However, we can use a function handle to allow
access to the subfunction from outside the m-file
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
43/48
[email protected] ENGR-25_Functions-3.ppt43
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
All Done for Today
Some RootMean Square
Calculations
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
44/48
[email protected] ENGR-25_Functions-3.ppt44
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Bruce Mayer, PELicensed Electrical & Mechanical Engineer
Engr/Math/Physics 25
Appendix 6972 23 xxxxf
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
45/48
[email protected] ENGR-25_Functions-3.ppt45
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
@Cos_ln (x) Graphed
0 0.5 1 1.5 2 2.5 3-10
-8
-6
-4
-2
0
2
4
6
8
x
y
=
cosln
(x)
cos_ln = @(x) 7*cos(x) - log(x+1)xplt = linspace(0,3);yplt = cos_ln(xplt);
plot(xplt,yplt)gridxlabel('x')ylabel('y = cos_ln(x)')
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
46/48
[email protected] ENGR-25_Functions-3.ppt46
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Anonymous Fcn Unit Vector>> uV1 = @(delX, delY, delZ) [delX delY
delZ]/(sqrt(sum([delX delY delZ].*[delX delY
delZ])));
>> uVtest1 = uV1(7, -9, 4)
uVtest1 =
0.5793 -0.7448 0.3310
>> uV2 = @(delX, delY, delZ) [delX delY
delZ]/norm([delX delY delZ]);>> uVtest2 = uV2(7, -9, 4)
uVtest2 =
0.5793 -0.7448 0.3310
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
47/48
[email protected] ENGR-25_Functions-3.ppt47
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Anonymous Fcn Unit Vector thes for this case
Use uv2 to find
mz
mymx
AB
ABAB
30
8040
uT =
-0.4240 0.8480 0.3180
>> SpcAng = acosd(uT)
SpcAng =
115.0873 32.0054 71.4580
-
7/30/2019 ENGR-25 Lec-07 Sp12 Functions-3 Handles Types
48/48
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Anonymous Fcns: Multi-Inputs
You can create anonymous functions having
more than one input. For
example, define the function
>> pythag = @(x,y) sqrt(x.^2 + y.^2);
>> c = pythag(13,17)c =
21.4009
>> pythag([3,7],[4,11])ans =
5.0000 13.0384
22 yxpythag