bruce mayer, pe registered electrical & mechanical engineer bmayer@chabotcollege
DESCRIPTION
Engr/Math/Physics 25. Chp3 MATLAB Functions: Part1. Bruce Mayer, PE Registered Electrical & Mechanical Engineer [email protected]. Learning Goals. Understand the difference between Built-In and User-Defined Functions Write User Defined Functions - PowerPoint PPT PresentationTRANSCRIPT
[email protected] • ENGR-25_Functions-1.ppt1
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Bruce Mayer, PERegistered Electrical & Mechanical Engineer
Engr/Math/Physics 25
Chp3 MATLABFunctions:
Part1
[email protected] • ENGR-25_Functions-1.ppt2
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
[email protected] • ENGR-25_Functions-1.ppt3
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Functions MATLAB Has Two Types of Functions1. 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
[email protected] • ENGR-25_Functions-1.ppt4
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 complexctranspose.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.
[email protected] • ENGR-25_Functions-1.ppt5
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Built-In Exponential FunctionsCommand Conventional Math Function
exp(x) Exponential; ex
sqrt(x) Square root; xlog(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”
[email protected] • ENGR-25_Functions-1.ppt6
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
Command Conventional Math Functionabs(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
[email protected] • ENGR-25_Functions-1.ppt7
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Built-In Rounding FunctionsCommand 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
[email protected] • ENGR-25_Functions-1.ppt8
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Bruce Mayer, PELicensed Electrical & Mechanical Engineer
Engr/MTH/Phys 25
Complex Numbers
1- j 1- i
[email protected] • ENGR-25_Functions-1.ppt9
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Complex Numbers – Math What do We Do
with?
1- i
7x Factoring
7171 x Let’s Make-Up or
IMAGINE
Def.)(Engr 1
Def.)(Math 1
j
i
jx 646.2
[email protected] • ENGR-25_Functions-1.ppt10
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Complex No.s – Basic Concept
World of REAL numbersa x2 + b x + c = 0 x =
± 2ab2– 4ac b
Discriminant D
Solution(s) of a quadratic equation exist only for non-negative values of D !
World of COMPLEX numbersSolution(s) of a quadratic equation exist also for negative values of D !
x =
± j 2a
|b2– 4ac| b 1jIn Engineering √(-1) = j in Math √(-1) = i
[email protected] • ENGR-25_Functions-1.ppt11
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Complex Number, z, Defined1 ji
z = x + j yReal partx = Re(z)
Imaginary party = Im(z)
Complex numbersIm(z) 0
Real numbersIm(z) = 0 Re(z)
Im(z)
Complex numbersReal
numbers
[email protected] • ENGR-25_Functions-1.ppt12
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
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
Addition
z1 + z2, = (x1 + x2) + j ( y1 + y2)
1 ji
[email protected] • ENGR-25_Functions-1.ppt13
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
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)21 (z – z*) = j y j Im(z)2
1z z* = x2 + y2
Division z2
z1 = (x22 + y2
2)z2 z2*z1z2* =
x1 x2 + y1 y2 + j (x22 + y2
2)y1 x2 – x1 y2
1 ji
[email protected] • ENGR-25_Functions-1.ppt14
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Complex No.s – Graphically
x
yr
z = x + i y
Im(z)
Re(z)
The Argand diagram Modulus (magnitude) of z
arctan = arg z =
xy + p, if x < 0
arctan xy , if x > 0
r = mod z = |z| = x2 + y2
x = r cos
y = r sin
Argument (angle) of z
Polar form of a complex number z
z = r (cos + j sin
1 ji
[email protected] • ENGR-25_Functions-1.ppt15
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Complex No.s – Polar Formz1 = r1 (cos 1 + j sin 1 z2 = r2 (cos 2 + j sin 2
x
yr
z = x + j y
Im(z)
Re(z)
|z1z2| = |z1| |z2| ; arg(z1z2) = arg(z1) + arg(z2)
z1 z2 = r1r2 (cos (1 + 2) + j sin(1 + 2))
= (cos (1 – 2) + j sin(1 – 2))z2
z1
r2
r1
= ; arg( ) = arg(z1) – arg(z2) |z2||z1|
z2
z1
z2
z1
Multiplication
Division
1 ji
[email protected] • ENGR-25_Functions-1.ppt16
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Polar Multiplication Proof Consider:
22221111 sincossincos jdwjdw
Then 21
221212121
22112121
sinsincossinsincoscoscos
sincossincos
jjjdd
jjddww
But 12 j Then factoring out j, & Grouping 212121212121 sincoscossinsinsincoscos jddww
Recall Trig IDs
sinsincoscossincossinsincoscos
1 ji
[email protected] • ENGR-25_Functions-1.ppt17
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Polar Multiplication Proof cont
Using Trig ID in the Loooong Expression
So Finally
21212121 sincos jddww
21cos
212121212121 sincoscossinsinsincoscos jddww
21sin
1 ji
[email protected] • ENGR-25_Functions-1.ppt18
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
De Moivre’s Formulaz1 z2 = r1r2 (cos (1 + 2) + j sin(1 + 2))
z1 z2…zn = r1r2 …rn [cos (1 + 2 …+ n) + j sin(1 + 2 …+ n)]
zn = rn (cos (n) + j sin(n))z1 = z2=…= zn
r = 1 (cos + j sin )n = cos (n) + j sin(n)
French Mathematician Abraham de Moivre (1667-1754)
1 ji
[email protected] • ENGR-25_Functions-1.ppt19
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Complex Functions
f(x) = g(x) + j h(x)
A complex function
Real function
Real function
A complex conjugate functionf*(x) = g(x) – j h(x) f(x) f*(x) = g2(x) + h2(x)
Example: f(z) = z2 + 2z + 1; z = x + j y
f(z) = g(x,y) + j h(x,y)g(x,y) = (x2 – y2 + 2x + 1)h(x,y) = 2y (x + 1)
1 ji
[email protected] • ENGR-25_Functions-1.ppt20
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Verify by MuPad From the last Line
(collect comand) collect real and imaginary parts
12,
22,12, 22
xyyxhyxyyxhxyxyxg
[email protected] • ENGR-25_Functions-1.ppt21
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Euler’s Formula
A complex conjugate is also inverse
A power series for an exponential
!
u!
uueu
321
32
5!3!4!2!1
3!)(j
2!)(1
5342
32
θθθθθ
θjθjθe jθ
j
θ sin θez jθ j cos
jθu ei
Im(z)
Re(z)
1
1
–1
–1–
e–i
jθ-jθ
jθ-jθ
eej
θ
eeθ
21 sin
21 cos
θ sin θezz* -jθ j cos-1
1 ji
cos(θ) sin(θ)
[email protected] • ENGR-25_Functions-1.ppt22
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Complex Numbers – Engineering
[email protected] • ENGR-25_Functions-1.ppt23
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
)sin(cos
jrrejyxz j
jyxz
11 jjj Then from the Vector Plot
xy
yxr
1
22
tan
x
yr
z = x + j y
Im(z)
Re(z)
1 ji
[email protected] • ENGR-25_Functions-1.ppt24
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 n•m
j
j
Fejdcm
Aejban
Complex DIVISION is Painfully Tedious• See Next Slide
dbjcamndbjcamn
jjj AFeFeAemn
adbcjbdacbdjadbcjac
jdcjbamn2
1 ji
[email protected] • ENGR-25_Functions-1.ppt25
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
jdcjba
mn
The Exponential Form is Cleaner• See Next Slide
22
22
2
dcadbcjbdac
mn
dcddcjcbdjadbcjac
mn
jdcjdc
jdcjba
mn
1 ji
[email protected] • ENGR-25_Functions-1.ppt26
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
j
j
j
eFAFeAe
mn
Look for lotsof this inENGR43
1 ji
[email protected] • ENGR-25_Functions-1.ppt27
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Root of a Complex Number How to Find?
3 197 j
jrejyxz Use Euler
xy
yxr
1
22
tan
In This Case
Note that θ is in the 2nd Quadrant
Thus
2.1107
19tan
25.20197
1
22
r
3 2.110
3
25.20
197
je
j
[email protected] • ENGR-25_Functions-1.ppt28
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Root of A Complex Number Now use
Properites of Exponents
Use Euler in Reverse
74.36
32.110
31
2.11031
31
2.110
3 2.110
726.2
726.2
25.20
25.20
25.20
j
j
j
j
j
e
e
e
e
e sincos jrz
In this Case
631.11841.274.36sin74.36cos726.2
jjz
By MATLAB(-7+19j)^(1/3)ans = 2.1841 + 1.6305i
[email protected] • ENGR-25_Functions-1.ppt29
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
ln of a NEGATIVE Number (1) What is Im(z)
Re(z)
r
–r
–r = πz=(-19,0) 19ln
180
190arctan19019 22
p
r
State −19 as a complex no.
019 jz
Find Euler Reln Quantities r, & θ
[email protected] • ENGR-25_Functions-1.ppt30
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 = πz=(-19,0)
pjez
jz
19
019
Taking the ln
eje
ezj
j
ln9444.2ln19ln
19lnln
p
p
p
1416.39444.2ln jz
[email protected] • ENGR-25_Functions-1.ppt31
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 = πz=(-19,0)
pjez
jz
19
019
Taking the common (Base-10) log
ej
e
ezj
j
log2788.1log19log
19loglog
p
p
p
3644.12788.1
4343.02788.119loglog
jjez j
p
p
[email protected] • ENGR-25_Functions-1.ppt32
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
sin or cos of Complex number Recall from Euler Development
By Sum & Difference Formulas
sinh22
sin2
1sin
cosh22
cos2
cos
jeejeejjeej
eeeejee
jjjjjj
jjjjjj
jy
x
sinsincoscossincossinsincoscos
[email protected] • ENGR-25_Functions-1.ppt33
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
sin or cos of Complex number Thus
From PreviousSlide
So Finally
jyxjyxyx
jyxjyxjyxsincoscossinsin
sinsincoscoscos
yjjy
yjysinhsin
coshcos
yxjyxyx
yxjyxjyxsinhcoscoshsinsin
sinhsincoshcoscos
[email protected] • ENGR-25_Functions-1.ppt34
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*cac = -7.0000 -22.0000i
>> Mag_b = abs(b)Mag_b = 6.4031
>> c_star = conj(c)c_star = -5.0000 + 4.0000i
1 ji
[email protected] • ENGR-25_Functions-1.ppt35
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Complex Ops
>> b_d = b/db_d = 5.0000 + 4.0000i
>> c_b = c/bc_b = 0 + 1.0000i
>> c_a = c/ac_a = -1.7692 - 0.1538i
>> Re_d = real(d)Re_d = 0
>> Im_b = imag(b)Im_b = 5
[email protected] • ENGR-25_Functions-1.ppt36
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Complex Ops
>> b_sq = b^2b_sq = -9.0000 -40.0000i
>> b_cu = b^3b_cu = 2.3600e+002 +1.1500e+002i
>> cos_a = cos(a)cos_a = -3.7245 - 0.5118i
>> exp_c = exp(c)exp_c = -0.0044 + 0.0051i
>> log_b = log10(b)log_b = 0.8064 + 0.9752i
[email protected] • ENGR-25_Functions-1.ppt37
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Complex Ops>> r = 73;>> theta = 2*pi/11;>> theta_deg = 180*theta/pitheta_deg = 32.7273
>> z = r*exp(j*theta)z = 61.4115 +39.4668i
>> abs(z)ans = 73
>> 180*angle(z)/pians = 32.7273
>> x = -23; y = 19;>> z2 = complex(x,y)z2 = -23.0000 +19.0000i
1 ji
[email protected] • ENGR-25_Functions-1.ppt38
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Caveat MATLAB Accepts>> z2 = 3+7j;>> z3 = 5 + i*11;>> z4 = 7 + 13*j;>> z2z2 = 3.0000 + 7.0000i>> z3z3 = 5.0000 +11.0000i>> z4z4 = 7.0000 +13.0000i
But NOT>> z5 = 7 + j5;??? Undefined function or variable 'j5'.
1 ji
[email protected] • ENGR-25_Functions-1.ppt39
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
All Done for Today
LeonhardEuler
(1707-1783)
[email protected] • ENGR-25_Functions-1.ppt40
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Bruce Mayer, PELicensed Electrical & Mechanical Engineer
Engr/Math/Physics 25
Appendix
6972 23 xxxxf
[email protected] • ENGR-25_Functions-1.ppt41
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
[email protected] • ENGR-25_Functions-1.ppt42
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Complex Integration Example
I = xbxeax dcos I = Re xee ibxax d I = Re xe ib)x(a d
22
cos sinsin cos
sin cos
babx b bx aibx b bx ae
ibabx i bxe
ibaexe
ax
axib)x(a
ib)x(a d
22sin coscos
babx b bx aexbxe axax
d
22cos sinsin
babx b bx aexbxe axax
d
1 ji
Us EULER to Faciltitate (Nasty) AntiDerivation
[email protected] • ENGR-25_Functions-1.ppt43
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Illuminate Previous Slide By EULER
xbxibxexeeI axibxaxC dsincosd
Using Term-by-Term Integration xbxeixbxexbxiexbxeI axaxaxax
C dsindcosdsindcos
• As “i” is just a CONSTANT Taking the REAL Part of the above
xbxeixbxeII axaxC dsindcosReRe
Cibxaxax IxeexbxeI RedRedcos
[email protected] • ENGR-25_Functions-1.ppt44
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Rotation OperatorPolar form of a complex number
z = r (cos + j sin jθe r αθjjθjαjα eeezez' r r
z(x,y)
Im(z)
Re(z)
r
–r
–rz'(x',y')
α)(θ sin α)cos(θr
θ sin cosθr
j y'jx'z'j yjx z
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 ej 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') .
1 ji
[email protected] • ENGR-25_Functions-1.ppt45
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Periodicity
z' = 2πjjθeez' = 2πθjez = jθe
zej e jθjθ sin2πcos2πz'
2, 1, 0,2π nee jθnθj ,
The function ej occurs in the natural sciences whenever periodic motion is described or when a system has periodic structure.
1 ji
[email protected] • ENGR-25_Functions-1.ppt46
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Periodicity on a Circleor 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 1 = zk
3
z0 = ej0 = 1 z1 = ej 2p/3 = - + j
z2 = ej 4p/3 = - – j
21
21
23
23 zk = ej 2pk/3, where k = 0, ±1
3 roots of third degree of 1
1 ji
[email protected] • ENGR-25_Functions-1.ppt47
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Periodicity on a Circle contor n nth roots of 1
z0 = ej0 = 1 z±1 = e±j p/3 = ± j
z±2 = e ±j 2p/3 = - ± j
21
21
23
23
6 sixth roots of 1
z0
Im(z)
Re(z)
z1z2
z3
z-2 z-1
Used for description of the properties of the benzene molecule.
n nth roots of 1
zk = ej 2pk/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.
1 ji
[email protected] • ENGR-25_Functions-1.ppt48
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.
... a a a a a
f(x) = f(x + a) f(x) = x/aje 2π
Periodic function
Generalization for three-dimensional periodic systems
f(x,y,z) = z/cjy/bjx/aj eee 2π2π2π
1 ji
[email protected] • ENGR-25_Functions-1.ppt49
Bruce Mayer, PE Engineering/Math/Physics 25: Computational Methods
Trig on complex numbers