This example shows the basics of how to create a model, add blocks to it, connect blocks, and simulate the model. You also learn how to organize your model with subsystems, name parts of a model, and modify a model.
In the Simulink® Start Page, click the Blank Model template.
A new model based on the Blank Model template opens in the Simulink Editor.
Open the Library Browser so that you can access the blocks you need to create your model. In the Simulink Editor, click the Library Browser button .
A minimal model takes an input signal, operates on it, and outputs the result. In the Library Browser, the Sources library contains blocks that represent input signals. The Sinks library has blocks that you can use to capture and display outputs. The other libraries contain blocks you can use for a variety of purposes, such as math operations.
In this basic model, the input is a sine wave, the operation is a gain (which increases the signal value by multiplying), and you output the result to a scope. Try different techniques to explore the library and to add blocks to your model.
Open the Sources library. In the tree view of the Library Browser, click the Sources library.
In the right pane, hover over the Sine Wave block to see a tooltip describing its purpose.
Add a block to your model using a context menu. Right-click the block and select Add block to model untitled. (To learn more about the block, select Help from the context menu.)
Add a block to your model by dragging. In the library tree view, click Math Operations. In the Math Operations library, locate the Gain block and drag it to your model to the right of the Sine Wave block.
In the library tree view, click Simulink to view the sublibraries as icons in the right pane. This view is an alternative way to navigate the library structure. Double-click the Sinks library icon.
In the Sinks library, locate the Scope block and add it to your model using the context menu or by dragging it.
The figure shows your model so far.
Connect the blocks to create the relationships between model elements that you need to make the model operate. Reading the model is easier when you line up the blocks according to how they interact with each other. Shortcuts help you to align and connect the blocks.
Drag the Gain block so it lines up with the Sine Wave block. An alignment guide appears when the blocks line up horizontally. Release the block, and a blue arrow appears as a preview of the suggested connection.
To make the connection, click the end of the arrow. A solid line appears in place of the guide.
Line up and connect the Scope block to the Gain block using the same technique.
Tip: Use the Diagram > Arrange menu for additional alignment options.
You can set parameters on most blocks. Parameters help you to specify how a block operates in your model. You can use the default values or you can set values as needed. Use the Property Inspector to set parameters. Alternatively, you can double-click most blocks to set the parameters using a block dialog box. To understand when to use each approach, see Setting Properties and Parameters.
In your model, set the amplitude in the Sine Wave block and the value of the gain in the Gain block.
Display the Property Inspector. Select View > Property Inspector.
Select the Sine Wave block.
In the Property Inspector, set the Amplitude parameter
Select the Gain block and set the Gain parameter
3. The value appears on the block.
Suppose that you want to perform another gain but on the absolute value of the output from the Sine Wave block. Add blocks for this purpose, trying different techniques for locating blocks in the library and adding them to your model.
If you know the name of the block that you want to add, you can use a shortcut. Click in the model where you want to add the block, and type the block name, in this case Gain. A list of possible blocks appears.
Click the block name or, with the block name highlighted, press Enter. You can use the arrow keys to highlight the block name if it is not first in the list. You can use this technique to add blocks anywhere the blue magnifying glass appears, for example, when you draw a signal line.
Many blocks display a prompt for you to enter a value
for one of the block parameters. The Gain block prompts
you to enter the Gain value. Type
The name of each additional instance of a block adds a number that increments by 1, for example, Gain1, Gain2, and so on.
Add an Abs block to the model. Suppose
you do not know the library a block is in or the full name of the
block. In this case, you can search for it using the search box in
the Library Browser. Enter
abs in the search box
and press Enter. When you find the Abs block, add
it to the left of the Gain1 block.
Add another Scope block. You can right-click the existing Scope block and drag to create the copy or use Edit > Copy and Edit > Paste.
The figure shows the current state of your model.
Note: When you name blocks in a model in numerical order (for example Gain1, Gain2, and so on), copying and pasting them creates names that follow standard sorting order conventions for ASCII characters. This sorting order can result in a sequence of numbers on the block names that is hard to understand. If the numbering scheme is important to you, number blocks so that copying and pasting them creates names that follow a typical reading order. To do so, use a leading zero in the block names, for example Gain001, Gain002, and so on.
The input to the second gain is the absolute value of the sine wave. To use a single Sine Wave block as the input to both gain operations, create a branch from the Sine Wave block output signal.
For the first set of blocks in your model, you used the horizontal alignment guides to help you align and connect blocks. You can also use guides to align blocks vertically. Drag the Scope1 block so that it lines up under the Scope block. Release it when the vertical alignment guide shows that the blocks are aligned.
Align and connect the Abs and Gain1 blocks as shown.
Create a branch from the Sine Wave block output to the Abs block. With your cursor over the output signal line from the Sine Wave block, press Ctrl and drag down. Drag the branch until the end is to next to the Abs block.
Drag toward the Abs block until the line connects to it. Move the vertex as needed to straighten the line. (A circle appears over the vertex.)
Try these other methods to connect blocks:
Drag a connection from the output of one block to the input of the other block. Use this technique when your blocks are already aligned, that is, no guideline appears.
Select the first block and Ctrl+click the block you want to connect it to. This technique is useful when you do not want the blocks to align. The connection line bends as needed to make the connection, as shown in the figure.
Note: You can select multiple blocks to connect them to a block that has multiple inputs, such as a bus.
To approximate a diagonal line from line segments, press Shift and drag a vertex.
Tip: To improve the shape of a signal line, select the line and, from the ellipsis menu, select Autoroute Line. The line redraws if a better route between model elements is possible.
You can group blocks in subsystems and label blocks, subsystems, and signals. For more information about subsystems, see Create a Subsystem.
Drag a selection box around the Abs and Gain1 blocks.
Move the cursor over the ellipses that appear at the lower-right corner of the selection box. From the bar, select Create Subsystem.
A subsystem block appears in the model in place of the Abs and Gain1 blocks. To resize the subsystem block for the best fit in your model, drag the block handles.
Give the subsystem a meaningful name. Double-click
the block name and type
Open the Absolute Value subsystem by double-clicking it.
Tip: To use the Explorer Bar to navigate the model hierarchy, right-click the block and select Open in New Tab.
The subsystem contains the Abs and Gain1 blocks that you selected as the basis of the subsystem. They are connected in sequence to two new blocks: In1 (an Inport block) and Out1 (an Outport block). Inport and Outport blocks correspond to the input and output ports on the subsystem.
Click the Simulink Editor Up to Parent button to return to the top level of the model.
You can name any signal. Double-click a signal and
type the name. For example, double-click the signal from the Gain
block and type
My Signal. Double-click the line
and not a blank area of the canvas, which instead creates a separate
text element (annotation). For other techniques that you can use with
signal names, see Signal Name and Label Actions.
The figure shows the model after you create the subsystem, name it, and name a signal.
Tip: For additional resizing and alignment options, use the Diagram > Arrange menu.
You can simulate a model using the Simulation > Run command (Ctrl+T) or the Run button . Simulate the model using the technique that you prefer.
In this example, simulation runs for 10 seconds, the default setting.
Double-click both scope blocks to open them and view the results. In each scope, click the Autoscale button to view the entire signal.
The figure shows the two results. In the second plot, as expected, the absolute value of the sine wave is always positive.
You can add blocks on an existing signal, remove blocks from models, and redraw connections. To modify this model, add a bias to the input to both branches of your model. Also, replace one of the scopes with a different sink.
For some blocks, connecting a line from another block adds an input port or output port to the block. For example, a port appears on a subsystem when you add a connection to it. Additional blocks that create ports include the Bus Creator, Scope, and Add, Sum, and Product blocks. For more information, see Automatic Port Creation: Add inports and outports to blocks when routing signals.
Add a Bias block to the model and set the Bias parameter
Drag the block onto the signal line after the Sine Wave block but before the branch. If you need to make room for the block, drag the Sine Wave block to the left or move the end of the branch by dragging it to the right.
When you drag the block onto the signal line, the block connects to the signal line at both ends. Release the block when you are satisfied with the position.
Remove the Scope block. Press Shift and drag the Scope block if you want to disconnect it from the model but do not want to delete it. Cut or delete it using the Edit menu commands or a keystroke. The broken connection appears as a red dotted line.
Tip: When you delete a block that has one input and one output, a prompt appears between the broken connection lines. Click the prompt to connect the signals.
Add a To Workspace block to the model and place it at the end of the broken connection. The To Workspace block outputs the results to a variable in the MATLAB workspace.
Add another input to the subsystem. Add a Sine
Wave block to the model and set the amplitude to
Place it to the left of the subsystem block.
Drag a line from the new Sine Wave block to the left side of the subsystem. A new port, In2, appears on the block.
Add another output to the subsystem. Add a To Workspace block to the model and place it to the right of the subsystem. Drag a line from the input port to the right side of the subsystem. A new port, Out2, appears on the block.
Open the subsystem and rename the Out1 block to Scope. Rename the Out2 block to Workspace. Add a Manual Switch block to the model. Resize it and connect it as shown. Branch the signal after the Gain block to direct the output to the To Workspace block.
Then, return to the top level of the model. The figure shows the current model.
Simulate the model.
appear in the MATLAB workspace. You can double-click each variable
to explore the results.
If you want to use the second sine wave as input to the subsystem algorithm, open the subsystem and double-click the switch. The input changes to In2. Simulate again.
Tip: To toggle between simulating the model with and without the effects of the Bias block, right-click the Bias block and select Comment Through. The block stays in the model but does not affect the operation. Right-click the Bias block and select Uncomment to enable the block. The Comment Out command comments out the block's output signal, so signal data does not pass through. Try each of these commands to better understand their effects.