Docsity

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

Partial preview of the text

Download Functions - Computational Methods - Lecture Slides and more Slides Computational Methods in PDF only on Docsity!

Chp3 MATLAB

Functions: Part

Learning Goals

  • Understand the difference between Built-In
and User-Defined 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

Getting Help for Functions

  • Use the lookfor command to find functions that
are relevant to your application
  • For example typing lookfor complex returns
a list of functions that operate on complex
numbers (more to come):

>> lookfor complex ctranspose.m: %' Complex conjugate transpose. COMPLEX Construct complex result from real and imaginary parts. CONJ Complex conjugate. CPLXPAIR Sort numbers into complex conjugate pairs. IMAG Complex imaginary part. REAL Complex real part. CPLXMAP Plot a function of a complex variable.

Built-In Exponential Functions

Command Conventional Math Function

exp(x) (^) Exponential; ex sqrt(x) (^) Square root; √ x log(x) (^) Natural logarithm; ln x log10(x) (^) Common (base 10) logarithm; log x = log 10 x

 Note the use of log for
NATURAL Logarithms and
log10 for “normal” Logarithms
  • This a historical Artifact from the FORTRAN Language – FORTRAN designers were concerned with confusing ln with “one-n”

Built-In Rounding Functions

Command Conventional Math Function

ceil(x) (^) Round to nearest integer toward +∞ fix(x) (^) Round to nearest integer toward zero floor(x) (^) Round to nearest integer toward −∞ round(x) (^) Round toward nearest integer.

sign(x) Signum function: +1 if x > 0; 0 if x = 0; −1 if x < 0.

Graph

Bruce Mayer, PE Licensed Electrical & Mechanical Engineer [email protected]

Engr/MTH/Phys 25

Complex

Numbers

j = - (^1) i = - 1

Complex No.s – Basic Concept

World of REAL numbers

a × x^2 + b × x + c = (^0) x = ±^ 2 a

b b^2 – 4 ac

Discriminant D

Solution(s) of a quadratic equation exist only for non-negative values of D!

World of COMPLEX numbers

Solution(s) of a quadratic equation exist also for negative values of D!

x = ±^ j^ 2 a^ |b

b^2 – 4 ac| j ≡ − 1

In Engineering √( -1) = j in Math √( -1) = i

Complex Number, z , Defined

i = j ≡ − 1

z = x + j y Real part x = Re( z )

Imaginary part y = Im( z )

Complex
numbers
Im(z) ≠ 0
Real
numbers
Im( z ) = 0 Re( z )

Im( z )

Complex numbers

Real numbers

Complex No.s – Basic Rules cont

z 1 = x 1 + j y 1 z 2 = x 2 + j y 2

 Multiplication z 1 z 2, =^ ( x 1 x 2 –^ y 1 y 2 ) +^ j^ ( x 1 y 2 + x 2 y 1 )
 The complex conjugate z = x + j y^ z *^ = x – j y

( _z + z_* ) = x ≡ Re( z ) 2

1 ( _z – z_* ) = j y ≡ j Im( z ) 2

1 _z z = x_*^2 + y^2

 Division z

2

z 1

= ( x

z 2 z 2 * 22 + y 22 )

z 1 z 2 *

x 1 x 2 + y 1 y 2

+ j ( x

22 +^ y 22 )

y 1 x 2 – x 1 y 2

i = j ≡ − 1

Complex No.s – Graphically

x

y r θ

z = x + i y

Im( z )

Re( z )

The Argand diagram (^) Modulus (magnitude) of z

arctan

θ = arg z = x

y (^) + π, if x < 0

arctan (^) x^ y^ , if x > 0

r = mod z = |z| = x^2 + y^2

x = r cos θ
y = r sin θ

Argument (angle) of z

Polar form of a complex number z

z = r (cos θ + j sin θ)

i = j ≡ − 1

Polar Multiplication Proof

  • Consider: w 1 (^) = d 1 (cos φ 1 + j sin φ 1 ) w 2 = d 2 (cos φ 2 + j sin φ 2 )
 Then

1 2 (^121212212 )

1 2 1 2 1 1 2 2 cos cos cos sin sin cos sin sin

cos sin cos sin φ φ φ φ φ φ φ φ

φ φ φ φ d d j j j

ww d d j j

= + + +

= + +

 But j^^2 = −^1 Then factoring out j , & Grouping

w 1 w 2 = d 1 d 2 [^ ( cosφ 1 cosφ 2 −sinφ 1 sinφ 2 )^ + j (sin^ φ 1 cosφ 2 +cosφ 1 sin φ 2 )]

 Recall
Trig IDs

( ) α β α β (α β)

α β α β α β

sin cos cos sin sin
cos cos sin sin cos

i = j ≡ − 1

Polar Multiplication Proof cont

  • Using Trig ID in the Loooong Expression
 So Finally

w 1 w 2 = d 1 d 2 [cos (φ 1 +φ 2 ) + j sin (φ 1 + φ 2 )]

cos (φ + 1 φ 2 )

w 1 w 2 = d 1 d 2 [ ( cos φ 1 cosφ 2 −sinφ 1 sin φ 2 ) + j (sin φ 1 cosφ 2 +cosφ 1 sin φ 2 )]

sin (φ + 1 φ 2 )

i = j ≡ − 1

Complex Functions

f ( x ) = g ( x ) + j h ( x )

 A complex function

Real function

Real function

 A complex conjugate function

_f_* ( x ) = g ( x ) – j h ( x ) f ( x ) _f_* ( x ) = g^2 ( x ) + h^2 ( x )

 Example: f ( z ) =^ z

(^2) + 2 z + 1; z = x + j y

f ( z ) = g ( x , y ) + j h ( x , y )

g ( x , y ) = ( x^2 – y^2 + 2 x + 1) h ( x , y ) = 2 y ( x + 1)

i = j ≡ − 1

Verify by MuPad

  • From the last Line
(collect comand)
collect real and
imaginary parts

( )

( )

( , ) 2 ( 1 )

2 2

h x y y x
h x y xy y
g x y x y x