310 likes | 526 Views
EE3417 Lab Session. Week 1 Md Ahsan Habib mdahsan.habib@mavs.uta.edu. 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: mdahsan.habib@mavs.uta.edu. MATLAB Overview.
E N D
EE3417 Lab Session Week 1 MdAhsanHabib mdahsan.habib@mavs.uta.edu
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: mdahsan.habib@mavs.uta.edu
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 • >> clear a • clc→ 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: • ejθ=cos θ + jsin θ, z = r ejθ • z = |z|ej∠z • |z| = r = √(a2+ b2) • ∠z = θ = tan-1(b/a), π≥ θ ≥-π • Conjugate of z, z* = a – jb = r e-jθ= |z|e-j∠z • zz* = (a+jb)(a-jb) = a2 + b2 = |z|2 • Useful Identities (7) • e∓jnπ= -1, n → odd integer ⇨e∓j(2n+1)π= -1, n → integer • e∓j2nπ= 1, n → integer
Complex Number – A common mistake z1 = a +jb θ1 ∠ z1 = tan-1(b/a) = θ1 ∠ z2= tan-1(-b/-a) = θ2 ∠ z2≠ ∠ z1 ∠ z2 = θ2 =θ1 - 180 θ2 z2=-a -jb z1 = -a +jb θ1 ∠ z1 = tan-1(b/-a) = θ1 ∠ z2= tan-1(-b/a) = θ2 ∠ z1 ≠ ∠ z2 ∠ z1 = θ1 =180 + θ2 θ2 z2=a -jb Example B.1 (9)
Complex Number - MATLAB • Matlab predefines i = j = • >> z = -3-j4 • real 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 number polar form to Cartesian form • z = 4 e-j(3π/4) • >> [z_real, z_imag] = pol2cart(-3*pi/4,4)
Complex Number -Exercise • Determine z1z2 and z1/z2 if z1 = 3+j4 and z2 = 2+3j • >> Verify your results using MATLAB • Convert your results from Cartesian coordinate to Polar coordinate • >> Verify your results using MATLAB function pol2cart (13)
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; • >> plot (t,f,’-k’,t,h,’-b’); • >> xlabel(‘t’); ylabel(‘Amplitude’); • >> legend (‘f(t)’,’h(t)); h 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)
Matrix Operation , Solve • Ax = y; • x = A-1Ax = A-1y • >> 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); • fplot(sqr,[-10 20 -1 20]); • Unit Step Function • step = @(x)(x>=0); • fplot(step,[-1 5 -1 5]); • step = @(x)(x-2>=0); • fplot(step,[-1 5 -1 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 • >> f1 = @(x)(2*x)*and((x<=10),(x>=5)); • >> fplot(f1,[-10 20 -10 30]); 20 10 5 10
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+an-1 λn-1+…+a1 λ+a0 λ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 satisfies eq(i) Example B.13 (47)
Eigenvalue and Eigenvector Applications • 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. Tacoma Narrow Bridge collapsing • 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 EigenvectorMATLAB • 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<n • An improper function can always be separated into sum of a polynomial in x and a proper function. • A proper function can be further expanded into partial fraction
Partial Fraction Expansion • Method of Clearing Fraction • The Heaviside “Cover-up” Method • 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.8 (27) Example B.9 (29) 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 5.9713 3.1107 0.1112 P = 1.4142 1.4142 1.4142 -1.4142 K = 1.0000 2.8284
Partial Fraction ExpansionExercise • 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 Nth root of -1. Here, N is the input • Write a script that will • vary N from 1:5 • call N_root each time to find Nth roots • display the discrete Nth 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 Nth root of -1. Here, N is the input • Write a script that will • vary N from 1:5 • call N_root each time to find Nth roots • display the discrete Nth roots