# Chp3 MATLAB Functions: Part1

## Presentation on theme: "Chp3 MATLAB Functions: Part1"— Presentation transcript:

Chp3 MATLAB Functions: Part1
Engr/Math/Physics 25 Chp3 MATLAB Functions: Part1 Bruce Mayer, PE Registered Electrical & Mechanical Engineer

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

Functions MATLAB Has Two Types of Functions
Built-In Functions Provided by the Softeware e.g.; sqrt, exp, cos, sinh, etc. User-Defined Functions are .m-files that can accept InPut Arguments and Return OutPut Values

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; lnx log10(x) Common (base 10) logarithm; logx = log10x 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 Complex-No. Functions
Command Conventional Math Function abs(x) Absolute value (Magnitude or Modulus) angle(x) Angle of a complex number (Argument) conj(x) Complex Conjugate imag(x) Imaginary part of a complex number real(x) Real part of a complex number Useful for Analyzing Periodic Systems e.g., Sinusoidal Steady-State Electrical Ckts

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. Sort of like the FFT problem we did in lab Graph

Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu
Engr/MTH/Phys 25 Complex Numbers Bruce Mayer, PE Licensed Electrical & Mechanical Engineer

Complex Numbers – Math What do We Do with? Factoring
Let’s Make-Up or IMAGINE

Complex No.s – Basic Concept
Discriminant D World of REAL numbers x = 2a b2– 4ac b a  x2 + b  x + c = 0 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 2a |b2– 4ac| b In Engineering √(-1) = j in Math √(-1) = i

Complex Number, z, Defined
z = x + j y Imaginary part y = Im(z) Real part x = Re(z) Im(z) Real numbers Real numbers Im(z) = 0 Complex numbers Complex numbers Im(z)  0 REAL no.s described by a (number) LINE. Complex no.s described by a PLANE Re(z)

Complex No.s – Basic Rules
Powers of j j2 = –1 j3 = –j j4 = +1 j –1 = –j j4n = +1; j4n+1 = +j ; j4n+2 = –1; j4n+3 = –j for n = 0, ±1, ± 2, … Equality z1 = x1 + j y1 z2 = x2 + j y2 z1 = z2,  x1 = x2 AND y1 = y2 Do on board j-1 = -j Addition z1 + z2, = (x1 + x2) + j ( y1 + y2)

Complex No.s – Basic Rules cont
z1 = x1 + j y1 z2 = x2 + j y2 Multiplication z1 z2, = (x1 x2 – y1 y2) + j (x1 y2 + x2 y1) 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* = x2 + y2 Multiply by F.O.I.L. * For division multiply by 1, where on is the conjugate of the DIVISOR divided by itself z2 z1 = (x22 + y22) z2 z2* z1z2* x1 x2 + y1 y2 + j y1 x2 – x1 y2 Division

Complex No.s – Graphically
The Argand diagram Modulus (magnitude) of z r = mod z = |z| = x2 + y2 x y r z = x + i y Im(z) Re(z) Argument (angle) of z arctan  = arg z = x y + p, if x < 0 , if x > 0 x = r cos  Polar form of a complex number z z = r (cos  + j sin ) y = r sin 

Complex No.s – Polar Form
z1 = r1 (cos 1 + j sin 1) z2 = r2 (cos 2 + j sin 2) Multiplication z1 z2 = r1r2 (cos (1 + 2) + j sin(1 + 2)) x y r z = x + j y Im(z) Re(z) |z1z2| = |z1| |z2| ; arg(z1z2) = arg(z1) + arg(z2) Division = (cos (1 – 2) + j sin(1 – 2)) z2 z1 r2 r1 = ; arg( ) = arg(z1) – arg(z2) |z2| |z1| z2 z1

Polar Multiplication Proof
Consider: Then But Then factoring out j, & Grouping Recall Trig IDs

Polar Multiplication Proof cont
Using Trig ID in the Loooong Expression So Finally Q.E.D.

