engr-25 lec-06 sp12 functions-2 builtin udf (1)
TRANSCRIPT
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
1/40
[email protected] ENGR-25_Functions-2.ppt1Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Bruce Mayer, PELicensed Electrical & Mechanical Engineer
Engr/Math/Physics 25
Chp3 MATLAB
Functions: Part2
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
2/40
[email protected] ENGR-25_Functions-2.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 an External Data-File
As generated, for example, by anElectronic Data-Acquisition System
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
3/40
[email protected] ENGR-25_Functions-2.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, median2. User-Defined
Functions are .m-files
that can accept InPutArguments/Parameters and
Return OutPut Values
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
4/40
[email protected] ENGR-25_Functions-2.ppt4Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Array Function Operations
MATLAB will treat a variable as anarray automatically.
For example, to compute the
square roots of 7, 11 and 3-5j, type
>> u = [7, 11, (3-5j)]
>> sqrt(u)
ans =2.6458 3.3166 2.1013 - 1.1897i
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
5/40
[email protected] ENGR-25_Functions-2.ppt5
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Expressing Fcn Arguments
We can write sin2 in text, but MATLABrequires parentheses surrounding the 2
The 2 is called the fcn argument orparameter
To evaluate sin2 in MATLAB, type sin(2)
The MATLAB function name must be followed by apair of parentheses that enclose the argument
To express in text the sine of the 2nd element
of the array x, we would type sin[x(2)]. However, in MATLAB you cannot use
square brackets or braces in this way,and you must type sin(x(2))
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
6/40
[email protected] ENGR-25_Functions-2.ppt6
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Expressing Fcn Arguments cont
Evaluate sin(x2
+ 5) type sin(x.^2 + 5) Evaluate sin(x+1) type sin(sqrt(x)+1)
Using a function as an argument of another
function is called function composition.
Check the order of precedence and the numberand placement of parentheses when typing such
expressions
Every left-facing parenthesis requiresa right-facing mate.
However, this condition does NOTguarantee that the expression is CORRECT!
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
7/40
[email protected] ENGR-25_Functions-2.ppt7
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Expressing Fcn Arguments cont
Another common mistake involvesexpressions like cos2y, which means (cosy)2.
In MATLAB write this expression as(cos(y))^2 or cos(y)^2
Do NOT write the Expression as
cos^2(y)
cos^2y
cos(y^2)
>> cos(pi/1.73)ans =
-0.2427
>> (cos(pi/1.73))^2ans =
0.0589
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
8/40
[email protected] ENGR-25_Functions-2.ppt8
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Built-In Trig Functions
MATLAB trig fcns operate in radian mode
Thus sin(5) returns the sineof 5 rads, NOT the sine of 5.
Command Conventional Math Functioncos(x) Cosine; cos xcot(x) Cotangent; cot xcsc(x) Cosecant; csc xsec(x) Secant; sec xsin(x) Sine; sin xtan(x) Tangent; tan x
rads
rads
180
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
9/40
[email protected] ENGR-25_Functions-2.ppt9
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Built-In Inverse-Trig Functions
MATLAB trig-1 fcns also operate in radian mode
i.e., They Return Angles in RADIANS
Command Conventional Math Functionacos(x) Inverse cosine; arccos x.acot(x) Inverse cotangent; arccot x.acsc(x) Inverse cosecant; arccsc xasec(x) Inverse secant; arcsec xasin(x) Inverse sine; arcsin xatan(x) Inverse tangent; arctan x
atan2(y,x) Four-quadrant inverse tangent TwoaTans?
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
10/40
[email protected] ENGR-25_Functions-2.ppt10
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Why Two aTans?
Consider theSituation at Right(-4,3)(-4,3)(-4,3)
(4,-3)
37
143
87.3675.0arctan
43arctan
87.3675.0arctan
43arctan
x
x
Due to aTan range
/2 y /2
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
11/40
[email protected] ENGR-25_Functions-2.ppt11
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Why Two aTans? cont
Calculator Confusion from
(-4,3)(-4,3)(-4,3)
(4,-3)
37
143
14343arctan43arctan37
MATLAB Solves This problemwith atan2 which allows input
of the CoOrds to ID the proper Quadant
MATLAB atan>> q1 = atan(-3/4)
q1 =-0.6435>> q2 = atan(3/-4)q2 =
-0.6435
MATLAB atan2>> q3 = atan2(-3,4)
q3 =-0.6435>> q4 = atan2(3,-4)q4 =
2.4981
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
12/40
[email protected] ENGR-25_Functions-2.ppt12
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
atan2 built into Complex angle
As noted in the
Discussion ofComplex numbers
converting between
the RECTANGULAR
& POLAR forms
require that we note
the Range-Limits of
the atan function MATLABs angle
function uses atan2
>> z1 = 4 - 3jz1 =
4.0000 - 3.0000i
>> z2 = -4 + 3jz2 =-4.0000 + 3.0000i
>> r1 = abs(z1)r1 =
5
>> r2 = abs(z2)
r2 =5
>> theta1 = atan2(imag(z1),real(z1))theta1 =
-0.6435
>> theta2 = atan2(imag(z2),real(z2))theta2 =
2.4981
>> z1a = r1 *exp(j *theta1)z1a =
4.0000 - 3.0000i
>> z2b = r2 *exp(j *theta2)z2b =
-4.0000 + 3.0000i
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
13/40
[email protected] ENGR-25_Functions-2.ppt13
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Degree Trig Functions
MATLAB now has Trig Functions thatOperate on angles in DEGREES ()
Example: Y = cosd()
Where is Measured in Degrees cosd()
sind()
tand()
cotd()
cscd()
secd()
acosd(y)
asind(y)
atand(y)
acotd(y)
acscd(y)
asecd(y)
NOatan2d(y)though
(wellmake one)
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
14/40
[email protected] ENGR-25_Functions-2.ppt14
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Built-In Hyperbolic Functions
Command
Conventional Math Function
cosh(x) Hyperbolic cosine.coth(x) Hyperbolic cotangentcsch(x) Hyperbolic cosecantsech(x) Hyperbolic secantsinh(x) Hyperbolic sinetanh(x) Hyperbolic tangent
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
15/40
[email protected] ENGR-25_Functions-2.ppt15
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Built-In Inverse-Hyp Functions
Command Conventional Math Functionacosh(x) Inverse Hyperbolic cosine.acoth(x) Inverse Hyperbolic cotangentacsch(x) Inverse Hyperbolic cosecantasech(x) Inverse Hyperbolic secantasinh(x) Inverse Hyperbolic sineatanh(x) Inverse Hyperbolic tangent
>> asinh(37)ans =
4.3042>> asinh(-37)ans =
-4.3042
>> acosh(37)ans =
4.3039>> acosh(-37)ans =
4.3039 + 3.1416i
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
16/40
[email protected] ENGR-25_Functions-2.ppt16
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
User-Defined Functions (UDFs)
The first line in a function file must begin witha FUNCTION DEFINITION LINE that has a list
ofinputs & outputs.
This line distinguishes a function m-file from a
script m-file. The 1st Line Syntax:function [output variables] = name(input variables)
Note that the OUTPUT variables are enclosed inSQUARE BRACKETS, while the input variables
must be enclosed with parentheses. The function name (here, name) should be the
same as the file name in which it is saved
(with the .m extension).
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
17/40
[email protected] ENGR-25_Functions-2.ppt17
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
UDF Example
The Active m-File Linesfunction ave3 = avg3(a, b, c)TriTot = a+b+c;ave3 = TriTot/3;
Note the use of asemicolon at the end
of the lines. This
prevents the values ofTriTot and ave3
from being displayed
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
18/40
[email protected] ENGR-25_Functions-2.ppt18
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
UDF Example cont
Call this Function with its OutPut Agrument>> TriAve = avg3(7,13,-3)TriAve =
5.6667
To DetermineTriAve the
Function takes
a = 7 b = 13
c = 3
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
19/40
[email protected] ENGR-25_Functions-2.ppt19
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
UDF Example cont
Call the avg3 function withOUT its outputargument and try to access its internal value,avg3. You will see an error message.
>> avg3(-23,19,29)
ans =8.3333
>> ave3??? Undefined function or variable 'ave3'.
The variable ave3 is LOCAL to the UDF
Its Value is NOT defined OutSide the UDF
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
20/40
[email protected] ENGR-25_Functions-2.ppt20
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
UDF Example cont
The variables a, b, & care local to the function
avg3, so unless you pass
their values by naming
them in the commandwindow, their values will
not be available in the
workspace outside the
function. The variable TriTot is
also local to the function.
>> a=-37; b=73; c=19;>> s = avg3(a,b,c);
>> aa =
-37>> b
b =73
>> cc =
19
>> TriTot??? Undefinedfunction or variable'TriTot'.
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
21/40
[email protected] ENGR-25_Functions-2.ppt21
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
UDF 2nd Example
New Function tetf t cos6
Write Function DecayCos with inputs of t &
function ecos = DecayCos(t,w)% Calc the decay of a cosine% Bruce Mayer, PE ENGR25 28Jun05% INPUTS:% t = time (sec)
% w = angular frequency (rads/s)% Calculation section:ePart = exp(t/6);ecos =cos(w.*t)./ePart;
w & t can be
Arrays
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
22/40
[email protected] ENGR-25_Functions-2.ppt22
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
UDF 2nd Example cont
Pass Arrays to DecayCos tetf t cos6
>> p = DecayCos([1:3],[11:13])
p =
0.0037 0.3039 0.1617
Note that in MATLAB [11:13] [11:1:13] = [ 11, 12, 13]
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
23/40
[email protected] ENGR-25_Functions-2.ppt23
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
UDF Multiple Outputs
A function may have more than one output.These are enclosed in square brackets [ ]
The OUPput is on the LEFT-hand side of the =sign in the function definition line
For example, the function Res computes theElectrical Current, Ires, and Power-
Dissipated, Pres, in an
Electrical Resistor
given its Resistance, R,and Voltage-Drop, v,
as input arguments
R
v
i
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
24/40
[email protected] ENGR-25_Functions-2.ppt24
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
UDF Multiple Outputs cont
Calling thefunction Res for
4.7 and 28 V Res has 2-INputs &
2-OUTputs>> [I1 P1] = res(4.7,28)
I1 =5.9574
P1 =166.8085
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
25/40
[email protected] ENGR-25_Functions-2.ppt25
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Examples of Fcn Def Lines
1. One input, one output:
function [area_square] = square(side)
2. Vector-Brackets are optional for one output:
function area_square = square(side)
3. Three inputs, one output (Brackets Optional):function [volume_box] = box(height,width,length)
4. One input, Two outputs (Vector-Brackets Reqd):
function [area_circle,circumf] = circle(radius)
5. Output can be a Plot as well as number(s)
function sqplot(side)
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
26/40
[email protected] ENGR-25_Functions-2.ppt26
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Example Free Fall
Consider an objectwith arbitrary mass, m,
falling downward
under the acceleration
of gravity with negligible
air-resistance (drag)
With original
velocity v0
Take DOWN as
POSITIVE direction
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
27/40
[email protected] ENGR-25_Functions-2.ppt27
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Example Free Fall cont
0
vgttv tvgtddttvdt
0
2
0 2
1
Use NewtonianMechanics to
analyze the Ballistics
of the falling Object
The Velocity andDistance-Traveled
as a function of time
& Original Velocity
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
28/40
[email protected] ENGR-25_Functions-2.ppt28
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Free Fall/Drop MATLAB Fcn
function [dist, vel] = drop(g, v0, t);% Calc Speed and distance-traveled by% dropped object subject to accel of gravity% Bruce Mayer, PE ENGR25 27Jun05%
% Parameter/Argument List% g = accel of gravity% v0 = velocity at drop point (zero-pt)% t = falling time
%% Calculation section:vel = g*t + v0;dist = 0.5*g*t.^2 + v0*t;
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
29/40
[email protected] ENGR-25_Functions-2.ppt29
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Calling Function drop
1. The variable names used in the functiondefinition may, but need not, be used when
the function is called:
>> a = 32.17; % ft/s^2
>> v_zero = -17; % ft/s>> tf = 4.3; % s
>> [dist_ft, spd_fps] = drop(a, v_zero, tf)dist_ft =224.3117
spd_fps =121.3310
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
30/40
[email protected] ENGR-25_Functions-2.ppt30
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Calling Function drop cont
2. The input variables need not be assignedvalues outside the function prior to the
function call:
>> [ft_dist, fps_spd] = drop(32.17, -17, 4.3)
ft_dist =224.3117
fps_spd =
121.3310
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
31/40
[email protected] ENGR-25_Functions-2.ppt31
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Calling Function drop cont
3. The inputs and outputs may be arrays:
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
32/40
[email protected] ENGR-25_Functions-2.ppt32
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Local Variables
The names of the input variables given inthe function definition line are local to that
function
i.e., the Function sets up a PRIVATE Workspace
This means that other variable names canbe used when you call the function
All variables inside a function are erased
after the function finishes executing, exceptwhen the same variable names appear in the
output variable list used in the function call.
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
33/40
[email protected] ENGR-25_Functions-2.ppt33
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Global Variables
Theglobal
command declares certain
variables global, and therefore their values
are available to the basic workspace and to
other functions that declare these variables
global The syntax to declare the variables a, x, and
q as GLOBAL is: global a x q
Any assignment to those variables, in anyfunction or in the base workspace, is
available to all the other functions
declaring them global.
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
34/40
[email protected] ENGR-25_Functions-2.ppt34
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Private WorkSpace
Cmd Window
WorkSpace
Function-1
WorkSpace
Command
Window
Function
1
INPUTS to
Function-1
OUTPUTS from
Function-1
( 4 3)( 4 3)( 4 3)143
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
35/40
[email protected] ENGR-25_Functions-2.ppt35
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Make atan2d Functionfunction Q = atan2d(Y,X);
% Bruce Mayer, PE * 8Sep10% ENGR25 * Fcn Lecture%% Modifies Built-in Function
atan2 to return answer in DEGREES%Q = (180/pi)*atan2(Y,X);
>> a1 = atan2d(3,-4)
a1 =
143.1301
>> a2 = atan2d(-3,4)
a2 =
-36.8699
(-4,3)(-4,3)(-4,3)
(4,-3)
37
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
36/40
[email protected] ENGR-25_Functions-2.ppt36
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
All Done for Today
Thisworkspace
for rent
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
37/40
[email protected] ENGR-25_Functions-2.ppt37
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-06 Sp12 Functions-2 BuiltIn UDF (1)
38/40
[email protected] ENGR-25_Functions-2.ppt38
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Calling Function drop 1
1. Make as current directory C:\Working_Files_Laptop_0505\BMayer\
Chabot_Engineering\ENGR-25_Comp-
Meth\E25_Demo
2. Use FILE => NEW => M-FILE to open
m-file editor
3. To Slide-28 and Copy the drop Text
4. Paste drop-Text into m-file editor and
save-as drop.m
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
39/40
[email protected] ENGR-25_Functions-2.ppt39
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Calling Function drop - 2
5. Test drop using SI units>> [y_m, s_mps] = drop(9.8, 2.3, .78)
y_m =
4.7752
s_mps =
9.9440>>
-
7/30/2019 ENGR-25 Lec-06 Sp12 Functions-2 BuiltIn UDF (1)
40/40
Bruce Mayer, PEEngineering/Math/Physics 25: Computational Methods
Calling Function drop - 3
6. The inputs and outputs may be arrays:>> [ft_fall, fps_vel] = drop(32.17,-17, [0:.5:2.5])
ft_fall =0 -4.4787 -0.9150 10.6913 30.3400 58.0313
fps_vel =-17.0000 -0.9150 15.1700 31.2550 47.3400 63.4250
This function call produces the arrays
ft_fall and fps_vel, each with sixvalues corresponding to the six values oftime in the array t.