You can use Simulink® to model a system and then simulate the dynamic behavior of that system. The basic techniques you use to create a simple model in this tutorial are the same as those you use for more complex models. This example simulates simplified motion of a car. A car is typically in motion while the gas pedal is pressed. After the pedal is released, the car idles and comes to a stop.
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||Represent the accelerator pedal|
|Gain||Multiply the input signal by a factor||Calculate how pressing the accelerator affects the car 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. The results display in a Scope window. The input pulse represents a press of the gas pedal — 1 when the pedal is pressed and 0 when it is not. The output ramp is 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, For example,
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 — Blocks for systems with continuous states
Discrete — 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 category 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 by copying the existing one and pasting it at another point using keyboard shortcuts.
Your model now has 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.
To view simulation results, connect the first output to a Signal Viewer.
Access the context menu by right-clicking the signal. Select Create & Connect Viewer > Simulink > Scope. A viewer icon appears on the signal and a scope window opens.
You can open the scope 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 — other models
could have time units in milliseconds or years.
To run the simulation, click the Run button .
The simulation runs and produces the output in the viewer.
This example takes an existing model,
models a proximity sensor based on this motion model. In this scenario, a digital
sensor measures the distance between the car an obstacle 10 m (30 ft) away. The
model outputs the sensor measurement, and the position of the car, taking these
conditions into consideration:
The car comes to a hard stop when it reaches the obstacle.
In the physical world, a sensor measures the distance imprecisely, causing random numerical errors.
A digital sensor operates at fixed time intervals.
To start, open the
moving_car model. In the MATLAB Command Window, enter
open_system(fullfile(matlabroot,... 'help', 'toolbox', 'simulink', 'examples', 'moving_car'))
You first need to model the hard stop when the car position reaches
10. The Integrator, Second Order block has
a parameter for that purpose.
Double-click the Integrator, Second Order block. The Block Parameters dialog box appears.
Select Limit x and enter
for Upper limit x.
The background color for the parameter changes to indicate a modification that is not applied to the model.
Click OK to apply the changes and close the dialog box.
Add a sensor that measures the distance from the obstacle.
Modify the model. Extend the model window to accommodate the new blocks as necessary.
Find the actual distance. To find the distance between
the obstacle position and the vehicle position, add the
Subtract block. Also add the Constant
block to set the constant value of
for the position of the obstacle.
Model the imperfect measurement that would be typical
to a real sensor. Generate noise by using the
Band-Limited White Noise block from
the Sources library. Set the
Noise power parameter to
0.001. Add the noise to the
measurement by using an Add block from
the Math Operations library.
Model the digital sensor that fires every 0.1 seconds.
In Simulink, sampling of a signal at a given interval
requires a sample and hold, implemented by a zero-order
hold. Add the Zero-Order Hold block from
the Discrete library. After you add the
block to the model, change the Sample
Time parameter to
Add another Outport to connect to the sensor output. Leave the Port number parameter as default.
Connect the new blocks. Note that the output of the Integrator, Second-Order block is already connected to another port. To create a branch in that signal, left-click the signal to highlight potential ports for connection, and click the appropriate port.
Add signal names to the model to make it easier to understand.
Double-click the signal. An editable textbox appears.
Type the signal name.
To finish, click away from the textbox.
Repeat these steps to add the names as shown.
Compare the actual distance signal with the measured distance signal.
Create and connect a Scope to the actual distance. Note that the name of the signal appears in the viewer title.
Add the measured distance signal to the same viewer. Right-click the signal, and select Connect to Viewer > Scope1. Make sure you are connecting to the viewer you created in the previous step.
Run the model. The Viewer shows the two signals, actual distance in yellow and measured distance in blue.
Zoom into the graph to observe the effect of noise and sampling. Click the Zoom button . Left-click and drag a window around the region you want to see.
You can repeatedly zoom in to observe the details.
From the plot, you can see that the measurement can deviate from the actual value by as much as 0.3 m. This information becomes useful when designing a safety feature, for example, a collision warning.