De Moivre’s Formula z1 z2 = r1r2 (cos (1 + 2) + j sin(1 + 2))
z1 z2…zn = r1r2 …rn [cos (1 + 2 + …+ n) + j sin(1 + 2 + …+ n)] z1 = z2=…= zn zn = rn (cos (n) + j sin(n)) Named after French Mathematician Abraham de Moivre ( ) r = 1 (cos  + j sin )n = cos (n) + j sin(n) French Mathematician Abraham de Moivre ( )

Complex Functions A complex function A complex conjugate function
Real function f(x) = g(x) + j h(x) Real function A complex conjugate function f*(x) = g(x) – j h(x) f(x) f*(x) = g2(x) + h2(x) f(z) = z2 + 2z + 1; z = x + j y Example: g(x,y) = (x2 – y2 + 2x + 1) h(x,y) = 2y (x + 1) f(z) = g(x,y) + j h(x,y)

Verify by MuPad From the last Line (collect comand) collect real and imaginary parts

Euler’s Formula ei e–i cos(θ) sin(θ) Im(z) Re(z) 1 –1  – 
A power series for an exponential cos(θ) sin(θ) e^j(-x) = cos(-x) + jsin(-x) => use trig IDs: cos(-y) = cosy & sin(-y) = -siny A complex conjugate is also inverse

Complex Numbers – Engineering
Natural No.s = COUNTING No.s = 1, 2, 3, 4, ... WHOLE No.s = 0, 1, 2, 3, 4, ... (includes the null = ZERO) INTEGER No.s = -3, -2, -1, 0, 1, 2, 3, ...

Complex Number Calcs Im(z) Consider a General Complex Number
y r z = x + j y Im(z) Re(z) Consider a General Complex Number This Can Be thought of as a VECTOR in the Complex Plane This Vector Can be Expressed in Polar (exponential) Form Thru the Euler Identity Where Then from the Vector Plot Need to be careful abaout the values of θ when z is in the LEFT-half of the Argand Plane => must Add 180 degrees

Complex Number Calcs cont
Consider Two Complex Numbers The PRODUCT n•m The SUM, Σ, and DIFFERENCE, , for these numbers Complex DIVISION is Painfully Tedious See Next Slide

Complex Number Division
For the Quotient n/m in Rectangular Form Use the Complex CONJUGATE to Clear the Complex Denominator The Generally accepted Form of a Complex Quotient Does NOT contain Complex or Imaginary DENOMINATORS The Exponential Form is Cleaner See Next Slide

Complex Number Division cont.
For the Quotient n/m in Exponential Form Look for lots of this in ENGR43 However Must Still Calculate the Magnitudes and Angles

Root of a Complex Number
How to Find? Use Euler Note that θ is in the 2nd Quadrant Thus In This Case

Root of A Complex Number
Now use Properites of Exponents Use Euler in Reverse In this Case By MATLAB (-7+19j)^(1/3) ans = i

ln of a NEGATIVE Number (1)
Im(z) What is r z=(-19,0)  = π –r Re(z) State −19 as a complex no. –r Find Euler Reln Quantities r, & θ

ln of a NEGATIVE Number (2)
Im(z) Note that θ is 180º, NOT Zero Thus the Polar form of −19 r z=(-19,0)  = π –r Re(z) –r Taking the ln

Log of a NEGATIVE Number
Im(z) Recall complex forms for −19 r z=(-19,0)  = π –r Re(z) –r Taking the common (Base-10) log

sin or cos of Complex number
Recall from Euler Development By Sum & Difference Formulas

sin or cos of Complex number
Thus From Previous Slide So Finally

MATLAB Complex Operations
>> a = 3+2j; >> b = -4+5i; >> c = -5-j*4; >> d = i; >> Mag_b = abs(b) Mag_b = 6.4031 >> c_star = conj(c) c_star = i >> ac = a*c ac = i

Complex Ops >> b_d = b/d b_d = 5.0000 + 4.0000i
>> c_b = c/b c_b = i >> c_a = c/a c_a = i >> Re_d = real(d) Re_d = >> Im_b = imag(b) Im_b = 5

Complex Ops >> b_sq = b^2 b_sq = -9.0000 -40.0000i
>> b_cu = b^3 b_cu = 2.3600e e+002i >> cos_a = cos(a) cos_a = i >> exp_c = exp(c) exp_c = i >> log_b = log10(b) log_b = i

