Sei sulla pagina 1di 8

# ME240: Lab 3 Handout _

## Introduction to MATLAB’s Simulink and Dynamic System Simulation

Objectives

In this lab you will learn how to use a program called Simulink to simulate dynamic systems.
Simulink runs under MATLAB and uses block diagrams to represent dynamic systems. You can
think of Simulink as a tool that allows programming in a graphical manner. Instead of large
amounts of code, you can simply drag-and-drop pre-made blocks into a “model” window and
connect the blocks’ inputs and outputs to create a program or simulation. Once a system is
defined, you can run a simulation by choosing options from the Simulink menu. The progress of
the simulation can be monitored while the simulation is running, and the final results can be
made available in the MATLAB workspace when the simulation is complete.

## Background: Structure of a Block Diagram

The overall block diagram in Simulink will have the form shown in Figure 1. This figure only
represents the general form a block diagram. There are many source, system, and sink blocks
from which to choose.

## Figure 1: A typical setup of a Simulink model block diagram.

Though Figure 1 gives an idea of the form a Simulink diagram, it would be best to give an actual
example. Shown in Figure 2 is a Simulink diagram that will simulate the response of a system
described by a transfer function. The source is a step input, the transfer function is a second-
order system, and the sink is a scope that will plot the data input to it versus time. The initial
conditions are assumed to be zero in this system.

2
Step Input Scope
s + 6s + 9
2

## Figure 2: An actual simulation model in Simulink

The Simulink block diagram shown in Figure 2 has a “source” represented by a step input, a
“system” defined by the transfer function block, and a “sink” using a scope to plot the response
of the system versus time. The system in Figure 2 could be thought of as a mass-spring-damper
system with a step force input and an output of a position. Thus the step input would be f(t) and
the scope plot could be considered x(t). Note that the output is automatically converted back to
the time domain even though the system is described by a transfer function. No conversion is
needed for the time-based input either.

Page 1 of 8
Background: Steps Required to Simulate a System

The steps required in order to simulate a system using Simulink are listed below.

## 1. Write the governing equations of the system

2. Create an intermediate block diagram representing the mathematics of the governing
equations.
3. Use relations between the inputs and outputs of the intermediate block diagram to
created a full simulation model (all-integrator model).
4. Set up block and simulation parameters.
5. Run the simulation.
6. Display and analyze the results.

The steps outlined above will now be put into practice with the following system.

As an example Simulink application, we will obtain the time response of the mass-spring-damper
system shown below having the initial conditions listed. As we proceed through this example,

System Parameters

m = 1kg b = 2 mN⋅s
k = 40 mN x (0) = 0.1m
x (0) = 0 ms f (t ) = 0

## Figure 3: A mass-spring-damper system

Write the Governing Equations. The first step towards defining the system in Simulink is
determining the governing equations for the system. You should verify that the equation of
motion for the above system is

## mx + bx + kx = f (t ) f (t ) = 0 (3.1)

Create an Intermediate Block Diagram. To begin, rewrite equation 3.1 in the following form.
The main purpose of this form is to find an equation representing the highest derivative.


x= 1
m ( −bx − kx ) (3.2)

Page 2 of 8
Using Equation 3.2, an intermediate (incomplete)
diagram can be drawn as shown in Figure 4. Notice
that the diagram represents Equation 3.2 through the
summation block. The variables x and x are x
considered as inputs to the system. The variable x is
considered the output of the system.

## Create an All-Integrator Block Diagram. The x

inputs, velocity and position, must be provided to
complete the diagram of Figure 4. Recognize that the
variables can be derived from the output, x
acceleration, by

x = ∫ 
Figure 4: Initial incomplete block diagram
x dt
(3.3)
x = ∫∫ 
x dt

Thus, we can find the variables x and x through the integration of x . Figure 5 shows the
completed block diagram with double integration blocks. The data representing x and x is fed
back from the integration into the respective constants (gains). A scope is attached to the line
representing x to show the position with time. In an actual block simulation, the parameters b, k,
and m would be replaced by their respective values.

x x x

x

Figure 5: The completed block diagram with source, system, and sinks

To create the above block diagram in Simulink, invoke Simulink by opening MATLAB and
typing

Page 3 of 8
This command displays a window containing icons representing the block libraries. Now select
File from the Simulink menu and choose NewModel to open a new untitled window in which
you can create the block diagram.

## We will now build the Simulink model shown in Figure 5.

Copying Blocks from Libraries. You are now ready to copy the individual blocks from the
block libraries. Blocks are copied from the block libraries by opening a library and dragging the
desired block into the new window. Placing the pointer on the desired block and then holding
the left mouse button down as you move a copy of the block to the desired location accomplish
dragging a block.

