Sei sulla pagina 1di 5

Introduction to Simulink

Simulink is a component of Matlab used to solve sets of differential equations


numerically. Its graphical nature makes it easy to set up and debug even complicated m
systems, and you can export the results of your simulation directly into Matlab for
further processing. We will use a simple example to show how to set up differential k c
equations for solution in Matlab.

The figure at right shows a simple mass-spring-damper system. As you learned in


Vibrations, the differential equation of motion is easy to obtain:

+ + = () (1)

where f(t) is a forcing function applied to the mass. We need to rearrange this slightly to get it into
Simulink format. In most cases we solve for the highest derivative in each differential equation. For the
equation above we have

1
= [() ] (2)

This is a second-order differential equation. To solve this numerically we must integrate it twice. To
begin, start Matlab and type simulink at the prompt. A new window will open containing libraries of
Simulink components. Most of the interesting components are in the Continuous, Math, Source and Sink
libraries. To begin a new Simulink simulation, go to the File menu and click New.

Since we must integrate the equation twice, an obvious way


to begin is to drag two Integrator blocks (in the Continuous 1 1
library) into the simulation window. The two Integrator blocks s s
are shown in the figure at right. They have been renamed Int1 Int2
Int1 and Int2 to help de-clutter the model.

Each integrator block has two ports: an input and an output. The input accepts an expression for a first
derivative and the output gives the results of integration. Since we need to integrate the equation twice, we
will connect two Integrators in series. A schematic view of how to perform double integration is shown at
right. To begin, we feed the input of the first Integrator
with an expression for the second derivative of the
variable. The Integrator integrates this once, and x
produces the first derivative. We send this first
derivative to the second Integrator, which provides the
variable at its output.

Of course, simply sending to the first


Integrator wont work; we must give it x
the expression for derived above (see
the figure at right).

1
To do this, we place a Sum block (Math
library) at the input to the first Integrator to
piece together the expressions for the 1 1
1
second derivative. You can control the s s
number and type of inputs to the Sum block 1/m Int1 Int2

by double-clicking it. Of the three inputs to


the expression, one is positive and two are
negative, so we type + - - at the List of signs prompt. It is also a good idea to place a Gain block (Math
library) between the Sum block and the first Integrator in order to accomplish the (1/m) premultiplication.
Your simulation diagram should now look like the figure above. Note that the blocks have been
connected with wires. To create a wire, click on the output of one block and drag the cursor to the input
of the next block.

The first quantity in the


summation is the forcing
function, f(t). One common 1
1 1

way to add a forcing function Signal


1/m
s
Int1
s
Int2
Generator
to a Simulink model is to use a
Signal Generator block (in the
Source library). Signal Generators are capable of producing sine waves, square waves, sawtooth waves and
random noise. Double-clicking a Signal Generator allows you to change its amplitude and frequency. For
now leave the defaults intact. (1 Hz sine wave with an amplitude of 1).

The second input to the


summation is the velocity of
the mass multiplied by the 1
1 1
s xdot s
damping coefficient ( ). The Signal
1/m Int1 Int2
Generator
velocity can be obtained by
creating a branch in the wire
after the first integrator. Do
this by right-clicking where you 1
want the branch to start and c*xdot xdot
c
dragging to where you want it
to end. Your diagram should now look something like the one shown at right.

Note that the velocity passes through another Gain block before entering the summation. The Gain block
multiplies the velocity by the damping coefficient. In the figure, the Gain block has been flipped so that
it points from right to left. Do this by right-clicking the block and selecting Format Flip Block. Double-
click the block and change the damping coefficient to 0.1.

All that remains is to add the third input into the Sum block. The output of the second Integrator gives the
position coordinate. Multiply this by the spring constant (using another Gain block, with k = 100) and
send the result into the Sum block. Your model should look like the figure on the next page. A Scope block
(Sinks library) has been added to the model in order to be able to quickly plot the output after running the
model.

2
1 1
1
s xdot s
Signal
Generator 1/m Int1 Int2 Scope

