ENGR-25_Functions-2.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.

Slides:



Advertisements
Similar presentations
Lecture 14 User-defined functions Function: concept, syntax, and examples © 2007 Daniel Valentine. All rights reserved. Published by Elsevier.
Advertisements

Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
6/4/13 Obj: SWBAT plot polar coordinates
A MATLAB function is a special type of M-file that runs in its own independent workspace. It receives input data through an input argument list, and returns.
6.6 – TRIG INVERSES AND THEIR GRAPHS
The Inverse Trigonometric Functions Section 4.2. Objectives Find the exact value of expressions involving the inverse sine, cosine, and tangent functions.
LG 2-3 Inverse Trig Functions
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
Section 4 Inverses of the Trigonometric Functions
7-6 The Inverse Trigonometric Functions
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Introduction to MATLAB 7 for Engineers William J. Palm.
8.5 Solving More Difficult Trig Equations
1 7.3 Evaluating Trig Functions of Acute Angles In this section, we will study the following topics: Evaluating trig functions of acute angles using right.
Inverse Trigonometric Functions
6.3 – Trig Identities.
1 Trigonometric Functions of Any Angle & Polar Coordinates Sections 8.1, 8.2, 8.3,
Trigonometric Functions of Any Angle & Polar Coordinates
Introduction to MATLAB ENGR 1187 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
Objectives ► The Inverse Sine Function ► The Inverse Cosine Function ► The Inverse Tangent Function ► The Inverse Secant, Cosecant, and Cotangent Functions.
5.1 Trigonometric Functions of Acute Angles Fri Oct 17 Do Now Solve for x 1) 1^2 + x^2 = 2^2 2) x^2 + x^2 = 1.
ENGR-25_MATLAB_OverView-1.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. A Concise Introduction to MATLAB ® William J. Palm III.
Matlab Review Exam 2 – Oct 28, Exam Reminders Open book. Only Matlab book, no notes or loose papers in book. Open book. Only Matlab book, no notes.
Precalculus Section 7.5. Warmup Graph the function. State the Domain, Range, Asymptotes, and Period 1.f(x) = -2 tan(1/3 x) 2.f(x) = sec(2x) + 1.
Recap Graphic Window Edit Window Start Button Matrices in MATLAB Scalar Operations Order of Operations Array Operations Matrix Addition Matrix Multiplication.
Section 6.1 Inverse Trig Functions Section 6.2 Trig Equations I Section 6.3 Trig Equations II Section 6.4 Equations Chapter 6 Inverse Trig Functions and.
Copyright © 2005 Pearson Education, Inc.. Chapter 6 Inverse Circular Functions and Trigonometric Equations.
Copyright © 2009 Pearson Addison-Wesley Inverse Circular Functions and Trigonometric Equations.
ENG 1181 College of Engineering Engineering Education Innovation Center MATLAB is a powerful program for numerical computations, plotting and programming.
Introduction to MATLAB ENGR 1181 MATLAB 1. Programming In The Real World Programming is a powerful tool for solving problems in every day industry settings.
Introduction to Engineering MATLAB – 1 Introduction to MATLAB Agenda Introduction Arithmetic Operations MATLAB Windows Command Window Defining Variables.
Slide Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
Copyright © 2009 Pearson Addison-Wesley Trigonometric Functions.
13.7 (part 2) answers 34) y = cos (x – 1.5) 35) y = cos (x + 3/(2π)) 36) y = sin x –3π 37) 38) y = sin (x – 2) –4 39) y = cos (x +3) + π 40) y = sin (x.
ENGR-25_Functions-1.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Registered Electrical.
ENGR-25_Programming-3.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.
Section 7.5 Inverse Circular Functions
Introduction to MATLAB ENGR 1181 MATLAB 1. Opening MATLAB  Students, please open MATLAB now.  CLICK on the shortcut icon → Alternatively, select… start/All.
Chapter 3 MATLAB Fundamentals Introduction to MATLAB Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
ENGR-25_MATLAB_OverView-2.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical.
MA/CS 375 Fall 2002 Lecture 3. Example 2 A is a matrix with 3 rows and 2 columns.
Inverse Trig Functions. Recall We know that for a function to have an inverse that is a function, it must be one-to-one—it must pass the Horizontal Line.
Introduction to MATLAB for Engineers, Third Edition William J. Palm III Chapter 3 Functions and Files PowerPoint to accompany Copyright © The McGraw-Hill.
C++ Programming Lecture 9 Functions – Part I By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Recap Saving Plots Summary of Chapter 5 Introduction of Chapter 6.
Trigonometric Functions of Any Angle & Polar Coordinates
ENGR-25_Programming-1.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Registered Electrical.
ENG 1181 First-Year Engineering Program College of Engineering Engineering Education Innovation Center First-Year Engineering Program MAT - Introduction.
Warm-up – 9/18/2015 Do your warm-up in your notes 1) 2) 3)
MA/CS 375 Fall 2003 Lecture 3. .* Multiplication We can use the.* operator to perform multiplication entry by entry of two matrices:
MA/CS 375 Fall 2002 Lecture 2. Motivation for Suffering All This Math and Stuff Try the Actor demo from
4.2 Trig Functions of Acute Angles. Trig Functions Adjacent Opposite Hypotenuse A B C Sine (θ) = sin = Cosine (θ ) = cos = Tangent (θ) = tan = Cosecant.
Describe the vertical shift in the graph of y = -2sin3x + 4. A.) Up 2 B.) Down 2 C.) Up 4 D.) Down 4.
Sine & Cosine Tangent & Cotangent Secant & Cosecant.
Section 4.2 The Unit Circle. Has a radius of 1 Center at the origin Defined by the equations: a) b)
MATLAB (Matrix Algebra laboratory), distributed by The MathWorks, is a technical computing environment for high performance numeric computation and.
Copyright © 2009 Pearson Addison-Wesley Trigonometric Functions.
User Defined Functions Spring EE 201. Class Learning Objectives  Achieve Comprehension LOL of User Defined Functions. Spring
Copyright © 2009 Pearson Addison-Wesley Trigonometric Identities and Equations.
A Concise Introduction to MATLAB®
Introduction to MATLAB 7
Copyright © 2017, 2013, 2009 Pearson Education, Inc.
Analytic Trigonometry
Do Now The terminal side of angle θ in standard position passes through the point (12, 16). Find the value of the six trigonometric functions of angle.
Introduction to the Six Trigonometric Functions & the Unit Circle
ECE 1304 Introduction to Electrical and Computer Engineering
5 Trigonometric Functions Copyright © 2009 Pearson Addison-Wesley.
Warm-up: 1) Make a quick sketch of each relation
Graph of Secant, Cosecant, and Cotangent
The Inverse Trigonometric Functions (Continued)
Presentation transcript:

ENGR-25_Functions-2.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical & Mechanical Engineer Engr/Math/Physics 25 Chp3 MATLAB Functions: Part2

ENGR-25_Functions-2.ppt 2 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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

ENGR-25_Functions-2.ppt 3 Bruce Mayer, PE Engineering/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 InPut Arguments/Parameters and Return OutPut Values

ENGR-25_Functions-2.ppt 4 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Array Function Operations  MATLAB will treat a variable as an array automatically.  For example, to compute the square roots of 7, 11 and 3-5j, type >> u = [7, 11, (3-5j)] >> sqrt(u) ans = i

ENGR-25_Functions-2.ppt 5 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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 2 nd 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))

ENGR-25_Functions-2.ppt 6 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Expressing Fcn Arguments cont  Evaluate sin(x 2 + 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!

ENGR-25_Functions-2.ppt 7 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Expressing Fcn Arguments cont  Another common mistake involves expressions like cos 2 y, 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 = >> (cos(pi/1.73))^2 ans =

ENGR-25_Functions-2.ppt 8 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Built-In Trig Functions  MATLAB trig fcns operate in radian mode Thus sin(5) returns the sine of 5 rads, NOT the sine of 5°. CommandConventional Math Function cos(x) Cosine; cos x cot(x) Cotangent; cot x csc(x) Cosecant; csc x sec(x) Secant; sec x sin(x) Sine; sin x tan(x) Tangent; tan x

ENGR-25_Functions-2.ppt 9 Bruce Mayer, PE Engineering/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 CommandConventional Math Function acos(x) Inverse cosine; arccos x. acot(x) Inverse cotangent; arccot x. acsc(x) Inverse cosecant; arccsc x asec(x) Inverse secant; arcsec x asin(x) Inverse sine; arcsin x atan(x) Inverse tangent; arctan x atan2(y,x) Four-quadrant inverse tangent Two aTan’s?

ENGR-25_Functions-2.ppt 10 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Why Two aTan’s?  Consider the Situation at Right  Due to aTan range −  /2  y   /2

ENGR-25_Functions-2.ppt 11 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Why Two aTan’s? cont  Calculator Confusion from  MATLAB Solves This problem with atan2 which allows input of the CoOrds to ID the proper Quadant  MATLAB atan >> q1 = atan(-3/4) q1 = >> q2 = atan(3/-4) q2 =  MATLAB atan2 >> q3 = atan2(-3,4) q3 = >> q4 = atan2(3,-4) q4 =

ENGR-25_Functions-2.ppt 12 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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 = i >> z2 = j z2 = i >> r1 = abs(z1) r1 = 5 >> r2 = abs(z2) r2 = 5 >> theta1 = atan2(imag(z1),real(z1)) theta1 = >> theta2 = atan2(imag(z2),real(z2)) theta2 = >> z1a = r1 *exp(j *theta1) z1a = i >> z2b = r2 *exp(j *theta2) z2b = i

ENGR-25_Functions-2.ppt 13 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods “Degree” Trig Functions  MATLAB now has Trig Functions that Operate 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) NO atan2d(y) though (we’ll make one)

ENGR-25_Functions-2.ppt 14 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Built-In Hyperbolic Functions CommandConventional Math Function cosh(x) Hyperbolic cosine. coth(x) Hyperbolic cotangent csch(x) Hyperbolic cosecant sech(x) Hyperbolic secant sinh(x) Hyperbolic sine tanh(x) Hyperbolic tangent

ENGR-25_Functions-2.ppt 15 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Built-In Inverse-Hyp Functions CommandConventional 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 = >> asinh(-37) ans = >> acosh(37) ans = >> acosh(-37) ans = i

ENGR-25_Functions-2.ppt 16 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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 1 st 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).

ENGR-25_Functions-2.ppt 17 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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

ENGR-25_Functions-2.ppt 18 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods UDF Example cont  “Call” this Function with its OutPut Agrument >> TriAve = avg3(7,13,-3) TriAve =  To Determine TriAve the Function takes a = 7 b = 13 c = −3

ENGR-25_Functions-2.ppt 19 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods UDF Example cont  Call the avg3 function withOUT its output argument and try to access its internal value, avg3. You will see an error message. >> avg3(-23,19,29) ans = >> ave3 ??? Undefined function or variable 'ave3'.  The variable ave3 is LOCAL to the UDF It’s Value is NOT defined OutSide the UDF

ENGR-25_Functions-2.ppt 20 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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'.

ENGR-25_Functions-2.ppt 21 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods UDF – 2 nd Example  New Function  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

ENGR-25_Functions-2.ppt 22 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods UDF – 2 nd Example cont  Pass Arrays to DecayCos >> p = DecayCos([1:3],[11:13]) p =  Note that in MATLAB [11:13]  [11:1:13] = [ 11, 12, 13]

ENGR-25_Functions-2.ppt 23 Bruce Mayer, PE Engineering/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 the Electrical Current, Ires, and Power- Dissipated, Pres, in an Electrical Resistor given its Resistance, R, and Voltage-Drop, v, as input arguments

ENGR-25_Functions-2.ppt 24 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods UDF – Multiple Outputs cont  Calling the function Res for 4.7Ω and 28 V Res has 2-INputs & 2-OUTputs >> [I1 P1] = res(4.7,28) I1 = P1 =

ENGR-25_Functions-2.ppt 25 Bruce Mayer, PE Engineering/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)

ENGR-25_Functions-2.ppt 26 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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 v 0  Take DOWN as POSITIVE direction

ENGR-25_Functions-2.ppt 27 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Example  Free Fall cont  Use Newtonian Mechanics to analyze the Ballistics of the falling Object  The Velocity and Distance-Traveled as a function of time & Original Velocity

ENGR-25_Functions-2.ppt 28 Bruce Mayer, PE Engineering/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;

ENGR-25_Functions-2.ppt 29 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Calling Function drop 1.The variable names used in the function definition 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 = spd_fps =

ENGR-25_Functions-2.ppt 30 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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 = fps_spd =

ENGR-25_Functions-2.ppt 31 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Calling Function drop cont 3.The inputs and outputs may be arrays:

ENGR-25_Functions-2.ppt 32 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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.

ENGR-25_Functions-2.ppt 33 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Global Variables  The global 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 any function or in the base workspace, is available to all the other functions declaring them global.

ENGR-25_Functions-2.ppt 34 Bruce Mayer, PE Engineering/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

ENGR-25_Functions-2.ppt 35 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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 = >> a2 = atan2d(-3,4) a2 =

ENGR-25_Functions-2.ppt 36 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods All Done for Today This workspace for rent

ENGR-25_Functions-2.ppt 37 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical & Mechanical Engineer Engr/Math/Physics 25 Appendix

ENGR-25_Functions-2.ppt 38 Bruce Mayer, PE Engineering/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

ENGR-25_Functions-2.ppt 39 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Calling Function drop Test drop using SI units >> [y_m, s_mps] = drop(9.8, 2.3,.78) y_m = s_mps = >>

ENGR-25_Functions-2.ppt 40 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Calling Function drop The inputs and outputs may be arrays: >> [ft_fall, fps_vel] = drop(32.17,-17, [0:.5:2.5]) ft_fall = fps_vel =  This function call produces the arrays ft_fall and fps_vel, each with six values corresponding to the six values of time in the array t.