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 change the existing model configuration.
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 example, 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.
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.
Right-click the block and select Add block to model untitled. (To learn more about the block, select the Help command from the context menu.)
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.
Reading the model is easier when you line up the blocks according to how they interact with each other. Connect the blocks so the model can operate. 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 block dialog box to set parameters.
In your model, set the amplitude in the Sine Wave block and the value of the gain in the Gain block.
To open the Sine Wave block dialog box, double-click the block.
For the Amplitude parameter,
2 and click OK.
In the Gain block dialog box, set the Gain value
3. The value appears on the block.
In this model, you want to perform another gain but on the absolute value of the output from the Sine Wave block. Add more blocks to your model using additional techniques.
When 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.
For many blocks, a prompt appears for entering 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 has a number appended. The number increments for each new copy.
If you do not know the library a block is in or the
full name of the block, 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 model 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, you can use Edit > Copy and Edit > Paste.
The figure shows the current state of your model.
Note: When you name blocks in a model in order numerically (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. To number blocks so that copying and pasting them creates names that follow a typical reading order, 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.
In this figure, the horizontal alignment guide shows that the Scope1 and Gain1 blocks are already 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, Ctrl+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 to multiple inputs.
To approximate a diagonal line from line segments, Shift+drag a vertex.
You can group blocks in subsystems and label blocks, subsystems, and signals. For more about subsystems, see Create a Subsystem.
Drag a selection box around the Abs and Gain1 blocks.
Move your mouse over the prompt that appears at the lower-right corner of the selection box. From the action bar that appears, click 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, use the block handles.
Double-click the block name and type
Value to give the subsystem a meaningful name.
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 block.
Double-click the name Out1 and type
Click the Simulink Editor Up to Parent button to return to the top level of the model. The output port of the Absolute Value subsystem is now named To Scope1.
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
To Scope. Be sure to double-click
the signal and not a blank area of the canvas or you create an 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, rename its output port, and name a signal.
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, absolute value of the sine wave is always positive.
You can add blocks on an existing connection and remove blocks from models and redraw connections. Add a bias to the input to both branches of your model. Also, replace one of the scopes with a different sink.
Add a Bias block to the model and set the Bias parameter
Drag the block and place it on 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 its position.
Remove the Scope1 block. Shift+drag the Scope1 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.
When you delete a block with 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.
Rename the output port on the Absolute Value subsystem. Open the subsystem and rename the To Scope1 block as Workspace. Then, return to the top level of the model. The figure shows the current model.
Simulate the model. The
appears in the MATLAB workspace. You can double-click the variable
to explore the results.
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.