You can use Simulink® to model a system and then simulate the dynamic behavior of that system. The basic techniques you use to create the simple model in this tutorial are the same techniques that you use for more complex models.
To create this simple model, you need four Simulink blocks. Blocks are the model elements that define the mathematics of a system and provide input signals:
Sine Wave — Generate an input signal for the model.
Integrator — Process the input signal.
Bus Creator — Combine multiple signals into one signal.
Scope — Visualize and compare the input signal with the output signal.
Simulating this model integrates a sine wave signal to a cosine signal and then displays the result, along with the original signal, in a Scope window.
Use the Simulink Editor to build your models.
Start MATLAB®. From the MATLAB Toolstrip, click the Simulink button .
A short delay occurs the first time you open Simulink.
Click the Blank Model template, and then click the Create Model button.
The Simulink Editor opens with a new block diagram.
Select File > Save as. In the File
name text box, enter a name for your model, For example,
From the Simulink Library Browser you can search for blocks to use in your model. Also, you can create a new Simulink model, project, or Stateflow® chart.
From the Simulink Editor toolbar, click the Simulink Library 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.
Search for a Sine Wave block. In the search box on
the browser toolbar, enter
sine, 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 <block name>. The Help browser opens with the reference page for the block.
View block parameters. Right-click a block, and then select Block Parameters. The block parameters dialog box opens.
You build models by dragging blocks from the Simulink Library Browser window to the Simulink Editor or single-clicking your model and entering a search term.
To build the simple model, begin by copying blocks from the Simulink Library Browser to the Simulink Editor.
In the left pane of Simulink Library Browser,
From the right pane, select the Sine Wave block.
Drag the Sine Wave block to the Simulink Editor. A copy of the Sine Wave block appears in your model.
Add a Scope block using this alternative procedure:
Click within the block diagram.
After the search icon appears, type
and then from the list, select
Add the following blocks to your model using one of the approaches you used to add the Sine Wave and Scope blocks.
Your model should now have the blocks you need for the simple model.
Before you connect the blocks in your model, arrange the block inputs and outputs to make signal connections as straightforward as possible.
Move the Scope block after the Bus block output. You can either:
Click and drag the block.
Select the block, and then press the arrow keys on your keyboard.
Move the blocks until your model looks similar to the following figure.
Most blocks have angle brackets on one or both sides. These angle brackets represent input and output ports:
> symbol pointing into a
block is an input port.
> symbol pointing out of
a block is an output port.
You connect block output ports to input ports with lines. The lines represent signals with time-varying values.
Connect the blocks by drawing lines between output ports and input ports.
Position the cursor over the output port on the right side of the Sine Wave block.
The pointer changes to a cross hair (+).
Click, and then drag a line from the output port to the top input port of the Bus block.
While holding down the mouse button, the connecting line appears as a red dotted arrow.
Release the mouse button when the pointer is over the output port.
Simulink connects the blocks with a line and an arrow indicating the direction of signal flow.
Connect the output port of the Integrator block to the bottom input port on the Bus Connector block using a Ctrl key shortcut:
Select the Integrator block.
Press and hold the Ctrl key.
Click the Bus Connector block.
The Integrator block connects to the Bus Connector block with a signal line.
Connect the Bus Connector block to the Scope block by aligning ports:
Click and drag the Scope block until its input port is aligned with the Bus Connector output port. A light blue line appears between the ports when the blocks line up horizontally.
Release the mouse button. Ablue arrow appears as a suggested connection.
Click the end of the blue arrow. The arrow changes to a black signal line connecting the blocks.
Your simple model is almost complete. To finish the model, connect the Sine Wave block to the Integrator block. This connection is different from the other connections, which all connect output ports to input ports.
Hold down the Ctrl key.
Position the cursor where you want to start a branch line. Click, and then drag the cursor away from the line to form a dotted-red line segment.
Drag the cursor to the Integrator input port, and then release the mouse button.
The new line, called a branch line, carries the same signal that passes from the Sine Wave block to the Bus block.
Drag line segments to straighten and align with blocks. Your model is now complete.
Before you simulate the behavior of your model, you can modify the default values for the configuration parameters. The configuration parameters include the type of numerical solver, start time, stop time, and maximum step size.
From the Simulink Editor menu, select Simulation > Model Configuration Parameters. The Configuration Parameters dialog box opens to the Solver pane.
Tip Alternatively, you can open the Model Configuration Parameters dialog box by clicking the parameters button on the Simulink Editor toolbar.
In the Stop time field, enter
In the Max step size field, enter
With the Solver parameter set to
auto, Simulink determines
the best numerical solver for simulating your model.
From the Simulink Editor menu bar, select Simulation > Run.
The simulation runs, and then stops when it reaches the stop time specified in the Model Configuration Parameters dialog box.
Tip Alternatively, you can control a simulation by clicking the Run simulation button and Pause simulation button on the Simulink Editor toolbar or Scope window toolbar.
After simulating a model, you can view the simulation results in a Scope window.
Double-click the Scope block.
The Scope window opens and displays the simulation results. The plot shows a sine wave signal with the resulting cosine wave signal.
On the Scope window toolbar, click the Style button.
A Style dialog box opens with display options.
Change the appearance of the display. For example, select white for the display color and axes background color (icons with a pitcher).
Select black for the ticks, labels, and grid colors (icon with a paintbrush).
Change signal line colors for the Sine Wave to blue and the Integrator to red. To see your changes, click OK or Apply.