You can use Simulink® to model a system and then simulate the dynamic behavior of that system. Simulink allows you to create block diagrams, where blocks you connect represent parts of a system, and signals represent input/output relationships between those blocks. The primary function of Simulink is to simulate behavior of system components over time. In its simplest form, this task involves keeping a clock, determining the order in which the blocks are to be simulated, and propagating the outputs, computed in the block diagram, to the next block. Consider a switch that turns on a heater. At each time step, Simulink must compute the output of the switch, propagate it to the heater, and then compute the heat output.
Often, the effect of a component's input on its output is not instantaneous. For example, turning on a heater does not result in an instant change in temperature. Rather, this action provides input to a differential equation, and the history of the temperature (a state) is also a factor. When the simulation of a block diagram requires solving a differential or difference equation, Simulink employs memory and numerical solvers to compute the state values for the time step.
Simulink handles data in three categories:
Signals — Block inputs and outputs, computed during simulation
States — Internal values, representing the dynamics of the block, computed during simulation
Parameters — Values that affect the behavior of a block, controlled by the user
At each time step, Simulink computes new values for signals and states. By contrast, you specify parameters when you build the model and can occasionally change them while simulation is running.
The basic techniques you use to create a simple model in this tutorial are the same techniques that you use for more complex models. This example simulates simplified motion of a car, after a brief press of the accelerator pedal.
A Simulink block is a model element that defines a mathematical relationship between its input and output. To create this simple model, you need four Simulink blocks.
|Block name||Block Purpose||Model Purpose|
|Pulse Generator||Generate an input signal for the model||Simulate the accelerator pedal|
|Gain||Multiply the input signal by a factor||Simulate how pressing the accelerator affects the car's acceleration|
|Integrator, Second-Order||Integrate input signal twice||Obtain position from acceleration|
|Outport||Designate a signal as an output from the model||Designate the position as an output from the model|
Simulating this model integrates a brief pulse twice to get a ramp and then displays the result in a Scope window. The input pulse represents a press of the accelerator pedal in a car, and the output ramp represents the increasing distance from the starting point.
Use the Simulink Editor to build your models.
Start MATLAB®. From the MATLAB Toolstrip, click the Simulink button .
Click the Blank Model template.
The Simulink Editor opens.
From the File menu, select Save as. In the
File name text box, enter a name for your model,
Save. The model is saved with the file extension
Simulink provides a set of block libraries, organized by functionality in the Library Browser. The following libraries are common to most workflows:
Continuous — Building blocks for systems with continuous states
Discrete — Building blocks for systems with discrete states
Math Operations — Blocks that implement algebraic and logical equations
Sinks — Blocks that store and show the signals that connect to them
Sources — Blocks that generate the signal values that drive the model
From the Simulink Editor toolbar, click the Library Browser button .
Set the Library Browser to stay on top of the other desktop windows. On the Library Browser toolbar, select the Stay on top button .
To browse through the block libraries, select a MathWorks® product and then a functional area in the left pane. To search all of the available block libraries, enter a search term.
For example, find the Pulse Generator block. In the search box on
the browser toolbar, enter
pulse, and then press the Enter key.
Simulink searches the libraries for blocks with
their name or description, and then displays the blocks.
Get detailed information about a block. Right-click a block, and then select Help for the Pulse Generator block. The Help browser opens with the reference page for the block.
Blocks typically have several parameters. You can access all parameters by double-clicking the block.
To start building the model, browse the library and add the blocks.
From the Sources library, drag the Pulse
Generator block to the Simulink Editor. A copy of the Pulse Generator block
appears in your model with a text box for the value of the
Amplitude parameter. Enter
Parameter values are held throughout the simulation.
Add the following blocks to your model using the same approach.
Port number: 1
Add a second Outport block right-clicking and dragging the existing one.
Your model should now have the blocks you need.
Arrange the blocks as follows by clicking and dragging each block. To resize a block, click and drag a corner.
Connect the blocks by creating lines between output ports and input ports.
Click the output port on the right side of the Pulse Generator block.
The output port, and all input ports suitable for a connection get highlighted.
Click the input port of the Gain block.
Simulink connects the blocks with a line and an arrow indicating the direction of signal flow.
Connect the output port of the Gain block to the input port on the Integrator, Second Order block.
Connect the two outputs of the Integrator, Second Order block to the two Outport blocks.
Save your model. Select File > Save and provide a name.
Your model is complete.
To view the results, connect the first output to a Signal Viewer.
Access the context menu by right-clicking the signal. Select Create & Connect Viewer > Simulink > Scope. This creates a viewer icon on the signal, and opens a Viewer display.
You can open the viewer at any time by double-clicking the icon.
On the model window, set the simulation stop time by changing the value at the toolbar.
The default stop time of
10.0 is appropriate for
this model. This time value has no unit. Time unit in Simulink depends on how the equations are constructed. This example
simulates the simplified motion of a car for 10 seconds.
To run the simulation, click the Run simulation button .
The simulation runs and produces the output on the Viewer.