Most of the blocks that are needed for this example are contained in the libraries titled
Continuous and Math. To open this library, double-click on the icon using the mouse. This will
display the contents of the library. Note that the math library includes a summing junction, and a
gain block, while the continuous library contains and integrator. Copy a summing junction, two
integrators and two gain blocks into the window, and arrange the blocks as shown in the block
diagram above. The orientation of the gain blocks can be reversed by selecting the Flip Block

Only the blocks titled Scope and To Workspace remain to be copied into the new window at this
point. These blocks are located in the library titled Sinks. Copy these blocks to the new window
in the same manner as before. By including the To Workspace block, you are specifying that the
output of the system be stored as a vector in the MATLAB workspace after the simulation is
complete. Including the Scope block allows the output to be viewed as the simulation is running.

Connect Blocks. Now you need to connect the blocks. The angle bracket (>) pointing out of a
block represents the output port and the angle bracket pointing into a block represents the input
port. First, connect the output of the summing junction to the input of the first integrator by
dragging the output port of the summing junction to the input port of the integrator. A line with
an arrowhead should now appear showing the connection. Connect the output of the first
integrator to the input of the second integrator in the same manner.

Most of the remaining connections require that you connect line segments in order to route the
connections around other blocks. As an example of how this is done, let us now connect the
output of the second integrator to the input of the gain block [-k/m]. To do this, click on the
output port of the second integrator and drag it horizontally to create the first line segment and
release the left mouse button. Now click on the end of this line segment and drag it vertically to
create the second line segment and then release the left mouse button. To complete this process,
connect the end of this line segment to the input of the -k/m gain block.

Note that the output of the first integrator must be split into two line segments. To split the line,
point and right click on the line. Then, drag the new line vertically and release the right mouse
button to create the new line segment. Now you can proceed as before by connecting this output
to the input of the [-b/m] gain block. At this point, you should be able to make all of the
remaining connections on your own following the above procedures.

Page 4 of 8
Set Block Parameters. After all of the connections are made, you will need to set the gains of
the gain blocks to the proper values and set the initial conditions of the integrator. To set the
value of the [-b/m] gain block, double-click on the gain block to open the dialog box and enter
the appropriate value. In the same manner, set the value of the [-k/m] gain block. The sink
block labeled To Workspace must also be set up. Open the dialog box (double-click) for the
block labeled To Workspace and enter position for the variable name and enter 2000 for the
maximum number of rows (which is the maximum number of points that will be stored). Leave
Decimation and Sample Time as default, and change the Save Format to Matrix.

Set up Simulation Routine. Now the integration routine parameters must be set. To do so,
choose menu item Simulation and select Parameters. Choose a Variable-Step solver using the
ODE45 algorithm in the solver options. Set the start time, stop time, relative and absolute
tolerance to 0, 10, 1e-3, 1e-6, respectively. Here you have specified that the simulation will run
from 0 to 10 seconds with the given tolerances. Hit Apply and Close buttons.

## Simulate the System with Initial Conditions

Setting the initial conditions of the integrators is done in a similar fashion. The output of the
second integrator is position with the corresponding initial condition of 0.100 meters. Open the
dialog box of the second integrator and input 0.100. In the same manner, set the initial
condition for the first integrator equal to zero (the initial velocity).

In order to view the output as the simulation is running, open the block titled Scope and place its
window to the side of the current untitled window so that it can be seen as the simulation is
running. To run the simulation, select menu item Simulation and choose Start. You should see
the output in the Scope window as the result is generated. You may need to hit the auto-zoom
(binoculars) button to see it. The results of the simulation are also available in the MATLAB
workspace. Go to the MATLAB workspace and plot the time response by typing

» plot(tout,position)

where tout is the time vector automatically sent to the MATLAB workspace when a Simulink
simulation is run. The variable position is the output from the To Workspace block in Simulink.
If you run another simulation, the variables tout and position will be replaced with the new data.
You will need the data for a later plot so rename the data time_ic and pos_ic, respectively.

Save Block Diagram. You will want to save the block diagram since you will be asked to
modify it in the next exercise. To save the block diagram you have created, select menu item
File, choose Save and then supply the requested filename. NOTE: save the file in the directory
c:\matlab\me240.

Page 5 of 8
Find the Analytical Solution and Compare to the Simulation

To compare with the numerical integration results from Simulink, we will plot the inverse
Laplace transform of the output response X(s) by using MATLAB. In MATLAB, there is a
function impulse(.), which gives the response of the system to an impulse input. Since the
Laplace Transform of an impulse is unity, the impulse response is simply the inverse Laplace
transform of the transfer function. Therefore, if we treat X(s) as if it were a transfer function, the
impulse response of X(s) would give us its Laplace inverse x(t).

Type the following in MATLAB as an m-file. Run it several times varying the damping constant
and observe the different system responses. You don’t need to type the comments (beginning
with %). Any line with a % at the beginning will not be run in MATLAB. The symbols #
should be replaced with appropriate numerical values. The data obtained will be used to create a
plot as described in the Lab 3 Questions.

