User Defined Functions - Computational Methods - Lecture Slides, Slides of Calculus for Engineers

These are the Lecture Slides of Computational Methods which includes Thévenin’s Equivalent Circuit, Circuit Simplification, Analysis of Power Transfer, Voltage Division, Analytical Game Plan, Array Operation, Element Operations, Number of Allowable Values etc.Key important points are: User Defined Functions, External Data-File, Data-Acquisition System, Types of Functions, Array Function Operations, Function Composition, Order of Precedence, Placement of Parentheses, Built-In Trig Functions

Typology: Slides

2012/2013

Uploaded on 03/26/2013

abduu
abduu 🇮🇳

4.4

(49)

195 documents

1 / 37

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
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
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25

Partial preview of the text

Download User Defined Functions - Computational Methods - Lecture Slides and more Slides Calculus for Engineers in PDF only on Docsity!

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

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

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

  • 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!

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

° ° =

180

Built-In Inverse-Trig Functions

 MATLAB trig -1^ fcns also operate in radian mode

  • i.e., They Return Angles in RADIANS

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

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 =

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 atan

z1 = 4 - 3j z1 =4.0000 - 3.0000i z2 z2= = -4 + 3j -4.0000 + 3.0000i r1 r1= = abs(z1) 5 r2 = abs(z2) r2 = 5 theta1 theta1 = = atan2(imag(z1),real(z1)) -0. theta2 = atan2(imag(z2),real(z2))

theta22.4981 =

z1a z1a = = r1 *exp(j *theta1) 4.0000 - 3.0000i z2b z2b = = r2 *exp(j *theta2)

-4.0000 + 3.0000i

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

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 =

asinh(-37) ans = -4.

acosh(37) ans =

acosh(-37) ans = 4.3039 + 3.1416i

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” this Function with its OutPut Agrument

TriAve = avg3(7,13,-3) TriAve =

 To Determine

TriAve the

Function takes

  • a = 7
  • b = 13
  • c = −

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

UDF – 2 nd^ Example

  • New Function

f ( ) t e ( t )

t

cos ω

 Write Function DecayCos with inputs of t & ω

function ecos = DecayCos(t,w) % Calc the decay of a cosine % Bruce Mayer, PE • ENGR25 • 28Jun % 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