Create a Simple Model - MATLAB & Simulink (2024)

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 NameBlock PurposeModel Purpose
Pulse GeneratorGenerate an input signal for the modelRepresent the accelerator pedal
GainMultiply the input signal by a constant valueCalculate how pressing the accelerator affects the car acceleration
Second-Order IntegratorIntegrate the input signal twiceObtain position from acceleration
OutportDesignate a signal as an output from the modelDesignate the position as an output from the model

Create a Simple Model- MATLAB & Simulink (1)

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.

Open New Model

Use the Simulink Editor to build your models.

  1. Start MATLAB®. From the MATLAB toolstrip, click Simulink Create a Simple Model- MATLAB & Simulink (2).

    Create a Simple Model- MATLAB & Simulink (3)

  2. 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.

    Create a Simple Model- MATLAB & Simulink (4)

  3. 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, 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

To open the Library Browser, in the Simulink Toolstrip, on the Simulation tab, click Library Browser.

Create a Simple Model- MATLAB & Simulink (5)

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, then select Help for the Pulse Generator block. The Help browser opens and displays the reference page for the block.

Blocks typically have several parameters. You can access all 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.

  1. 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 copy of the Pulse Generator block appears in your model with a text box for the value of the Amplitude parameter. Enter 1.

    Create a Simple Model- MATLAB & Simulink (7)

    Parameter values persist throughout the simulation.

  2. Add a Gain 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. For each block in the list, the name of the library that contains the block is displayed underneath the block name. Select the Out1 block stored in the Simulink library by pressing Enter.

    Create a Simple Model- MATLAB & Simulink (8)

    For more information about the quick insert menu, see Add Blocks to Models Using Quick Insert Menu.

  3. Add these blocks to the model using the Library Browser or the quick insert menu.

    BlockLibraryParameter
    GainSimulink/Math Operations

    Gain: 2

    Second-Order IntegratorSimulink/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.

  4. Arrange the blocks by clicking and dragging each block. To resize a block, drag a corner of the block.

    Create a Simple Model- MATLAB & Simulink (9)

Connect Blocks

Connect the blocks by creating lines between output ports and input ports.

  1. 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 Create a Simple Model- MATLAB & Simulink (10).

    Create a Simple Model- MATLAB & Simulink (11)

  2. Point to Create a Simple Model- MATLAB & Simulink (12) to see the connection cue.

    Create a Simple Model- MATLAB & Simulink (13)

    Click the cue to connect the blocks with a line and an arrow that indicates the direction of signal flow.

  3. Connect the output port of the Gain block to the input port on the Second-Order Integrator block.

  4. Connect the two outputs of the Second-Order Integrator block to the two Outport blocks.

  5. Save your model. In the Simulation tab, click Save.

    Create a Simple Model- MATLAB & Simulink (15)

Add Signal Viewer

To view simulation results, connect the first output to a Signal Viewer.

Click the signal. In the Simulation tab under Prepare, click Add Viewer. Select Scope. A viewer icon appears on the signal and a scope window opens.

Create a Simple Model- MATLAB & Simulink (16)

You can open the scope at any time by double-clicking the icon.

Run Simulation

Specify the stop time for the simulation. Then, simulate the model.

  1. On the Simulation tab, set the simulation stop time. In the Simulink Toolstrip, on the Simulation tab, enter the value in the Stop Time field.

    Create a Simple Model- MATLAB & Simulink (17)

    The default stop time of 10.0 is appropriate for this model. This time value has no units. The 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.

  2. To run the simulation, click Run Create a Simple Model- MATLAB & Simulink (18).

The simulation runs and produces the output in the scope viewer.

Create a Simple Model- MATLAB & Simulink (19)

Refine Model

Change Block Parameters

Open Live Script

This example takes an existing model, moving_car.slx, and models a proximity sensor based on this motion model.

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.

To start, open the moving_car model. At the MATLAB command line, enter:

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.

  1. Double-click the Integrator, Second-Order block. The Block Parameters dialog box appears.

  2. 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

Add a sensor that measures the distance from the obstacle.

  1. Modify the model. Expand 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 from the Math Operations library. Also add the Constant block from the Sources library to set the constant value of 10 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 a digital sensor that fires every 0.1 seconds. 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. After you add the block to the model, change the Sample Time parameter to 0.1.

    • Add another Outport to connect to the sensor output. Keep the default value of the Port number parameter.

  2. 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.

    Create a Simple Model- MATLAB & Simulink (20)

Annotate Signals

Add signal names to the model.

  1. Double-click the signal and type the signal name.

    Create a Simple Model- MATLAB & Simulink (21)

  2. To finish, click away from the text box.

  3. Repeat these steps to add the names as shown.

    Create a Simple Model- MATLAB & Simulink (22)

Compare Multiple Signals

Compare the actual distance signal with the measured distance signal.

  1. Create and connect a Scope Viewer to the actual distance signal. Right-click the signal and select Create & Connect Viewer > Simulink > Scope. The name of the signal appears in the viewer title.

  2. Add the measured distance signal to the same viewer. Right-click the signal and select Connect to Viewer > Scope1. Make sure that you are connecting to the viewer you created in the previous step.

    Create a Simple Model- MATLAB & Simulink (23)

  3. Simulate the model. The scope viewer shows the two signals, actual distance in yellow and measured distance in blue.

    Create a Simple Model- MATLAB & Simulink (24)

  4. Zoom into the graph to observe the effect of noise and sampling. Click Zoom Create a Simple Model- MATLAB & Simulink (25). Left-click and drag a box around the region you want to see more closely.

    Create a Simple Model- MATLAB & Simulink (26)

    You can repeat this action as needed to observe the details.

    Create a Simple Model- MATLAB & Simulink (27)

The plot shows that the measurement can deviate from the actual value by as much as 0.3 m. This information becomes useful when designing safety features, such as a collision warning.

See Also

Blocks

  • Pulse Generator | Gain | Second-Order Integrator | Add | Constant | Zero-Order Hold | Band-Limited White Noise

Related Topics

  • Model and Validate a System
Create a Simple Model
- MATLAB & Simulink (2024)
Top Articles
Latest Posts
Article information

Author: Tish Haag

Last Updated:

Views: 5872

Rating: 4.7 / 5 (67 voted)

Reviews: 90% of readers found this page helpful

Author information

Name: Tish Haag

Birthday: 1999-11-18

Address: 30256 Tara Expressway, Kutchburgh, VT 92892-0078

Phone: +4215847628708

Job: Internal Consulting Engineer

Hobby: Roller skating, Roller skating, Kayaking, Flying, Graffiti, Ghost hunting, scrapbook

Introduction: My name is Tish Haag, I am a excited, delightful, curious, beautiful, agreeable, enchanting, fancy person who loves writing and wants to share my knowledge and understanding with you.