.1
c*xdot xdot
c

k*x x
100

To run the simulation click the Start Simulation button at the top of the window. This simple
simulation will run very quickly, almost instantly. Once it is completed you can double-click the Scope
block to see the position of the mass plotted versus time.

Now change the forcing frequency in the Signal Generator to 1.6 Hz and observe the response. As you can
see, the position of the mass blows up at this frequency. Why is this?

You have probably observed that the Scope has very limited plotting capabilities. It is really meant for
performing quick diagnostics on simulations. To obtain better quality plots you can use the XY Graph
block (also in the Sinks library). Here the x-coordinate is time, which is obtained by using a Clock block (in
the Sources library). Modify your model to look like the one in the figure below. After the simulation runs,
double-click the XY Graph block to see the results. You can also use a To Workspace block to send your
output to the Matlab command line for even better plots!

Clock

1 1
1
s xdot s
Signal XY Graph
Generator 1/m Int1 Int2

.1
c*xdot xdot
c

k*x x
100

3
A 2DOF System
The figure at right shows a simple mass-spring system with two masses, two dampers m1
and two springs. As we learned last week, the differential equations of motion are
easy to obtain:
k1 c1
1 1 + 1 ( 1 2 ) + 1 (1 2 ) = 1 ()
2 2 + 1 ( 2 1 ) + 2 2 + 1 (2 1 ) + 2 2 = 2 () m2

where f1(t) and f2(t) are forcing functions on masses 1 and 2, respectively. We need to c2
rearrange this slightly to get it into Simulink format by solving for the highest-order k2
derivatives. For the equations above we have

1
1 = [ () 1 ( 1 2 ) 1 (1 2 )]
1 1
(3)
1
2 = [ () 1 ( 2 1 ) 2 2 1 (2 1 ) 2 2 ]
2 2

The figure above shows one possible implementation of the system of equations in Simulink. There are a
couple of interesting things about this model:

1. The gain blocks are all given as variables, rather than numbers. For example, the gain block for the
first spring is given as k1, rather than giving its value directly.
2. The output of the model is sent to the workspace in the blocks labeled x1 and x2, as is the time
vector, t.

4
Important Note: the To Workspace blocks will send something strange called a Time Series to the
workspace by default, which I am not sure how to make use of. To send it as a simple series of numbers
(like we usually use in Matlab), you must double-click on each To Workspace block and change the Save
Format value to Array.

Of course, this simulation will not work on its own; we need to supply the values for the masses,
stiffnesses and dampers. We do this by writing an ordinary Matlab script, and calling the simulation from
there. Once the simulation has run, we can plot the results as usual. Create the following script in the
same directory as the simulation.
% 2DOF spring/mass/damper system

clear all; close all; clc

m1 = 10; % first mass (kg)


m2 = 20; % second mass (kg)

k1 = 1000; % first spring (N/m)


k2 = 2000; % second spring (N/m)

b1 = 1; % first damper (kg/s)


b2 = 2; % second damper (kg/s)

f1 = 10; % force on first mass (N)


f2 = 20; % force on second mass (N)
omega1 = 2; % frequency of first force (rad/s)
omega2 = 4; % frequency of second force (rad/s)

sim Spring_Mass_2DOF_Sim

plot(t,x1,t,x2)
grid on
xlabel('time (sec)')
ylabel('displacement (m)')
title('Displacement of Mass 1 and Mass 2')

When you first run the simulation, youll find that the plots are somewhat jagged and irregular. This is
because the default time step chosen by Matlab is too large to make a smooth graph. In Simulink, click on
the Simulation menu and choose Model Configuration Parameters. In the Solver Options block, choose Fixed-Step,
and pick 0.001 seconds as the fixed time step. Also, choose 5 seconds as the Stop Time. When you re-run
the simulation, the graphs should be smooth.

Displacement of Mass 1 and Mass 2


0.03

0.02

0.01
displacement (m)

-0.01

-0.02

-0.03
0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5
time (sec)

Potrebbero piacerti anche