ENGR-25_Functions-1.ppt 1 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Registered Electrical & Mechanical Engineer Engr/Math/Physics 25 Chp3 MATLAB Functions: Part1
ENGR-25_Functions-1.ppt 2 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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
ENGR-25_Functions-1.ppt 3 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Functions MATLAB Has Two Types of Functions 1.Built-In Functions Provided by the Softeware e.g.; sqrt, exp, cos, sinh, etc. 2.User-Defined Functions are.m-files that can accept InPut Arguments and Return OutPut Values
ENGR-25_Functions-1.ppt 4 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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.
ENGR-25_Functions-1.ppt 5 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Built-In Exponential Functions CommandConventional Math Function exp(x) Exponential; e x sqrt(x) Square root; x log(x) Natural logarithm; lnx log10(x) Common (base 10) logarithm; logx = 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”
ENGR-25_Functions-1.ppt 6 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Built-In Complex-No. Functions Useful for Analyzing Periodic Systems e.g., Sinusoidal Steady- State Electrical Ckts CommandConventional 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
ENGR-25_Functions-1.ppt 7 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Built-In Rounding Functions CommandConventional 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
ENGR-25_Functions-1.ppt 8 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Bruce Mayer, PE Licensed Electrical & Mechanical Engineer Engr/MTH/Phys 25 Complex Numbers
ENGR-25_Functions-1.ppt 9 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Complex Numbers – Math What do We Do with? Factoring Let’s Make-Up or IMAGINE
ENGR-25_Functions-1.ppt 10 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Complex No.s – Basic Concept World of REAL numbers a x 2 + b x + c = 0 x = x = ± 2a2a2a2a b 2 – 4ac b 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 = x = ± j ± j 2a2a2a2a | b 2 – 4ac | b In Engineering √(-1) = jin Math √(-1) = i
ENGR-25_Functions-1.ppt 11 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Complex Number, z, Defined z = x + j y Real part x = Re(z) Imaginary part y = Im(z) Complex numbers numbers Im(z) 0 Real numbers numbers Im(z) = 0 Re(z) Im(z) Complex numbers Realnumbers
ENGR-25_Functions-1.ppt 12 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Complex No.s – Basic Rules Powers of j j 2 = –1 j 3 = –j j 4 = +1 j –1 = –j j 4n = +1; j 4n+1 = +j ; j 4n+2 = –1; j 4n+3 = –j for n = 0, ±1, ± 2, … Equality z 1 = x 1 + j y 1 z 2 = x 2 + j y 2 z 1 = z 2, x 1 = x 2 AND y 1 = y 2 Addition z 1 + z 2, = (x 1 + x 2 ) + j ( y 1 + y 2 )
ENGR-25_Functions-1.ppt 13 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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) 21 (z – z*) = j y j Im(z) 21 z z* = x 2 + y 2 Division z2z2z2z2 z1z1z1z1 = (x22 + y22)(x22 + y22)(x22 + y22)(x22 + y22) z 2 z 2 * z1z2*z1z2*z1z2*z1z2* = x1 x2 + y1 y2x1 x2 + y1 y2x1 x2 + y1 y2x1 x2 + y1 y2 + j (x22 + y22)(x22 + y22)(x22 + y22)(x22 + y22) y1 x2 – x1 y2y1 x2 – x1 y2y1 x2 – x1 y2y1 x2 – x1 y2
ENGR-25_Functions-1.ppt 14 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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 xy, if x > 0 r = mod z = |z| = x 2 + y 2 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
ENGR-25_Functions-1.ppt 15 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Complex No.s – Polar Form z 1 = r 1 (cos + j sin z 2 = r 2 (cos + j sin x y r z = x + j y Im(z) Re(z) |z 1 z 2 | = |z 1 | |z 2 | ; arg(z 1 z 2 ) = arg(z 1 ) + arg(z 2 ) z 1 z 2 = r 1 r 2 (cos ( + ) + j sin( + )) = (cos ( – ) + j sin( – )) = (cos ( – ) + j sin( – )) z2z2z2z2 z1z1z1z1 r2r2r2r2 r1r1r1r1 = ; arg( ) = arg(z 1 ) – arg(z 2 ) = ; arg( ) = arg(z 1 ) – arg(z 2 ) |z 2 | |z 1 | z2z2z2z2 z1z1z1z1 z2z2z2z2 z1z1z1z1 Multiplication Multiplication Division
ENGR-25_Functions-1.ppt 16 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Polar Multiplication Proof Consider: Then ButThen factoring out j, & Grouping Recall Trig IDs
ENGR-25_Functions-1.ppt 17 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Polar Multiplication Proof cont Using Trig ID in the Loooong Expression So Finally
ENGR-25_Functions-1.ppt 18 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods De Moivre’s Formula z 1 z 2 = r 1 r 2 (cos ( + ) + j sin( + )) z 1 z 2 …z n = r 1 r 2 …r n [cos ( + …+ n ) + j sin( + …+ n )] z n = r n (cos (n ) + j sin(n )) z 1 = z 2 =…= z n r = 1 (cos + j sin ) n = cos (n ) + j sin(n ) FFrench Mathematician Abraham de Moivre ( )
ENGR-25_Functions-1.ppt 19 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Complex Functions f(x) = g(x) + j h(x) A complex 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 + 2z + 1; z = x + j y f(z) = g(x,y) + j h(x,y) g(x,y) = (x 2 – y 2 + 2x + 1) h(x,y) = 2y (x + 1)
ENGR-25_Functions-1.ppt 20 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Euler’s Formula A complex conjugate is also inverse A power series for an exponential eieieiei Im(z) Re(z) 1 1 –1–1–1–1 –1–1–1–1 – e–ie–ie–ie–i cos(θ)sin(θ)
ENGR-25_Functions-1.ppt 21 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Complex Numbers – Engineering
ENGR-25_Functions-1.ppt 22 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Complex Number Calcs 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 x y r z = x + j y Im(z) Re(z)
ENGR-25_Functions-1.ppt 23 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Complex Number Calcs cont Consider Two Complex Numbers The SUM, Σ, and DIFFERENCE, , for these numbers The PRODUCT nm Complex DIVISION is Painfully Tedious See Next Slide
ENGR-25_Functions-1.ppt 24 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Complex Number Division For the Quotient n/m in Rectangular Form The Generally accepted Form of a Complex Quotient Does NOT contain Complex or Imaginary DENOMINATORS Use the Complex CONJUGATE to Clear the Complex Denominator The Exponential Form is Cleaner See Next Slide
ENGR-25_Functions-1.ppt 25 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Complex Number Division cont. For the Quotient n/m in Exponential Form However Must Still Calculate the Magnitudes and Angles Look for lots of this in ENGR43
ENGR-25_Functions-1.ppt 26 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Root of A Complex Number How to Find? Use Euler In This Case Note that θ is in the 2 nd Quadrant Thus
ENGR-25_Functions-1.ppt 27 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods 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
ENGR-25_Functions-1.ppt 28 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods ln of a NEGATIVE Number (1) What is Im(z) Re(z) r –r–r–r–r –r–r–r–r = π z=(-19,0) State −19 as a complex no. Find Euler Reln Quantities r, & θ
ENGR-25_Functions-1.ppt 29 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods ln of a NEGATIVE Number (2) Note that θ is 180º, NOT Zero Thus the Polar form of −19 Im(z) Re(z) r –r–r–r–r –r–r–r–r = π z=(-19,0) Taking the ln
ENGR-25_Functions-1.ppt 30 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Log of a NEGATIVE Number Recall complex forms for −19 Im(z) Re(z) r –r–r–r–r –r–r–r–r = π z=(-19,0) Taking the common (Base-10) log
ENGR-25_Functions-1.ppt 31 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods MATLAB Complex Operations >> a = 3+2j; >> b = -4+5i; >> c = -5-j*4; >> d = i; >> ac = a*c ac = i >> Mag_b = abs(b) Mag_b = >> c_star = conj(c) c_star = i
ENGR-25_Functions-1.ppt 32 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Complex Ops >> b_d = b/d b_d = i >> c_b = c/b c_b = i >> c_a = c/a c_a = i >> Re_d = real(d) Re_d = 0 >> Im_b = imag(b) Im_b = 5
ENGR-25_Functions-1.ppt 33 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Complex Ops >> b_sq = b^2 b_sq = i >> b_cu = b^3 b_cu = e e+002i >> cos_a = cos(a) cos_a = i >> exp_c = exp(c) exp_c = i >> log_b = log10(b) log_b = i
ENGR-25_Functions-1.ppt 34 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Complex Ops >> r = 73; >> theta = 2*pi/11; >> theta_deg = 180*theta/pi theta_deg = >> z = r*exp(j*theta) z = i >> abs(z) ans = 73 >> 180*angle(z)/pi ans = >> x = -23; y = 19; >> z2 = complex(x,y) z2 = i
ENGR-25_Functions-1.ppt 35 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Caveat MATLAB Accepts >> z2 = 3+7j; >> z3 = 5 + i*11; >> z4 = *j; >> z2 z2 = i >> z3 z3 = i >> z4 z4 = i But NOT >> z5 = 7 + j5; ??? Undefined function or variable 'j5'.
ENGR-25_Functions-1.ppt 36 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods All Done for Today Leonhard Euler ( )
ENGR-25_Functions-1.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-1.ppt 38 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Complex Integration Example I =I =I =I = I = Re Us EULER to Faciltitate (Nasty) AntiDerivation
ENGR-25_Functions-1.ppt 39 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Illuminate Previous Slide By EULER Using Term-by-Term Integration As “i” is just a CONSTANT Taking the REAL Part of the above
ENGR-25_Functions-1.ppt 40 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Rotation Operator Polar form of a complex number z = r (cos + j sin z(x,y) Im(z) Re(z) r –r–r–r–r –r–r–r–r z'(x',y') x'= r cos( + = r(cos cos – sin sin y'= r sin( + = r(sin cos + cos sin x'= x cos – y sin y'= x sin + y cos The function e j 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').
ENGR-25_Functions-1.ppt 41 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Periodicity z' =z' =z' =z' = z' =z' =z' =z' = z =z =z =z = z'z'z'z' The function e j occurs in the natural sciences whenever periodic motion is described or when a system has periodic structure.
ENGR-25_Functions-1.ppt 42 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Periodicity on a Circle or n nth roots of 1 z0z0z0z0 Im(z) Re(z) z1z1z1z1 z2z2z2z2 z 0 = e j0 ; z 1 = e j 2 /3 ; z 2 = e j 4 /3 z 0 = e j0 ; z 1 = e j 2 /3 ; z 2 = e j 4 /3 z k = e j 2 k/3, where k = 0,1,2 z k = e j 2 k/3, where k = 0,1,2 z k 3 = (e j 2 k/3 ) 3 = e j 2 k = 1 1 = z k z k 3 = (e j 2 k/3 ) 3 = e j 2 k = 1 1 = z k z 0 = e j0 = 1 z 1 = e j 2 /3 = - + j z 2 = e j 4 /3 = - – j z k = e j 2 k/3, where k = 0, ±1 z k = e j 2 k/3, where k = 0, ±1 3 roots of third degree of 1
ENGR-25_Functions-1.ppt 43 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Periodicity on a Circle cont or n nth roots of 1 z 0 = e j0 = 1 z ±1 = e ±j /3 = ± j z ±2 = e ±j 2 /3 = - ± j sixth roots of 1 z0z0z0z0 Im(z) Re(z) z1z1z1z1 z2z2z2z2 z3z3z3z3 z -2 z -1 Used for description of the properties of the benzene molecule. n nth roots of 1 z k = e j 2 k/n for k = z k = e j 2 k/n for k = 0, ±1, ±2,..., ±(n –1)/2 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.
ENGR-25_Functions-1.ppt 44 Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods Periodicity on a Line Such functions are important for the description of periodic systems such as crystals aaaaaaaaaa f(x) = f(x + a) f(x) = Periodic function Generalization for three-dimensional periodic systems f(x,y,z) =