Create a Simple Model
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 constant value | Calculate how pressing the accelerator affects the car acceleration |
Second-Order Integrator | Integrate the 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 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.
Open New Model
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.
To avoid shadowing, the Simulink Editor checks loaded models and files on the path and creates a model with the next available name:
untitled
,untitled1
,untitled2
, and so on.From the Simulation tab, select Save > Save as. In the File name text box, enter a name for your model, for example,
simple_model
. Click Save. The model is saved with the file extension.slx
.
Open Simulink Library Browser
Simulink provides a set of block libraries that are organized by functionality in the Library Browser. These 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
To open the Library Browser, in the Simulink Toolstrip, on the Simulation tab, click Library Browser.
To browse through the block libraries, in the library tree, expand a category and then a functional area.
To search all of the available block libraries, enter a search term.
For example, find the Pulse Generator block. In the search box,
enter pulse
, then press Enter. The software
searches the libraries for blocks with pulse
in their name or
description and then displays the blocks on the Search Results
tab of the Library Browser.
Tip
You can return to browsing the library tree by clicking the Library Tab.
Get detailed information about a block. On the Search Results tab, right-click the Pulse Generator block, and then select Help for the Pulse Generator block. The documentation opens and displays the reference page for the block.
Blocks typically have several parameters. For most blocks, you can access block parameters by double-clicking the block.
Add Blocks to Model
To start building the model, add blocks to the model canvas. You can add blocks using the Library Browser or the quick insert menu.
Start by adding a Pulse Generator block from the Library Browser.
From the Sources library, drag the Pulse Generator block to the Simulink Editor. A Pulse Generator block with the default value of
1
for the Amplitude parameter is added to your model.Parameter values persist throughout the simulation.
Add an Outport block using the quick insert menu.
Double-click anywhere in the model canvas. In the quick insert menu that appears, enter
out
. A list of blocks appears. Verify that the Outport block from the Simulink library is selected. Check the library name listed under the block name and the block description in the details pane to the right of the search results.Tip
To hide the details pane, click the arrow
. To show the details pane, click the arrow again.
Add the Outport block to the model by pressing Enter.
For more information about the quick insert menu, see Add Blocks to Models Using Quick Insert Menu.
Add these blocks to the model using the Library Browser or the quick insert menu.
Block Library Parameter Gain Simulink/Math Operations Gain:
2
Second-Order Integrator Simulink/Continuous Initial condition:
0
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 by clicking and dragging each block. To resize a block, drag a corner of the block.
Connect Blocks
Connect the blocks by creating lines between output ports and input ports. Then, save your model.
Connect the output port of the Pulse Generator block to the input port on the Gain block.
Connect the output port of the Gain block to the input port on the Second-Order Integrator block.
Connect the two outputs of the Second-Order Integrator block to the two Outport blocks.
For example, to connect the Pulse Generator block to the Gain block:
Click the output port on the right side of the Pulse Generator block.
The output port and all input ports suitable for a connection are indicated by a blue chevron symbol
.
To see the connection cue, point to the chevron symbol
.
Click the cue to connect the blocks with a line and an arrow that indicates the direction of signal flow.
This animation shows how to connect blocks in your model.
Run Simulation
Specify the stop time for the simulation. Then, simulate the model.
On the Simulation tab, set the simulation stop time. In the Simulink Toolstrip, on the Simulation tab, enter the value in the Stop Time text box.
The default stop time of
10.0
is appropriate for this model. This time value has no units. The time unit in a Simulink simulation depends on how the equations are constructed. This example simulates the simplified motion of a car for 10 seconds, but other models could have time units in milliseconds or years.To simulate the model, click Run
.
View Simulation Data
To view simulation results in the Simulation
Data Inspector, click Data Inspector
.
To plot data in the Simulation Data Inspector, select signals from the table on
the left. For example, to plot the position of the car, select the signal named
Out1:1
.
Refine Model
Change Block Parameters
This example takes models a proximity sensor based on an existing motion model, moving_car
.
In this scenario, a digital sensor measures the distance between the car and 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.
Open the moving_car
model.
open_system("moving_car.slx");
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
10
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 New Blocks and Connections
Modify the model to add a sensor that measures the distance from the obstacle. Expand the model window to accommodate the new blocks as necessary.
To find the distance between the vehicle and obstacle position, add a Constant block from the
Sources
library and set the value of block to10
. To find the distance between the obstacle position and the vehicle position, add the Subtract block from theMath Operations
library.To simulate the imperfect measurements of a real sensor, add noise to the model by using the Band-Limited White Noise block from the
Sources
library. Double-click the block to set the Noise power parameter to0.001
. Add the noise to the distance measurement by using an Add block from theMath Operations
library.In Simulink, sampling of a signal at a given interval requires a sample and hold. Add the Zero-Order Hold block from the
Discrete
library. Then, double-click the block to change the Sample Time parameter to0.1
.To log the sensor output, connect the Zero-Order Hold block to another Outport block.
Connect the new blocks. The output of the Second-Order Integrator 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.
Annotate Signals
Add signal names to the model.
Double-click the signal and type the signal name.
To finish, click away from the text box.
Repeat these steps to add the names as shown.
View Multiple Signals
Compare the actual distance
signal with the
measured distance
signal. The measured
distance
signal is logged as output. To log the actual
distance
signal, you can mark it for signal logging.
Select the signal in the model.
On the Simulation tab, click Log Signals
.
A logging badge indicates that the signal is marked for
logging.
Simulate the model. To view simulation results in the Simulation
Data Inspector, click Data Inspector
. Select the
actual
distance
and measured distance
signals to plot
both signals on the same time plot.
The plot shows that the measurement can deviate from the actual value by as much as 0.3 m. This information is useful when designing safety features, such as a collision warning.
View Signals on Separate Subplots
You can also analyze results by viewing signals on separate subplots. For
example, you can add subplots for the pedal position
and
velocity
signals to view the relationship between the
pedal position, the velocity of the car, and the distance between the car and
the obstacle.
In the model, right-click the pedal position
signal and
select Log Selected signals to mark the pedal
position
signal for signal logging. Then simulation the
model.
In the Simulation Data Inspector, click Visualizations and layouts
. Then, create a
3
×1
layout by specifying the number of
rows and columns in the grid.
Add the velocity
signal to the middle subplot and the
pedal position
signal to the bottom subplot. To add a
signal to a subplot, select the subplot, and then select the signal from the
signal table.
Visualizing the data on three subplots lets you to see the how pressing the
gas pedal affects the velocity of the car and its distance from the obstacle. To
explore this further, you can change the behavior of the gas pedal by adjusting
the parameters of the Pulse Generator block. To open the Block
Parameters dialog box for the Pulse Generator block, double-click
the block. For example, model pressing the gas pedal for one second twice by
setting Period to 5
and Pulse
Width to 20
.
Simulate the model. In the Simulation Data Inspector, press the space bar to fit the signals to view.
In the Simulation Data Inspector, you can further inspect the data by customizing the plot and signal appearance, zooming and panning, and adding data cursors. For more information, see Create Plots Using the Simulation Data Inspector.
See Also
Blocks
- Pulse Generator | Gain | Second-Order Integrator | Sum | Constant | Zero-Order Hold | Band-Limited White Noise