Complex Ops >> 180*angle(z)/pi >> r = 73; ans =
>> x = -23; y = 19; >> z2 = complex(x,y) z2 = i >> r = 73; >> theta = 2*pi/11; >> theta_deg = 180*theta/pi theta_deg = >> z = r*exp(j*theta) z = i >> abs(z) ans = 73

Caveat MATLAB Accepts But NOT >> z2 = 3+7j;
>> z3 = 5 + i*11; >> z4 = *j; >> z2 z2 = i >> z3 z3 = i >> z4 z4 = i >> z5 = 7 + j5; ??? Undefined function or variable 'j5'.

Leonhard Euler (1707-1783) All Done for Today
".. indeed, far and away the most prolific writer in the history of the subject" writes Howard Eves in An Introduction to the History of Mathematics. Euler's contribution to mathematics is represented here by a few of the notations conventionalized by him or in his honor. Around the world, these are read, written, and spoken thousands of times every day: e for the base of the natural logarithm (a.k.a. "the calculus number") a, b, c for the sidelengths of a triangle ABC f(x) for functional value R and r for the circumradius and inradius of a triangle sin x and cos x for values of the sine and cosine functions i for the imaginary unit, the "square root of -1" capital sigma for summation. capital delta for finite difference. Euler grew up near Basel, Switzerland, and studied at an early age under Johann Bernoulli. He finished studies at the University of Basel when only 15 years old. From 1727 to 1741, Euler worked in St. Perersburg, Russia, and then moved to the Akademie in Berlin. In 1766 he returned to St. Petersburg, where he remained.

Licensed Electrical & Mechanical Engineer BMayer@ChabotCollege.edu
Engr/Math/Physics 25 Appendix Time For Live Demo Bruce Mayer, PE Licensed Electrical & Mechanical Engineer

Complex Integration Example
Us EULER to Faciltitate (Nasty) AntiDerivation I = I = Re I = Re

Illuminate Previous Slide
By EULER Using Term-by-Term Integration As “i” is just a CONSTANT Taking the REAL Part of the above

Rotation Operator Im(z) r z'(x',y') z = r (cos  + j sin ) = z(x,y) a
Re(z) r –r a z'(x',y') Polar form of a complex number z = r (cos  + j sin ) = x'= r cos( + a) = r(cos cosa – sin sina) y'= r sin( + a) = r(sin cosa + cos sina) x'= x cosa – y sina y'= x sina + y cosa The function eja can be regarded as a representation of the rotation operator R(x,y) which transforms the coordinates (x,y) of a point z into coordinates (x',y') of the rotated point z' : R(x,y) = (x',y') .

Periodicity z = z' = z' = z' The function ej occurs in the natural sciences whenever periodic motion is described or when a system has periodic structure.

Periodicity on a Circle
or n nth roots of 1 z0 Im(z) Re(z) 1 z1 z2 2 z0 = ej0 ; z1 = ej 2p/3 ; z2 = ej 4p/3 zk = ej 2pk/3, where k = 0,1,2 zk3 = (ej 2pk/3)3 = ej 2pk = 1  = zk z0 = ej0 = 1 z1 = ej 2p/3 = j z2 = ej 4p/3 = – j 2 1 3 roots of third degree of 1 zk = ej 2pk/3, where k = 0, ±1

Periodicity on a Circle cont
or n nth roots of 1 z0 Im(z) Re(z) z1 z2 z3 z-2 z-1 6 sixth roots of 1 Used for description of the properties of the benzene molecule. z0 = ej0 = 1 z±1 = e±j p/3 = ± j z±2 = e ±j 2p/3 = ± j 2 1 n nth roots of 1 zk = ej 2pk/n for k = 0, ±1, ±2, ... , ±(n –1)/ if n is odd 0, ±1, ±2, ... , ±(n/2 –1), n/2 if n is even Such functions are important for the description of systems with circular periodicity.

Periodicity on a Line f(x) = f(x + a) f(x) = f(x,y,z) = a a a a a ...
Periodic function f(x) = f(x) = f(x + a) Generalization for three-dimensional periodic systems f(x,y,z) = Such functions are important for the description of periodic systems such as crystals.

Trig on complex numbers