Prescott, Arizona Campus Department of Electrical and Computer Engineering EE 402 Control Systems Laboratory Spring Semester 2016 Lab Section 01 Thursday 1:25 – 4:05 pm King Eng. Bldg. Rm 122 Lab Instructor: Dr. Stephen Bruder Lab 01 – Pre-lab Control Systems Modeling & Analysis with MATLAB Date Experiment Performed: Thursday, Jan 21, 2016 Instructor’s Comments: Comment #1 Comment #2 Date Report Submitted: Monday, Jan 25 Group Members: Student # 1 Name & Email Student # 2 Name & Email Grade: EE 402 Control Systems Lab Spring 2016 1. BASIC MATLAB FUNCTIONALITY In the prelab, we will preview some of the basic functionality of MATLAB. In particular, you can enter “>> help topic” or “>> doc topic” or click on the icon to get more information. You can enter MATLAB commands in the command window for an interactive session or create scripts to be saved as *.m files. To create a *.m file, type “>> edit filename.m” in the command window, or select File New Script from the pull-down menu, or select the icon. 1.a. Built in Capabilities: Some particularly useful built-in variables are: Both i and j ( 1 ) can be used to indicate complex numbers >> 2 + sqrt(-1) + 2i +3j ans = 2.0000 + 6.0000i pi has the value 3.1415926… Building matrices/vectors >> row = [1 2 5.4 -6.6] row = 1.0000 2.0000 5.4000 -6.6000 >> column = [4; 2; 7; 4] column = 4 2 7 4 >> size(row) ans = 1 4 >> size(column) ans = 4 1 >> x = 1:4 x= 1 2 3 4 Some of the particularly relevant built-in MATLAB functions are listed in Table 1 below. Names of Students in the Group Page 2 of 6 EE 402 Control Systems Lab Spring 2016 Table 1 Some Common MATLAB Functions Description Function abs(x) sqrt(x) round(x) fix(x) floor(x) ceil(x) sign(x) rem(x, y) exp(x) log(x) Computes the absolute value of x Computes the positive square root of x Rounds x to the nearest integer Rounds x to the nearest integer towards 0 Rounds x to the nearest integer toward - ∞ Rounds x to the nearest integer toward + ∞ Returns: -1 if x < 0, 0 if x = 0, and 1 otherwise Returns the remainder of x/y (i.e. modulus function) log10(x) Computes x Computes e , where e is the base of the natural log Computes the natural log of x to base e sin(x), cos(x), tan(x) asin(x), acos(x), atan(x) >> log10(100) = 2 Computes the sine, cosine, & tangent of the angle x, where x is given in radians Computes the arcsine, arccosine, & arctangent of x returning an angle in radians Computes the arctangent (or inverse tangent) of atan2(y, x) y x retuning an angle in radians Returns the complex conjugate of the complex number x Returns the real part of the complex number x Returns the imaginary part of the complex number x Returns the angle =atan2(imag(x), real(x)) conj(x) real(x) imag(x) angle(x) 1.b. log10 ( x) ( i.e. Example >> abs(-2.1) = 2.1000 >> sqrt(2.1) = 1.4491 >> round(2.1) = 2 >> fix(-2.9) =-2 >> floor(-2.9) = -3 >> ceil(-2.9) = -2 >> sign(2.1) = 1 >> rem(17, 4) = 1 >> exp(1) = 2.7183 >> log(exp(1)) = 1 x r e j r cos( ) j sin( ) ) >> cos(60*pi/180) = 0.5000 >> acos(0.5)*180/pi = 60.0000 >> atan2(1,-1)*180/pi = 135 >> conj(2+3*j) = 2.000 - 3.000i >> real(2 + 3*j) = 2 >> imag(2 + 3*j) = 3 angle(1 + j)*180/pi = 45 Practice: 1. Compute the following using the MATLAB command window (2 decimal digits please): 17 15 5 log e ln e 5 1 2 13 2 7 3 10 4 121 11 = 2. Write a MATLAB script to compute: x for tan( ) sin( ) log10 a 5 b 2 cos( ) a. / 4 , a 5 , and b 3 => x = b. / 3 , a 2 , and b 3 => x = 1.c. Working with Polynomials To build the polynomial p(s) s3 2s 5 in MATLAB, enter >> p = [1 0 -2 -5]; Names of Students in the Group Page 3 of 6 EE 402 Control Systems Lab Spring 2016 Note that the coefficient of the highest power is first in the list and all coefficients (i.e., the 0s 2 term) must be entered!! To solve for the roots of the polynomial use >> r = roots(p) r= 2.0946 -1.0473 + 1.1359i -1.0473 - 1.1359i To recover the polynomial from its roots >> poly([2.0946, -1.0473 + 1.1359i, -1.0473 - 1.1359i]) ans = 1.0000 0 -2.0002 -5.0000 i.e., p(s) s3 2s 5 s 2.0946 s 1.0473 1.1359i s 1.0473 - 1.1359i 2. USING THE SYMBOLIC TOOLBOX The symbolic toolbox can be used to perform algebraic computations and render a symbolic (i.e., not only numeric) result. A particularly useful tool is the Laplace transform (and Inverse Laplace transform) functions. For example, if we had modeled a simple system and performed the forward LT to determine the system’s transfer function as G ( s ) b1s b0 a2 s a1s a0 2 (see Figure 1) and now wanted to determine the F (s) Input Y (s) Output systems output response due to a unit step input, we could use the Symbolic toolbox to compute the Figure 1 A simple system model result. >> syms s >> a2 = 1; a1 = 5; a0 = 6; % Values for the denominator coeffs >> b1 = 2; b0 = 5; % Values for the numerator coeffs >> Gs = (b1*s + b0)/(a2*s^2+a1*s+a0); % G(s) = The Transfer Function >> Ys = (1/s)*Gs; % Y(s) = F(s) G(s) >> yt = ilaplace(Ys) % y(t) = InverseLT(Y(s)) yt = 5/6 - exp(-3*t)/3 - exp(-2*t)/2 >> ezplot(yt, [0 5 0 1]) % Plot a symbolic function The last line of code returns the plot. Past a copy of your plot in Figure 2 (below). Names of Students in the Group Page 4 of 6 EE 402 Control Systems Lab Spring 2016 Figure 2 A plot of y (t ) vs time for t=0,…, 5 seconds 3. USING SIMULINK The previous example could also be analyzed using the Simulink graphical simulation tool. 1. Start MATLAB and enter >> simulink 2. Select File New Model 3. Select “Continuous” and drag a “Transfer Fcn” to your blank model window. 4. Select “Sources” and drag a “Step” to your model window. 5. Select “Sinks” and drag a “Scope” to your model window. 6. Connect the “Step” to the “Transfer Fcn” and “Transfer Fcn” to the “Scope.” 7. Double click on the “Step” and enter a “Step Time” of 0. Names of Students in the Group Page 5 of 6 EE 402 Control Systems Lab Spring 2016 8. Double click on the “Transfer Fcn” and set the “Numerator coefficients” to [b1 b0] and “Denominator coefficients” [a2 a1 a0]. 9. Set the simulation time to 5.0 sec. 10. Save the model as “first.mdl”. 11. In the command window enter >> a2 = 1; a1 = 5; a0 = 6; >> b1 = 2; b0 = 5; 12. Click on the play button (). 13. Double click on the “Scope” to view the results (you might want to autoscale the plot). Past a copy of your plot in Figure 3 (below). Figure 3 A plot of y (t ) vs time from Simulink modeling QUESTION: How does Figure 2 compare with Figure 3? Names of Students in the Group Page 6 of 6