## %Define the system parameters

m = #;
b = #;
k = #;
x0 = #;
v0 = #;
%Define the numerator and denominator matrices
tfnum = [m*x0 b*x0+m*v0];
tfden = [m b k];
%Use tf() to define the transfer function in MATLAB
sys = tf(tfnum,tfden);
%Create a time vector 0 to 10 sec, 200 points.
time = linspace(0,10,200);
%Find the impulse response and plot the results
x = impulse(sys,time);
plot(time,x);

## Simulate the System with a Force Input (and zero I.C.)

Add a Source Step Input. We will now simulate the system with a step input and zero initial
conditions. To do this, you will find the Step block in the Sources library. Thus, the governing
equation for the system now takes the form

## mx + cx + kx = f (t ) f (t ) ≠ 0 (3.4)

Modify the block diagram appropriately and simulate this system with zero initial conditions and
a step input of value 1.

Before proceeding to the following exercises, you should become familiar with the other types of
blocks available in the block libraries. All of the blocks that you will need to complete this lab
will be found in the block libraries titled Sources, Sinks, Continuous, and Math. Browse through
each of these libraries to become acquainted with the available blocks. The function of each
block should be obvious from its title. However, if you are unsure about a block's function or its
use, double-click on the block to open its dialog box and then select Help to get a detailed
explanation of its use.

Page 6 of 8
Simulation of a Non-Linear System with Simulink

## Consider the pendulum with viscous damping shown below.

System Parameters

m = 8 kg b = 2 rad
N ⋅m
/s

l = 25 cm θ (0) = 12 π rad
g = 9.804 sm2 θ(0) = 0 rad
s

## ml 2θ + bθ + mgl sin(θ ) = 0 (3.5)

Note that this equation is non-linear because of the sin(θ) function. However, this non-linearity
can be easily handled in Simulink using the non-linear block called Trigonometric Function
located in the Math block library.

Draw a block diagram for the system making use of the [sin(.)] block described above. For
reference, see the approach taken to make the block diagram of the first system (mass-spring-
damper system). You should find that this block diagram is the same as the block diagram of the
earlier problem with a few exceptions. The old gains of [-k/m] and [-c/m] are now [-g/l] and [-
b/ml2], respectively, and there will be a [sin(.)] block that was not present in the first block
diagram. Make the necessary modifications to the previous block diagram (see next paragraph),
and set the gains and initial conditions to the correct values. Then, run a simulation from 0 to 10

A line or block can be deleted in Simulink by highlighting it and hitting the <DELETE> key. To
change the position of a block, you can simply drag it from one location to another and its
connections will remain intact. Finally, if you want to change the size of a block for readability,
highlight the block and drag any corner until it is the desired size.

Now consider a linear approximation of sin(θ ) ≈ θ for θ ≈ 0 . Equation 3.5 then becomes

## ml 2θ + bθ + mgl θ = 0 (3.6)

Run a dynamic simulation of this linear approximation with different initial conditions and
compare to the nonlinear simulation (with same initial conditions). At what initial conditions
does the linear approximation become poor? Note: you may want to store your simulations with
different variable names or use the hold command with the plot(.) command. See help hold in
MATLAB for information. Create a plot of the linear and non-linear simulations as described in
the Lab 3 Questions.

Page 7 of 8
Application of a Simulation to the Quarter-Car Body Model

In the preceding tutorial and exercise, the block diagrams for the systems were generated from
the equations of motion. If the system is non-linear or has non-zero initial conditions, this is the
approach that must be taken. However, for a linear time-invariant (LTI) system with zero initial
conditions, you may find that it is more convenient to take the Laplace transform of the
equations of motion and determine the system transfer function. This transfer function can then
be conveniently represented in Simulink using the single block titled Transfer Fcn located in the
Continuous block library. You are asked to used this approach in the following simulation:

Consider the simplified quarter-car suspension model shown in Figure 7. Here the mass m
represents ¼ the mass of a car body and the springs k and damper b represents a suspension
spring and shock absorber, respectively. The position of the car body, x(t), is defined to be 0
when the road input r(t) equals 0 and the car is at rest in the vertical sense (an equilibrium point).

System Parameters

m = 250 kg
b = 3000 m/s
N

k = 1000 mN

## Figure 7: Quarter-body simplified vehicle model

Suppose you want to know if the suspension, which has limited range, tops or bottoms out during
the impact with the curb. To find this, you would want to see a plot of the suspension travel.
Use Simulink to obtain a 10 second time history for the suspension travel defined by

z (t ) = x(t ) − r (t ) (3.7)

after hitting a 30 cm curb. Assume that the vertical position and velocity of the car body are zero
prior to impact and that the profile of the curb can be approximated by a 30 cm step input. You
will need to print out this data as described in Lab 3 Questions. Also, you will need to print the
response x(t) of the vehicle body m as described in Lab 3 Questions.

Page 8 of 8