Simulink Tutorial
Simulink Tutorial
Simulink Tutorial
ST. AUGUSTINE
FACULTY OF ENGINEERING
DEPARTMENT OF CHEMICAL ENGINEERING
SIMULINK Tutorial
Objective:
The aim of this lab is to translate each model equation into a sequence of blocks, and to
connect them in the appropriate way to form a “system.” The variables in the equations
become signals that vary with time. Most Simulink blocks perform mathematical
operations on input signals to generate one or more output signals.
Example 1
The Scope provides a graphical trend plot as a function of time, and is just one of many things we
could do with our y signal. (Others include storing it on disk or in the MATLAB Workspace).
NOTE: the up/down/left/right arrow directions are arbitrary and have no
significance. The important concept is the flow of information from one
block to another.
Connecting blocks
The small > symbol on the right side of the Step block marks its “outport.” Note the “inport” on
the Gain block’s left side. Click and drag from the Step’s outport to the Gain’s inport to connect
them. The resulting signal line must have a solid, filled-in arrowhead, as in Figure 1. If not, the
blocks aren’t connected; try again. Similarly, connect the rest of the blocks until you obtain the
result shown earlier in Figure 1. Note the way Simulink automatically makes a neat right-angle
connection when you connect the Constant to the Sum.
The zero sample time signifies that the block’s output will be a continuous function of time. If it
were a positive value, the block’s output would be defined at integer multiples of the sample time,
but undefined at other times.
Leave all parameters at their defaults for this example.
Simulation parameters
Next we check the Simulation Parameters. In your model window, click on the Simulation menu
and select Configuration Parameters to open the window shown in Figure 4.
Figure 4 Simulation parameters menu, Solver panel
The left side allows you to navigate to various configuration panels. The Solver panel (selected in
Figure 4) allows you to specify the time at which the simulation starts and stops (which depends
on the time scale of your problem). Note also the Solver options. The default solver, ode45, is a
th
variable-step-size, 4 -order Runge Kutta algorithm, which is a good all-around choice, but is poor
for stiff problems, and there are situations where a fixed-step-size algorithm would be better. See
the Simulink Help for details. We’ll use ode45 in this simple example.
You can usually use the defaults for the remaining parameters on this panel, but if you suspect
that your results are inaccurate, try decreasing the relative tolerance by an order of magnitude or
two. If the results change significantly, decrease it even further. Direct control of the step size can
also be useful, as we’ll see later.
The Data Import/Export panel controls communication between MATLAB and Simulink. You
might find this valuable in some cases, but we’ll use the defaults for this and all other panels.
Close the configuration window. As the final preparation for the simulation, double-click on the
Scope to open the graphical display.
Figure 5 Ready to start the simulation
Example 2
Using subsystems
Modeling a single algebraic or differential equation is fairly easy, but what about multi-equation
models? It’s really no different. You define each equation and its variables (signals), then
combine equations by connecting their common signals.
One problem is that the diagram can become complicated, making it hard to understand the
model. You can reduce the apparent complexity by defining subsystems, each of which models
part of the complete system. This is conceptually similar to the use of functions and subprograms
in computer languages.
Example 3
For example, consider combining (5) with another differential equation:
( dz / dt ) = 2x – y – 3z
Adding this to our previous model would be easy, and the diagram would still be fairly clear, but
we will use subsystems to illustrate the concept.
Start with a new model window. Drag in a SubSystem block (from the Ports and Subsystems
category). Edit the block name, changing it to “Equation 5”. Next make a copy, naming it
“Equation 6.” Your model window should now resemble Figure 9 (name the model TwoODEs).
Figure 9 Two (renamed) subsystem blocks
Double-click on Equation 5 to open its modeling window. By default, it contains a single inport
(named In1) connected to a single outport (Out1). In other words, the default subsystem simply
equates the output signal with the input. Equation 5 also involves one “input” variable (x) and one
“output” (y). Thus, rename In1 to “x” and Out1 to “y”. Delete the signal connecting x and y, and
then insert Equation 5 using the same approach as before. Figure 10 shows the completed
subsystem window.
Close Equation 5 and return to the main model window. The Equation 5 icon now labels the input
and output ports with the appropriate names. Repeat the procedure to create Equation 6, which
requires two inputs (x and y) and generates one output (z). To get the extra inport, copy the
existing one or drag one from the Ports and Subsystems category.
Figure 11 shows one possible arrangement for Equation 6. Note the use of a 3-input summation
block (you could also use two 2-input summation blocks). Close Equation 6 and return to the
main model window.
Connect the subsystems (output y of Equation 5 to input y of Equation 6). Then define the x(t)
signal. This time use the Pulse Generator. Set its period to 10 and leave its other block
parameters at their defaults. Add a Mux (with 3 inputs – change the block parameter from 2 to 3)
and Scope, and set up to plot x, y, and z on the same scope.
Figure 12 shows the final arrangement. (Note the way the x and y signals cross – they are
independent.) Define the Simulation Parameters for a 30 time-unit run, with a maximum step size
of 0.1. Then run the simulation
Figure 12 Main model window with defined subsystems and input
Figure 13 shows the results. The yellow trace is the periodic pulse input, x(t). The magenta is y(t),
and the cyan is z(t). As with the sinusoidal input, the outputs eventually behave periodically.