Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Functions Two - Computational Methods - Lecture Slides, Slides of Computational Methods

Some concept of Computational Methods are Midair Collision, Applied Math, Row and Column Vectors, Arrays Two, Charged Particle, Optimize Distribution, Functions Two, Handles Types, Integration One. Main points of this lecture are: Functions Two, User-Defined Matlab Functions, Built-In, Global and Local Variables, Subfunctions, Nested-Functions, Import Data, External Data-File, Data-Acquisition System, Built-In Functions

Typology: Slides

2012/2013

Uploaded on 04/30/2013

aadita
aadita 🇮🇳

4.6

(29)

86 documents

1 / 40

Toggle sidebar

Related documents


Partial preview of the text

Download Functions Two - Computational Methods - Lecture Slides and more Slides Computational Methods in PDF only on Docsity! Chp3 MATLAB Functions: Part2 Docsity.com 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 an External Data-File – As generated, for example, by an Electronic Data-Acquisition System Docsity.com Expressing Fcn Arguments • We can write sin2 in text, but MATLAB requires parentheses surrounding the 2 – The 2 is called the fcn argument or parameter • To evaluate sin2 in MATLAB, type sin(2) – The MATLAB function name must be followed by a pair 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)) Docsity.com 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 number and placement of parentheses when typing such expressions • Every left-facing parenthesis requires a right-facing mate. – However, this condition does NOT guarantee that the expression is CORRECT! Docsity.com Expressing Fcn Arguments cont • Another common mistake involves expressions 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))^2 ans = 0.0589 Docsity.com Why Two aTan’s? • Consider the Situation at Right (-4,3) (4,-3) 37° 143° ( ) ( ) ( ) ( ) °−=−= −= °−=−= −= − + 87.3675.0arctan 43arctan 87.3675.0arctan 43arctan x x θ θ  Due to aTan range −π/2 ≤ y ≤ π/2 Docsity.com Why Two aTan’s? cont • Calculator Confusion from (-4,3) (4,-3) 37° 143° ( ) ( ) °=−=−=°− 14343arctan43arctan37  MATLAB Solves This problem with 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 Docsity.com atan2 built into Complex angle • As noted in the Discussion of Complex numbers converting between the RECTANGULAR & POLAR forms require that we note the Range-Limits of the atan function • MATLAB’s angle function uses atan2 >> z1 = 4 - 3j z1 = 4.0000 - 3.0000i >> z2 = -4 + 3j z2 = -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 Docsity.com Built-In Inverse-Hyp Functions Command Conventional Math Function acosh(x) Inverse Hyperbolic cosine. acoth(x) Inverse Hyperbolic cotangent acsch(x) Inverse Hyperbolic cosecant asech(x) Inverse Hyperbolic secant asinh(x) Inverse Hyperbolic sine atanh(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 Docsity.com User-Defined Functions (UDFs) • The first line in a function file must begin with a FUNCTION DEFINITION LINE that has a list of inputs & 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 in SQUARE 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). Docsity.com UDF Example • The Active m-File Lines function ave3 = avg3(a, b, c) TriTot = a+b+c; ave3 = TriTot/3; • Note the use of a semicolon at the end of the lines. This prevents the values of TriTot and ave3 from being displayed Docsity.com UDF Example cont • The variables a, b, & c are local to the function avg3, so unless you pass their values by naming them in the command window, 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); >> a a = -37 >> b b = 73 >> c c = 19 >> TriTot ??? Undefined function or variable 'TriTot'. Docsity.com 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 Docsity.com 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] Docsity.com 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) Docsity.com Example  Free Fall  Consider an object with arbitrary mass, m, falling downward • under the acceleration of gravity • with negligible air-resistance (drag) • With original velocity v0  Take DOWN as POSITIVE direction Docsity.com Example  Free Fall cont ( ) 0vgttv += ( ) tvgtddttvd t 0 2 0 2 1 +=⇒= ∫  Use Newtonian Mechanics to analyze the Ballistics of the falling Object  The Velocity and Distance-Traveled as a function of time & Original Velocity Docsity.com Calling Function drop cont 2. The input variables need not be assigned values 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 Docsity.com Calling Function drop cont 3. The inputs and outputs may be arrays: Docsity.com Local Variables • The names of the input variables given in the function definition line are local to that function – i.e., the Function sets up a PRIVATE Workspace • This means that other variable names can be used when you call the function • All variables inside a function are erased after the function finishes executing, except when the same variable names appear in the output variable list used in the function call. Docsity.com Make “atan2d” Function function 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) 37° 143° Docsity.com All Done for Today This workspace for rent Docsity.com Bruce Mayer, PE Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu Engr/Math/Physics 25 Appendix ( ) 6972 23 −+−= xxxxf Docsity.com
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved