Docsity

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

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: Part

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

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 = 2.6458 3.3166 2.1013 - 1.1897i

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))

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))^

or cos(y)^

  • Do NOT write the Expression as
    • cos^2(y)
    • cos^2y
    • cos(y^2)

cos(pi/1.73) ans = -0.

(cos(pi/1.73))^ ans =

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°.

Command Conventional 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

rads

rads −

Why Two aTan’s?

  • Consider the Situation

at Right

(-4,3)(-4,3)(-4,3)

(4,-3)

37°

143°

( ) ( ) ( ) = ( − ) = − °

−

arctan 0. 75 36. 87
arctan 3 4
arctan 0. 75 36. 87
arctan 3 4

x

x

θ

θ

 Due to aTan range

−π/2 ≤ y ≤ π/

Why Two aTan’s? cont

  • Calculator Confusion from

(-4,3)(-4,3)(-4,3)

(4,-3)

37°

143°

− 37 ° = arctan (− 3 4 ) =arctan( 3 − 4 ) = 143 °

 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. q2 = atan(3/-4) q2 = -0.

 MATLAB atan

q3 = atan2(-3,4) q3 = -0. q4 = atan2(3,-4) q4 =

“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)

Built-In Hyperbolic Functions

Command Conventional 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

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).

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 ave from being displayed

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 =

ave

??? Undefined function or variable 'ave3'.

 The variable ave3 is LOCAL to the UDF

  • It’s Value is NOT defined OutSide the UDF

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 =

b

b = 73

c c = 19 TriTot ??? Undefined function or variable 'TriTot'.