0 ratings0% found this document useful (0 votes) 79 views17 pagesSimulink Tutorial
Simulink tutorial for engineers
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content,
claim it here.
Available Formats
Download as PDF or read online on Scribd
Christopher Lum
lum@u.washington.edu
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 concems, 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.
“ype “siurk” In
‘Sinn leon Command Window
— "7
Figure 1: Starting Simulink using icon or Command Window
3. The Simulink interface should now appear as shown below in Figure 2.
© Christopher W. Lum Jum@u.washington.edu Page 1/17i x
Fie Eat We Hae
a
oes
Contino: ari Sanranes
main
Simulink
blockset
— fame
$n corms
$3 Gocntrter
Sl orowe
$B Licht Tle
| ath operon
‘3 road vateaten
el aces
‘Bi reasuonter
2 sera tes
3) sardoutra
2 sale
ae
additional Cone Syren nbn
blocksets [5 si riesscretossne
BB tnten das
# Ml snuntowe,
S-B tutnany ston
ry
a2)
|:
a
LEH
Lesko abe
or Dosis
main
Simulink
Mev i blockset
libraries
Peas Suspeens
SinalFours
she
Uae Deined Ful
Figure 2: Simulink interface
4, Start a new Simulink model using File > New > Model
METHOD 1: 2” Order Ordinary Differential Equation
5. Let’s use Simulink to simulate the response of the Mass/Spring/Damper system
described in Intermediate MATLAB Tutorial document, Recall that the second
order differential equation which governs the system is given by
x =4un-£29-L20
mm
© Christopher W. Lum lum@u.washington.edu
Equation 1
Page 2/17Let us assume that we have signals which represent u(t), 2(f), and 2(1), we can
multiply them by a product of constants, then sum then in a specific fashion to
obtain the signal 2(/). Start a new model.
Double click on the “Continuous” library from the main Simulink Blockset. This
opens library.
6. We will now construct the signal 2(0). The first thing we need is an integrator.
Find this block in the ‘Continuous’ section and drag two of them into your blank
model. You can rename them if you like. Connect them as shown in
Dees (Se o5t|9e)> sf (Mabe s| Pane
We will construct the signal Z(t)
a SS sy
Figure 3: Model with two integrators
7. Looking at the ODE, we see that we also need the signal u(t). ‘This is an external
input, or a source. Let’s use a step function which can be found in the ‘sources’
group. Drag this into your model.
© Christopher W. Lum Jum@u.washington.edu Page 3/17DES $58 e52|2e)> ef (Raden pane
o tf}
eat oes
Figure 4: Model with two integrators and a step function
8. We now have the signals u(t), 2(t), and 2(f), we need to multiply these by the
values 1/'m , k/m,and c/'m, respectively. For this problem, let k=2, c= 0.5,
and m=1. We can multiply the signals using a ‘gain’ block which is found in the
“Math Operations’ group. Draw three ‘gain’ blocks into your model and connect
them appropriately. (Hint: You can flip the orientation of the block by right click
> Format > Flip Block).
onper ode 0 |
EE Yen Stadion Format Teck Heb
Deas $28 657 22)> fo (Asher wane
a Te
Figure 5: Model with integrators, step function, and gain blocks
9. We can modify the multiplication factor of each ‘Gain’ block by double clicking
and modify the parameters. Since this is scalar multiplication, ensure that the
‘Multiplication’ field is set to “Element-wise (K.*u)" as shown in Figure 6,
© Christopher W. Lum Jum@u.washington.edu Page 4/17Figure 6: Parameters forthe ‘Gain’ blocks.
10, The last thing we need to do is to add all the signals together using ‘Sum’ block.
Drag one into your model and modify the parameters as shown in Figure 7
Figure 7: Parameters forthe ‘Sum! block
11, Connect the signals appropriately.
© Christopher W. Lum lum@u.washington.edu Page S/17Pasty (—
igure 8: Model with signals connected.
12. At this point, the model accurately solves the ordinary differential equation,
However, we would like to be able to see the inputs and outputs. In other words,
‘we would like to monitor the signals u(t) and 2(¢). We can do this using a
“Scope” which is in the ‘Sinks’ group. Drag these into your model and connect,
them appropriately. (Hint: You can split a signal by holding down the right
mouse button on the existing signal and dragging)
Run button
Deas $28 652 92) sho (ened BaEe
—AT
oy
Pasty (5
Figure 9: Final model,
13. Save the model as ‘mass_spring_damper_model_ODE.mdl”
© Christopher W. Lum Jum@u.washington.edu Page 6/1714, 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
©. Using the “sim” command from the
Command window.
For now, simply use option a or b, we will visit using option c later,
15, 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 10.
=lix!
BH 2 2 | th |\G@ae »
2) u() signal
b)_ a(t) signal
Figure 10; Output from scope outputs
METHOD 2: State Space Representation
| that in addition to using a second order ODE to model the system, we can use a
state space representation of this system of
AE + Bu Equation 2
yaCe+Du
0 1) 0)
where 4= | B= | c=(l 0) D=0
-kim ~cim} 1/m}
Once again, let
S,and m=1
lum@u.washington.edu Page 7/1716, Start a new, blank model. Click on the “State-Space” block and drag this into
your blank model. Your model should now look like Figure 11.
LI jax
le Ett Yiew Simulation Format Took Help
D/sOS/i BB 22)> s|seh|aate
State Spase
Ready joe ag
Figure [1;, Simulink model with just state space block added
17. 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, ¥(0) and since there are 2 states, 0 actually implies
¥(0)=(0 0)
18. 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).
19. 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
connect the output of the state-space block to the input of the sink. Your simulink
model should now look like
© Christopher W. Lum lum@u.washington.edu Page 8/17Run button
Fie Edt View Simulation Formet Too’
Ole a| te 24)/> |e oe|pet ®
Jnes} 5
y= Oxrbu
Step State Spase Seopa
Reads 100% I lodets
Figure 12: Simulink model with source and sink
20. Save the model as “mass_spring_damper_model.mdt”
21. 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 13.
iol
|S8/929 ABB|G Se
iis
Figure 13: Simulated response of system from scope block
© Christopher W. Lum lum@u.washington.edu Page 9/1722. 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 e~ 0.5, 1.0 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
mefile to write a script which will do this for us.
23, Start a new m-file.
24. Let's first analyze the system response when ¢= 0.5. Define the A, B, C, and D
matrices in the mefile. A sample code is shown below
Part 15~-
apetine the constants
Rea;
moa
c= 0.57
eSnter the A, Ey C, and D matrices
As [Ol
-k/m -0/m]:
Be [a
am):
ceo
= (oe
25, 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
lum@u.washington.edu Page 10/17Figure 14: State space block parameters using variables as parameters
26. 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)
27. We need to modify the parameters of these two blocks slightly. The appropriate
parameters are shown below
© Christopher W. Lum Jum@u.washington.edu Page 11/17Figure 15: Parameters for the “To Workspace” block
‘Your simulink model should now look like
=a
y= babu
State Space
Figure 16: Simulink model with “To Workspace” blocks
28, 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
© Christopher W. Lum Jum@u.washington.edu Page 12/17VariableName Class ‘Comments
sim_u struct array Created by 1" “To Workspace” block
sim_y struct array Created by 2" “To Workspace” block
tout double array Automatically created by Simulink when you run a model
29, 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
Shet's mm che mod=1 from the a-file using the “sin” coumand
sin (‘maes_epring_dauper_nodel') 7
Slow extract the data
t= sim_u.time:
u = sim_u.signels.velues?
y = sim_y-signels.veluess
30, Find the maximum response of the system. That
is shown below
s, find max(y). A sample code
Functions: | max, find,
‘Note: Here is an example of how fo use these functions
Shet's find the waximm ressonse of the system
macy = asclyl?
Siow that we Enow max (y), Ue want to find out where in the vector this
soccurs. This means thet we want te find the index.
ax_index = find (y-suer_yl +
31. 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.
Adda 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 .ipg) 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.
Christopher W. Lum lum@u.washington.edu Page 13/17Sliow Let's plot the deta to verify chat we have it convect
figure
hold on
plot|t,u, 'b-', 'Linettideh', 3)
plov|t,y, "r=", "Linewiden', 3)
plot |t(max_index),y (max index), 'kx', (MerkerSize' ,1S, ‘Lineliidta',3)
oria
tate (‘Response of system with ¢ =
xlebel('t (se2}")
yhebel('y (n)'}
degena(‘u','y" ,['max(y) = ' munzstr (max_y) 1)
hold off
234)
The output of this code should appear as
Response of Stem wi
32. 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
Jong line of code be broken up into two separate lines
lum@u.washington.edu Page 14/17Glow let's vepeat Chis process for tares differant values of 5. Ue woula
SLike vo use = for loop oo mske UiLs exsiec. First, oe derine © veecor
hich conteine the values of © which we would lake ta use
crenge = [0.8 1 15]:
SSuart anew figues to ploo ell the cesults in (ue do this outside the for
logy because ve only want 1 figurs, net 3}. Aloo cam the old on
cigure
hold on
soerine & coler nap which nasically derimes snet ditrarent colors we ase to
splot the different responses
calex_nap ~ Jet{Leagth{c range};
cor n = Llengtn(c_renge|
SObrain the cumeent value of ©
© = e_xengein)
sRedefine the Ay B, Cy and D nateices with the new value of ©
A= (OL
“xi -o/n]s
B > (0
1/0),
c+ (Lol
Ds 01s
Shun the Simulink uodel wath the new values of A, B,C, and D
sim(‘uses_spcing_danper_woded')
Sxceact the date
v= simustines
u + sim uesignale-valuess
y+ sim y.signale.valnes;
sind the uox of y and the index where chis occurs
© Christopher W. Lum Jum@u.washington.edu Page 15/17wy Sa
suax_index = Eindtyssne_ zy)
algo we vant to store tue cata eo we can use ar an the legens lever
siek_ data in} = nox y
blot the ow data
ploc(s,y,"Linewiaun' 2, ‘color’ eater wep |2,2))
lor (sfame inde) ;yinm_indee}, 0! color jcoler aap te),
"Mackerssze! 15, 'Linetideh! ,2|
SPlot the input oniy once
plot(tsm,'x-', Lanelindth’ 2)
Mawel che caguce
bitle( Response of 2ysten with Different Danping Cofticients')
xlabel('t (929)")
qi@elc'y a")
Iegend(
[iy (e = Syonmaoee(e_range(2)),°)'JeLtumcts) =" emmcser inex,
Diy Ce = *aundoce (2 eange (2)),°)'TeL'uax(y) = *paundete (ne
Diy ce = *ammzece (2 eamge(3)),°)' J) Emax (y) = * auzser ee
fay
ona
The output should look like
lum@u.washington.edu Page 16/17Version History: 09/28/04
11/23/05:
12/01/05:
12/09/05:
10/31/06:
11/01/06:
09/15/09:
09/25/10:
© Christopher W. Lum
Created:
Updated: made this format match other to-do documents
and removed references to AAS47.
Updated: changed header to match how-to template
Updated: Made changes to layout and added footer.
Updated: Fixed typo of "Tout" to "tout"
Updated: Touched up some of the graphics.
Updated: Added 2" order ODE method.
Updated: Minor changes
lum@u.washington.edu Page 17/17