Przeglądaj wersję html pliku:

Simulink Tutorial (ang)


Christopher Lum Autonomous Flight Systems Laboratory Updated: 11/01/06

Simulink Tutorial Introduction
This document is designed to act as a tutorial for an individual who has had no prior experience with Simulink. It is assumed that the reader has already read through the Beginner and Intermediate Matlab Tutorials. For any questions or concerns, please contact Christopher Lum lum@u.washington.edu

Starting the Program
1. Start Matlab. 2. Simulink is an extra toolbox that runs on top of Matlab. To start this, type “simulink” in the Command Window or click on the Simulink icon.

Figure 1: Starting Simulink using icon or Command Window

© Christopher W. Lum

lum@u.washington.edu

Page 1/13

3. The simulink interface should now appear as shown below in Figure 2.

Figure 2: Simulink interface

4. Start a new Simulink model using File > New > Model 5. Let’s use Simulink to simulate the response of the Mass/Spring/Damper system described in Intermediate Matlab Tutorial document. Recall that the state space representation of this system was given by

& x = Ax + Bu
y = Cx + Du

Equation 1

© Christopher W. Lum

lum@u.washington.edu

Page 2/13

1   0 where A =   − k / m − c / m   

 0  B= 1 / m    

C = (1 0 )

D=0

For this problem, let k = 2, m = 1, c = 0.5. Double click on the “Continuous” library from the main Simulink Blockset. This opens library. 6. Click on the “State-Space” block and drag this into your blank model. Your model should now look like Figure 3.

Figure 3: Simulink model with just state space block added

7. We now need to define the parameters of this block. Double click on the block to enter the parameters. Enter in the A, B, C, and D matrices. Leave the initial conditions as 0. (Note that this is the initial state vector, x (0 ) and since there are 2 states, 0 actually implies

x (0) = (0 0) )
T

8. Now, let’s subject this system to a unit step input which occurs at t = 1 second. Click on “Sources” in the Simulink interface and find the “Step” block. Drag this into the model and connect the output of the step to the input of the state space model (this can be done by clicking on the Step then holding Ctrl and then clicking on the state-space block). 9. We would like to be able to view to output of the system so Click on “Sinks” in the Simulink interface and find the “Scope” block. Drag this into the model and

© Christopher W. Lum

lum@u.washington.edu

Page 3/13

connect the output of the state-space block to the input of the sink. Your simulink model should now look like

Figure 4: Simulink model with source and sink

10. Save the model as “mass_spring_damper_model.mdl” 11. We can now simulate the system. This can be done many ways as listed below a. Click on the “Run” button b. Go Simulation > Start c. Using the “sim” command from the Command window. For now, simply use option a or b, we will visit using option c later. 12. We would like to look at the response of the system using the scope. Double click on the scope block to open it up. Autoscale the plot so that you can see the response (the autoscale button looks like a pair of binoculars). You should see something similar to Figure 5.

© Christopher W. Lum

lum@u.washington.edu

Page 4/13

Figure 5: Simulated response of system from scope block

13. Although this is nice for simple analysis, we would like to interface this with Matlab so we can analyze the data using Matlab functions. Let’s analyze how the system response changes if we use different damping coefficients of c = 0.5, 1 and 1.5. This would be very tedious if we had to change the A matrix each time by hand and then simulate the system and then look at the plot. Therefore, we will use the m-file to write a script which will do this for us. 14. Start a new m-file. 15. Let’s first analyze the system response when c = 0.5. Define the A, B, C, and D matrices in the m-file. A sample code is shown below

© Christopher W. Lum

lum@u.washington.edu

Page 5/13

16. We can actually use variables in all the simulink blocks provided that they are defined in the Workspace before the model is run. Now change the parameters of the State-space block to match the matrices that you defined in the m-file. The state space block should like similar to

Figure 6: State space block parameters using variables as parameters

© Christopher W. Lum

lum@u.washington.edu

Page 6/13

17. We need to export the data from simulink to Matlab so that we can plot it. Namely, we would like to see both the input and output of the system. To do this, we use the “To Workspace” block which can be found in the Sinks library. Drag 2 of these blocks into your model and connect them to the input and output (Note: to make a branch of a signal, right click on the signal and then drag to the second connection) 18. We need to modify the parameters of these two blocks slightly. The appropriate parameters are shown below

Figure 7: Parameters for the “To Workspace” block

Your simulink model should now look like

© Christopher W. Lum

lum@u.washington.edu

Page 7/13

Figure 8: Simulink model with “To Workspace” blocks

19. The simulink model is now setup to export the data to the workspace. When the model is run, three variables will be created in the workspace. They are
Variable Name sim_u sim_y tout Class struct array struct array double array Comments Created by 1 “To Workspace” block Created by 2nd “To Workspace” block Automatically created by Simulink when you run a model
st

20. Write code to run the model from the m-file using the “sim” command. Also, write code to extract the data (namely the input and output of the model). A sample code is shown below. Functions: sim Note: For our purposes, use sim with only 1 argument, the name of the model which you are trying to run

© Christopher W. Lum

lum@u.washington.edu

Page 8/13

21. Find the maximum response of the system. That is, find max(y). A sample code is shown below Functions: max, find, Note: Here is an example of how to use these functions

22. Plot both the input and output of the system on the same graph. Plot the input as a thick blue line and plot the output as a thick red line. Mark the maximum response of the system with a large, thick, black x. Label the plot appropriately. Add a grid and a legend. In the legend, report what the maximum value of y(t) is. A sample code and the output (after being exported as a .jpg) is shown below Functions: figure, hold on, plot, grid, xlabel, ylabel,legend, num2str Note: The num2str function is one of the most useful plotting functions. Be sure that you understand how to use it.

The output of this code should appear as

© Christopher W. Lum

lum@u.washington.edu

Page 9/13

23. Now let’s see how the response of the system changes with different damping coefficients. Simulate and plot the response of the system with three different damping coefficients of c = 0.5, 1.0, and 1.5. We can use a for loop to make the coding easier. A sample code is shown below. Functions: for, length, … Note: The “…” symbol mean continue typing on the next line. This allows a very long line of code be broken up into two separate lines.

© Christopher W. Lum

lum@u.washington.edu

Page 10/13

© Christopher W. Lum

lum@u.washington.edu

Page 11/13

The output should look like

© Christopher W. Lum

lum@u.washington.edu

Page 12/13

Version History:

09/28/04: Created: 11/23/05: Updated: 12/01/05: Updated: 12/09/05: Updated: 10/31/06: Updated: 11/01/06: Updated:

made this format match other to-do documents and removed references to AA547. changed header to match how-to template Made changes to layout and added footer. Fixed typo of "Tout" to "tout" Touched up some of the graphics.

© Christopher W. Lum

lum@u.washington.edu

Page 13/13

 
statystyka