EE 4314 Lab 1 Handout Control Systems Simulation with MATLAB and SIMULINK Spring 2015 Lab Information This is a take-home lab assignment. There is no experiment for this lab. You will study the tutorial in the next section and do the examples to learn the basics of MATLAB and Simulink for control systems simulation. You will need use to MATLAB, Simulink, and the control systems toolbox which are available in some of the OIT managed computer labs of UTA. Also, you can use your own installation of MATLAB. After studying the examples, you will work on the problems in the Lab Report section on your own and are required to submit a report by uploading it via EE4314 Blackboard. Please check the information about the assignment policy in the Lab Report section. 1.1 Matlab Basics The main elements of the interface are: the command window the workspace window (variable window) the command history window the file information window the start button 1.2 Basic Commands - who - list current variables. who lists the variables in the current workspace. whos - is a long form of WHO. It lists all the variables in the current workspace, together with information about their size, bytes, class, etc. help – display help text in command window. clc - clears the command window and homes the cursor. clf – clears current figure. clear - clears variables and functions from memory. clear all - removes all variables, globals, functions. save – saves workspace variables to disk. Use “help save” for details. load – loads workspace variables from disk. - plot – PLOT(X,Y) plots vector Y versus vector X. If X or Y is a matrix, then the vector is plotted versus the rows or columns of the matrix, whichever line up. 1.3 Matlab Variables In general, the matrix is defined in the Matlab command interface by input from the keyboard and assigned a freely chosen variable name. >> x = 1.00 A 1x1 matrix will be assigned to the variable x. >> y = [ 1 5 3 ] A row vector of length 3 will be defined. >> z = [ 3 1 2; 4 0 5]; A 2x3 matrix will be defined. An element of the matrix can be accessed by using the index (), for example: >> z(2,3) ans = 5 : can be used as an index to refer to the whole row or column, for example: >> z(:,1) ans = 3 4 Example 1: Simple 2D Plot PLOT(X,Y) plots vector Y versus vector X. If X or Y is a matrix, then the vector is plotted versus the rows or columns of the matrix, whichever line up. >> x = 0 : 1 : 10; >> y = -1: 1 : 9; >> plot(x,y) As a result we will get (a vector of length 11 will be defined) (another vector of length 11 will be defined) >> >> >> >> >> grid on; xlabel('Time (sec)'); ylabel('Distance (meter)'); legend('Distance from point A to B'); title('Distance Plot'); The result will be List of useful plot commands include: xlim - gets the x limits of the current axes. ylim - gets the y limits of the current axes. semilogx() - is the same as plot(), except a logarithmic (base 10) scale is used for the X-axis. semilogy() - is the same as plot(), except a logarithmic scale is used for the Yaxis. plot3(x,y,z) – similarly to plot(), plot3() plots a line in 3-space through the points whose coordinates are the elements of x, y and z. Example 2: 3D Plot >> >> >> >> x = -1 : 0.1 : 1; y = -1 : 0.1 : 1; Z = exp(-1*(x'*y)); mesh(x,y,Z) As a result we will get >> contour(x,y,Z) As a result we will get (a vector of length 21) (Z is a 21x21 matrix) Contour(Z) is a contour plot of matrix Z treating the values in Z as heights above a plane. 1.4 Vector Operations There are 2 types of vector operations in Matlab 1) Element by element operations .+ element by element addition operation .- element by element subtraction operation .* element by element multiplication operation .^ element by element power operation ./ element by element division operation 2) Matrix operations ' matrix transpose * matrix multiply ^ matrix power eye(N) is the N-by-N identity matrix. inv(X) is the inverse of the square matrix X. det(X) is the determinant of the square matrix X. trace(X) is the sum of the diagonal elements of X, which is also the sum of the eigenvalues of X. Example 3: Matrix Operations >> X = eye(2) X = 1 0 0 1 >> Y = [ 1 3 ; 2 1] Y = 1 3 2 1 >> Z = [ 3 2; 1 4] Z = 3 2 1 4 Element by element multiplication VS matrix multiplication >> Y.*Z ans = 3 2 6 4 >> Y*Z ans = 6 7 14 8 Element by element power VS matrix power >> Z^2 ans = 11 7 >> Z.^2 ans = 9 1 14 18 4 16 Inverse, transpose and multiplication example >> inv(Z) ans = 0.4000 -0.1000 >> inv(Z)*Y' ans = -0.2000 0.8000 -0.2000 0.3000 0.6000 0.1000 1.5 Complex Numbers An imaginary can be defined using the i , j. >> a = 2 + i a = 2.0000 + 1.0000i Useful commands for complex number operations include: - real(X) is the real part of X. imag(X) is the imaginary part of X. conj(X) is the complex conjugate of X. abs(X) is the absolute value of the elements of X. When X is complex, abs(X) is the complex modulus (magnitude) of the elements of X. angle(X) returns the phase angles, in radians, of a matrix with complex elements. cart2pol(X,Y) transforms corresponding elements of data stored in Cartesian coordinates X,Y to polar coordinates (angle TH and radius R). pol2cart(TH,R) transforms corresponding elements of data stored in polar coordinates (angle TH, radius R) to Cartesian coordinates X,Y. Example 4: Imaginary Number Operations >> real(a) ans = 2 >> imag(a) ans = 1 >> conj(a) ans = 2.0000 - 1.0000i >> abs(a) ans = 2.2361 >> angle(a) ans = 0.4636 >> [th,r] = cart2pol(real(a), imag(a)) th = 0.4636 r = 2.2361 >> [x, y] = pol2cart(th, r) x = 2 y = 1 2.1 Linear Systems / Control Systems Toolbox At the MATLAB command prompt, type “help control” to learn about the control systems toolbox. Some useful commands include: - tf(num, den) - creates a continuous-time transfer function with numerator(s) num and denominator(s) den. ss(a,b,c,d) - creates the continuous-time state-space model impulse() - calculates the unit impulse response of a linear system. step() - calculates the unit step response of a linear system. lsim() - simulates the time response of continuous or discrete linear systems to arbitrary inputs. residue(b,a) - finds the residues, poles, and direct term of a partial fraction expansion of the ratio of two polynomials, b(s) and a(s). ode23() - solves initial value problems for ordinary differential equations. Dsolve () – solves differential equations symbolically. Example 5: Time responses of a linear system Create a system with transfer function Gs >> Num = [1 0]; >> Den = [1 2 10]; >> sys = tf(Num,Den) Transfer function: s -------------s^2 + 2 s + 10 Simulate an impulse response of the Gs >> impulse(sys) The result is: s s 2s 10 2 Simulate a step response of the same system >> step(sys) The result is Now, lets simulate the time response of the above system using different input function. >> t = 0 : 0.1 : 10; >> u = sin(2.*t); >> lsim(sys,u,t) Example 6: Partial Fraction Expansion Suppose that we want to write Gs Gs s in pole-residue form of s 2s 10 2 rn r1 r2 k s . We can use residue() command to solve the s p1 s p2 s pn problem. >> Num = [1 0]; >> Den = [1 2 10]; >> [r,p,k] = residue(Num, Den) r = 0.5000 + 0.1667i 0.5000 - 0.1667i p = -1.0000 + 3.0000i -1.0000 - 3.0000i k = [] It means that we can now rewrite G(s) as Gs 0.5 0.1667i 0.5 0.1667i . s 1 3i s 1 3i Example 7: First Order ODE Solver Given x 2x 0 , plot the time response of x(t) from t = 0 – 10 from x(0) = 5. We will first open up m-file editor to create the function. First, click on the “New” button and type the following function code. function dx = myfunction(t,x) dx = -2*x; Then, save the function as myfunction.m in your current working directory. >> >> >> >> >> >> [t,x] = ode23(@myfunction, [0 10], 5); plot(t,x) grid on xlabel('time (seconde)'); ylabel('Amplitude'); title('Simple ODE Solver'); The result is: 2.2 Simulink Simulink® is an environment for multidomain simulation and Model-Based Design for dynamic and embedded systems. Simulink programming consists of manipulating or connecting block diagrams in a graphical user interface environment, and therefore it is more intuitive and descriptive than the conventional script-based environment of MATLAB. Simulink can be used to simulate very complex dynamical systems and evaluate the performance of controllers for such systems. Go to MATLAB Help and find simulink examples in the Demos tab. Take some time go over general demos. Example 8: Transfer Function Simulation using Simulink Create a simulink model of Gs s and then simulate and plot a step response s 2s 10 2 of this transfer function. Step1: Launch Simulink by typing simulink in the command window. Then, open a model. new blank simulink Step2: Go to >>Simulink>>Continuous, then drag the transfer function block from the panel and drop it onto the blank model window. Step3: Double-click the transfer function block to edit the parameters. Enter numerator coefficient and denominator coefficient as show in the picture below. Step4: Go to >>Simulink>>Sources, then drag the “Step” block onto the model window. Step5: Go to >>Simulink>>Sinks, then drag the “Scope” block onto the model window. Step6: Connect the terminals as show in the picture below. Step6: Run the simulation, by pressing the play button or go to >>Simulation>>Start. Step7: Double-click the “Scope” block to view the simulation result (Press the autoscale button on the scope window if needed). Step8: Press the auto-scale button (binocular) on the scope window. The picture below is the step response of the system. Example 9: Simulating an ODE Model using Simulink We will create a Simulink model for x 1.4x x f t using continuous blocks. And we will use a user defined function block to handle arbitrary f(t). In this example, we will also demonstrate how to send Simulink variables to workspace. Step1: Open a new blank simulink model. Step2: Go to >>Simulink>>Continuous, then drag the 2 integrator blocks from the panel and drop it onto the blank model window. Step3: Go to >>Simulink>>Sources, then a clock block onto the model window. Step4: Go to >>Simulink>>User-Defined Functions, then a “Embedded MATLAB Function” block onto the model window. Step5: Go to >>Simulink>>Math Operations, then the “Sum” block onto the model window. Step6: Go to >>Simulink>>Sinks, then drag the “To Workspace” block onto the model window. Double click the block and select “Array” under Save Format. Step7: Edit the “Sum” block so that it takes 3 inputs with signs +, −, −. Go to Math Operations, then drag the “Gain” block to model window. Edit its gain to 1.4. Step8: Connect the terminals as show in the picture below. Step9: We can now set the Embedded MATLAB Function block to reflect any function. In this example, we use f(t) = sin(t). Step10: We can set the initial value of the integrators by double clicking the integrator block. In this example, we leave them at 0. Step11: Run the simulation, by pressing the play button or go to >>Simulation>>Start. Now go to Matlab command window, notice 2 new variables in the workspace “simout” and “tout”. We can use these variables as normal Matlab variables. Plot of “simout” versus “tout” is shown in the picture below. Lab Report Submit your individual report for the following four problems via EE4314 Blackboard (https://elearn.uta.edu) in one of the following file formats: .doc, .docx, or .pdf. Your report should include –if any– your formulations, listings of MATLAB code, pictures of Simulink block diagrams, and resulting graphs for each problem. Make sure that you show all your work and provide all the information needed to make it a standalone and self-sufficient submission. Have an appropriate report format for your submission. This lab handout is a good example as to how you should format your report. Make sure that you include the following information in your report: Report title, your name, ID number, lab section, report due date for you. Answers to the problems with your o Mathematical derivations and formulations. It is highly recommended that you use the Equation Editor of Microsoft Word, MathType, or a similar editor to write your equations. You can also scan handwritten equations and merge them into your report. o Pictures of Simulink block diagrams and property windows of important blocks in the simulation. o Listings of MATLAB codes with inline comments and explanation in text. o Pictures of data plots with appropriate axis labeling, titles, and clearly visible axis values. Screen printing is not well accepted. Comments –if any– to let us know how we can make your learning experience better in this lab. Below is the assignment policy: This assignment is due 2/19/2015 by 11:59 pm. You must upload a single file in .doc, .docx, or .pdf format via the “Lab Report Submission” link at EE4314 Blackboard (https://elearn.uta.edu). Late reports will get 20% deduced score from the normal score for each late day (0-24 hr) starting right after the due date and time. For example, a paper that is worth 80 points and is 2 days late (24hr – 48hr) will get 80 – 80 × 2 × (20/100) = 48 points. A paper that is late for 5 or more days will get 0 score. You will have two chances of attempt to submit your report via Blackboard and only the last submission will be considered. Grading is out of 100 points and that includes 20% (20 points total) for the format. For example, part (b) of Problem 1 is 10 points and 2 points of that is for the format of your answer. A nice format refers to a clear, concise, and well organized presentation of your work. Problem 1 (25 pts). Let 𝑥̈ + 2𝜁𝜔𝑛 𝑥̇ + +𝜔𝑛2 𝑥 = 𝑓 (1) be a second order dynamical system, in which f = f(t) is the input with unit (m/s2), x = x(t) is the output in (m), ζ = 2.5 is the damping ratio (dimensionless) and ωn = 4 (rad/s) is the natural frequency. a) (15 pts) Use ode23() to simulate the time response of the system from t = 0 to t = 10 s with zero input (i.e. f(t) = 0), and initial conditions of (0.45 m, 0.21 m/s) for x and the first time derivative of x, respectively. Write the program using m-file(s) that output a plot of the change of x and 𝑥̇ with time. Plot both variables on the same graph with proper labeling and annotation such that x and 𝑥̇ are clearly distinguishable. (Hint for ode23: You need to use the state space representation of the system to create a function that returns the derivatives of x. For instance, let 𝑥1 = 𝑥, 𝑥2 = 𝑥̇ . Hence, your function implementation will return dx such that dx=A*x+B*f. Note that x=[x1; x2] is a vector of two elements so A is 2×2 and B is 2×1. Note also that ode23 requires a 2×1 vector of initial values.) b) (10 pts) Use ode23() to simulate the time response of the system from t = 0 to t = 10 s 1, 0 ≤ 𝑡 ≤ 4 𝑠 where 𝑥(0) = 0, 𝑥̇ (0) = 0 and 𝑓(𝑡) = { . Write the program using m0, 4 < 𝑡 ≤ 10 𝑠 file(s) that output a plot of the change of x and 𝑥̇ with time. Plot both variables on the same graph with proper labeling and annotation such that x and 𝑥̇ are clearly distinguishable. Problem 2 (25 pts). For the system in equation (1) with ζ = 2.5, ωn = 4 rad/s, and zero initial conditions, a) (8 pts) Transform the differential equation to frequency domain representation using Laplace transform. Show the steps of your formulation and indicate what Laplace transform properties that you use. (You do not need to use MATLAB for this part.) b) (5 pts) Use the tf command to create the system transfer function. Show your commands/codes and the transfer function you get. c) (6 pts) Plot the step response of the system obtained in part (b) for 0 ≤ 𝑡 ≤ 10 𝑠. Indicate which variable the plot shows: 𝑥, 𝑥̇ , or 𝑥̈ . d) (6 pts) Using lsim command, plot the time response of the system where f is a sinusoidal wave of amplitude 1.5 m/s2 and 0.85 Hz frequency for 0 ≤ 𝑡 ≤ 10 𝑠. Problem 3 (25 pts). For the system in equation (1) with ζ = 2.5 and ωn = 4 rad/s, a) (8 pts) Create a Simulink model of the 2nd order differential equation where f(t) is a function generator. Show the block diagram and indicate which signal line is 𝑥, 𝑥̇ , and 𝑥̈ . b) (7 pts) Simulate the time response of the system from t = 0 to t = 5 s where 𝑥(0) = 0.4, 𝑥̇ (0) = 0.2 with f(t) as a sinusoidal function of 1.5 m/s2 amplitude and 0.85 Hz frequency. Is the result the same as the result of Problem 2-d, why? c) (10 pts) Pick two different amplitudes and frequencies for f(t) such as f1(t) and f2(t). Simulate the time response of the system for both f1 and f2 from t = 0 to t = 5 s with zero initial conditions. Show by using the simulation results that the response of the system to f1(t)+f2(t) is the same as the sum of the individual responses to f1(t) and f2(t). What is this property of the system called? Problem 4 (25 pts). In Simulink, a) (7 pts) Using only two Step sources and a summer, create a single pulse signal that rises to an amplitude of 10 at t = .75 and falls back to zero at t = 3.5 as shown in part (a) of the following picture. Show the block diagram of your design and the plot of the pulse signal with clearly visible axes values. b) (10 pts) Using only Ramp sources and a summer, create the signal shown in part (b) of the following picture. Show the block diagram of your design and the plot of the signal with clearly visible axes values. 1.2 1 1.0 10 0 t = .75 t = 3.5 1.75 (a) 0 t = 1 t = 1.1 t = 1.3 t = 1.4 (b) c) (8 pts) Input the signal in part (b) to the system in Problem 3-a by replacing the function generator with the summed ramp sources. Apply zero initial conditions. Show the response of the system for 0 ≤ 𝑡 ≤ 5 𝑠 and compare it with the step response obtained in Problem 2-c.