EE3417 Lab Session Week 1 • Turn on your computer. Open MATLAB • The slides are uploaded in Blackboard • Quiz on June.
Download ReportTranscript EE3417 Lab Session Week 1 • Turn on your computer. Open MATLAB • The slides are uploaded in Blackboard • Quiz on June.
EE3417 Lab Session
Week 1
• Turn on your computer. Open MATLAB • The slides are uploaded in Blackboard • Quiz on June 12 (Week 2) • Syllabus: Week 1, 2 • My TA hours • • Email: [email protected]
MATLAB Overview
• • • • When MATLAB launched, Command Window Appears Command prompt(>>) in Command Windows to accept instruction or input Objects → Data Objects are placed in MATLAB workspace • >> a = 4; b = 3+2j; c = a * b; whos → Another way to view workspace • >> whos who → short version of whos -> reports only the names of workspace objects • >> who clear a → remove specific variables (a) from the workspace • clc >> clear a → clears the command window • >> clc help -> most important and useful command for new users • >> help whos exit -> terminates MATLAB
Algebra of Complex Number
• • • • • • Complex number: z = a + jb • Re z = a; Im z = b Complex number expressed in polar coordinates (r,θ) • a = rcos θ, b = rsin θ, z = r(cos θ+ jsin θ) Euler Formulae: • e jθ =cos θ + jsin θ, z = r e jθ z = |z|e j ∠z • • |z| = r = √(a 2 + b 2 ) ∠z = θ = tan -1 (b/a), π≥ θ ≥-π Conjugate of z, z* = a – jb = r e -jθ = |z|e -j ∠z • zz* = (a+jb)(a-jb) = a 2 + b 2 = |z| 2 Useful Identities • e ∓ jnπ • e ∓ j2nπ (7) = -1, n → odd integer = 1, n → integer ⇨ e ∓ j(2n+1)π = -1, n → integer
Complex Number – A common mistake
z 1 = a +jb θ 1 z 2 =-a -jb z 1 = -a +jb θ 2 ∠ ∠ ∠ ∠ z 1 = tan-1(b/a) = θ 1 z 2 = tan-1(-b/-a) = θ 2 z 2 ≠ ∠ z 1 z 2 = θ 2 = θ 1 - 180 θ 1 θ 2 z 2 =a -jb ∠ ∠ ∠ ∠ z 1 = tan-1(b/-a) = θ 1 z 2 = tan-1(-b/a) = θ 2 z 1 ≠ ∠ z 2 z 1 = θ 1 = 180 + θ 2 Example B.1
(9)
Complex Number - MATLAB
• Matlab predefines i = j = • real >> z = -3-j4 and imag operators extract real and imaginary components of z.
>> z_real = real(z) >> z_imag = imag(z) • Modulus or Magnitude of a complex number • • >> z_mag = sqrt(z_real^2+z_imag^2) |z| 2 = zz* >> z_mag = sqrt(z*conj(z)) >> z_mag = abs(z) • Angle of a complex number • >> z_rad = atan2(z_mag, z_real) atan2 -> two-argument arc-tangent function; ensures the angle reflects in the proper quadrant.
>> z_rad = angle(z) • MATLAB function pol2cart • z = 4 e -j(3π/4) number polar form to Cartesian form >> [z_real, z_imag] = pol2cart(-3*pi/4,4)
Complex Number - Exercise
• • Determine z 1 z 2 and z 1 /z 2 if z 1 = 3+j4 and z 2 >> Verify your results using MATLAB = 2+3j (13) Convert your results from Cartesian coordinate to Polar coordinate >> Verify your results using MATLAB function pol2cart
MATLAB - Vector Operation
• Vectors of even valued integers • >> k = 0:2:11 Negative and noninteger step sizes • >> k = 11:-10/3:0 If step size not speified, value of one assumed • >> k = 0:11 In MATLAB, ascending positive integer indices specify particular vector elements.
• >> k(5), k(1:4), Vector representation to create signals • 10 Hz sinusoid described by f(t) = sin(2π10t+π/6) when 0≤t<0.2
• >> t = 0:0.0004:0.2-0.0004; f = sin(2*pi*10*t+pi/6); f(1) Find the three cube roots of minus 1, • → >> k = 0:2; >> w = exp(j*(pi/3 + 2*pi*k/3)) • Exercise (56) Find the 100 cube roots of minus 1?
Simple Plotting
• • • MATLAB’s plot command >> plot(t,f); Axis labels are added using xlabel and ylabel >> xlabel(‘t’); ylabel(‘f(t)’) Plotting discrete points, 100 unique roots of w^100=-1 >> plot(real(w), imag(w), ‘o’); >> xlabel(‘Re(w)’); ylabel(‘Im(w)’); >> axis equal
Element by Element Operations
• • Multiplication, Division and Power x = [5 4 6]; y = [1 2 3]; >> x_mul_y = x * y >> x_elem_mul_y = x.*y >> x_div_y = x/y >> x_elem_div_y = x./y >> x_elem_pow_y = x.^y • Suppose h(t) = f(t)g(t) where g(t) = exp(-10*t) >> g = exp(-10*t); >> h = f.*g; h >> plot (t,f,’-k’,t,h,’-b’); >> xlabel(‘t’); ylabel(‘Amplitude’); >> legend (‘f(t)’,’h(t));
g(t)
Damped Sinusoid
Matrix Operation
• Common Useful function • eye(m) creates the m×m identity matrix >> eye(3) • ones(m,n) creates the m×n matrix of all ones >> ones(2,3) • zeros(m,n) creates the m×n matrix of all zeros >> zeros(3,2) • Row vector >> r = [1 3 2]; • A 2×3 matrix >> A = [2 3; 4 5; 0 6];
Matrix Operation
• • • Transpose >> c= r’; Concatenation >> B = [c A]; Matrix inverse >> D = inv(B); • • Matrix indices >> B(1,2) >> B(1:2,2:3) Colon can be used to specify all elements along a specified dimension >> B(2,:) >> B(:,2)
,
Solve Matrix Operation
• • Ax = y; x = A -1 Ax = A -1 y >> A = [1 -2 -3; -sqrt(3) 1 –sqrt(5); 3 –sqrt(7) 1]; >> y = [1; pi; exp(1)]; >> x = inv(A)*y
function handle
• • handle = @(arglist)anonymous_function constructs an anonymous function and returns a • • handle to that function.
arglist is a comma-separated list of input arguments.
The statement below creates an anonymous function that finds the square of a number.
• To execute the function associated with it • fhandle(arg1, arg2, ..., argN) >> sqr = @(x) x.^2; >> a = sqr(5);
function handle
• fplot function → Plot function between specified limits • fplot(fun,limits); 5 • fplot(sqr,[-10 20 -1 20]); 4 3 • Unit Step Function • step = @(x)(x>=0); • fplot(step,[-1 5 -1 5]); • step = @(x)(x-2>=0); • fplot(step,[-1 5 -1 5]); 2 1 0 -1 -1 5 0 1 2 3 4 5 4 3 2 1 0 -1 -1 0 1 2 3 4 5
Functions
• Unit Pulse Function >> u = @(x)and((x<=3),(x>=1)); >> fplot(u,[-5 5 -1 3],10000); >> u = @(x)and(((x+2)<=3),((x+2)>=1)); >> figure(); fplot(u,[-5 5 -1 3],10000); >> u = @(x)and(((2*x)<=3),((2*x)>=1)); >> fplot(u,[-5 5 -1 3],10000); • Piecewise function 20 10 5 10 >> f1 = @(x)(2*x)*and((x<=10),(x>=5)); >> fplot(f1,[-10 20 -10 30]); 2 1.5
1 0.5
1.5
1 0.5
0 -0.5
-1 -5 3 2.5
3 2.5
2 1.5
1 0.5
0 -0.5
2.5
2 3 -1 -5 -4 -3 -2 -1 -4 -3 -2 -1 0 -0.5
-1 -5 -4 -3 -2 -1 0 1 2 0 0 1 1 2 2 3 3 3 4 4 4 5 5 5
Eigenvalue and Eigenvector
For an (n×n) square matrix A, and vector x (x≠0) that satisfy the eq Ax = λx ----- eq(i) is an eigenvector and λ is the corresponding eigenvalue of A • • Q(λ) = |λI – A| = λ n +a n-1 satisfies eq(i) λ n-1 +…+a 1 λ+a 0 λ 0 = 0 Q(λ)→ characteristic polynomial of matrix A.
• The n zeros of the polynomial are the eigenvalues of A.
• Corresponding to each eigenvalue, there is an eigenvector that Example B.13
(47)
Eigenvalue and Eigenvector Applications
Tacoma Narrow Bridge collapsing • Constructed at 1940s.
• Crashed by winds which set bridge oscillated at a frequency closed to its own natural frequency.
• Natural frequency of the bridge is the eigenvalue of smallest magnitude of a system that models the bridge.
• The eigenvalue of smallest magnitude of a matrix is the same as the inverse (reciprocal) of the dominant eigenvalue of the inverse of the matrix.
• Eigenvalues can also be used to test for cracks or deformities in a solid.
• Car designers analyze eigenvalues in order to damp out the noise so that the occupants have a quiet ride.
Eigenvalue and Eigenvector MATLAB
• poly(A) to determine Characteristic Polynomial of matrix A (n×n) • Output is a row vector with n+1 elements that are the coefficients of the characteristic polynomial • >> A = [3 2 -2; -3 -1 3; 1 2 0]; >> poly(A) roots(C) computes the roots of the polynomial whose coefficients are the elements of the vector C.
>> roots(poly(A)) • The command in the form [V D] = eig(A) computes both the eigenvalues and eigenvectors of A. >> [V D] = eig(A)
Partial Fraction Expansion
• Rational function F(x) can be expressed as = P(x)/Q(x) • The function F(x) is improper if m≥n and proper if m
• A proper function can be further expanded into partial fraction
Partial Fraction Expansion
• Method of Clearing Fraction • Example B.8
(27) • The Heaviside “Cover-up” Method • Example B.9
(29) • Repeated Factors of Q(x) • If a function F(x) has a repeated factor in its denominator, Its partial expansion is given by • Example B.10
(33)
Partial Fraction Expansions
• • Partial fraction expansion of rational function F(x) = B(x)/A(x) MATLAB residue command. The basic form: • • • >> [R,P,K] = residue(B,A); B → Polynomial coefficient of the numerator A → Polynomial coefficient of the denominator This vectors are ordered in descending powers of the • • • independent variable R → coefficient of each partial fraction P → contain the corresponding roots of each partial fraction For a root repeated r times, the r partial fractions are ordered • • in ascending powers.
K → the direct terms, when the ration function is not proper They are ordered in descending powers of independent variable
Partial Fraction Expansions
Solve (64) >> [R,P,K] = residue([1 0 0 0 0 pi],[1 -sqrt(8) 0 sqrt(32) -4]) R = 7.8888
P = 1.4142
K = 1.0000
5.9713
1.4142
2.8284
3.1107
1.4142
0.1112
-1.4142
Partial Fraction Expansion Exercise
• Compute by hand the partial fraction expansion of • Verify your solution using MATLAB
MATLAB SCRIPT
• The command window is to design and execute individual command one at a time >> x = 1:10; >> y=log(x) >> plot(x,y) • To automate the execution of many commands - matlab program/script • MATLAB program - collection of MATLAB command and function stored in disk as a text file of type .m
• MATLAB editor – To run F5 • FOR loop clear all; x = (1:1000)'; for k = 1:5 Y(:,k)=k*log(x); end plot(x,y)
MATLAB FUNCTION
• Syntax function [y1,...,yN] = myfun(x1,...,xM) • Open a new script file and save it as eval_log_func.m
• Write a function that output multiple log functions function eval_log_func(maxLoop) x = (1:1000)' for k = 1:maxLoop y(:,k)=k*log(x); end plot(x,y) • In Command window >> clear all; >> g = eval_log_func(10);
MATLAB Function - Exercise
• Write a function (N_root.m) that will calculate the N th Here, N is the input root of -1. • Write a script that will • vary N from 1:5 • call N_root each time to find N th • display the discrete N th roots roots
Integral
• syms : shortcut for creating symbolic variables and • functions • Syntax: syms var1 ... varN >> syms x y Symbolic Integration • • Syntax: int(expr,var) computes the indefinite integral of expr with • • respect to var Syntax : int(expr, var, a, b) computes the definite integral of expr with • respect to var from a to b.
a and b: Number or symbolic expression, including expressions with infinities.
Integral
>> syms x; >> int(-2*x/(1 + x^2)^2,x) • >> int(x*log(1 + x), 0, 1) • >> int((6*exp(-(x-2)))^2,2,Inf);
MATLAB FUNCTION
• Syntax function [y1,...,yN] = myfun(x1,...,xM) • Open a new script file and save it as eval_log_func.m
• Write a function that output multiple log functions function eval_log_func(maxLoop) x = (1:1000)' for k = 1:maxLoop y(:,k)=k*log(x); end plot(x,y) • In Command window >> clear all; >> g = eval_log_func(10);
MATLAB Function - Exercise
• Write a function (N_root.m) that will calculate the N th Here, N is the input root of -1. • Write a script that will • vary N from 1:5 • call N_root each time to find N th • display the discrete N th roots roots