Simulink Basics Tutorial - Interaction With MATLAB

We will examine three of the ways in which Simulink can interact with MATLAB.
• Block parameters can be defined from MATLAB variab.
• Signals can be exchanged between Simulink and MATLAB.
• Entire systems can be extracted from Simulink into MATLAB.

## Block Parameters from MATLAB Variables

Often, a controller will be designed in MATLAB and verified in a Simulink model. Normally, numerical parameters such as gains and controller transfer functions are entered into simulink manually by entering the numbers in the block dialog boxes. Rather than enter numbers directly, it is possible to use MATLAB variab in Simulink block dialog boxes.

In this case, the complete controller transfer function is:

```                    s+2
2.5 -----
s
```
Suppose this transfer function were generated by some computation in MATLAB. In this case, there would most likely be three variab, the numerator polynomial, the denominator polynomial, and the gain. Enter the following commands in MATLAB to generate these variab.
```K=2.5
num=[1 2]
den=[1 0]
```
These variab can now be used in the blocks in Simulink. In your simulink model, double-click on the Gain block. Enter the following in the Gain field.
```
K
```

Close this dialog box. Notice now that the Gain block in the Simulink model shows the variable K rather than a number.

Double-click on the PI Controller block. Enter the following into the Numerator field.
```num
```
Enter the following into the Denominator field.
```den
```

Close this dialog box. Notice now that the PI Controller block shows the variab num and den (as functions of s) rather than an explicit transfer function.

You can simulate the model with the MATLAB variable parameters. Select Start from the Simulation menu to run the simulation. Double-click on the Scope block to view its output. Hit the autoscale button (binoculars) and you should see the following.

Now, if any calculations are done in MATLAB to change any of the variab used in the Simulink model, the simulation will use the new values the next time it is run. To try this, in MATLAB, change the gain, K, by entering the following at the command prompt.
```K=5
```
Start the Simulink simulation again, bring up the Scope window, and hit the autoscale button. You will see the following output which reflects the new, higher gain.

## Exchanging Signals with MATLAB

Sometimes, we would like to use the results of a Simulink simulation in the MATLAB command window for further calculations and plotting. Less often, we would like to generate signals in MATLAB which we then use as inputs in a Simulink model. These tasks are accomplished through the use of the To Workspace Sink Block and the From Workspace Source Block. We will only transfer signals from Simulink to MATLAB. Doing the reverse is a very similar process.

The To Workspace Sink Block saves a signal as a vector in the MATLAB Workspace. Open the model which you used previously in this tutorial or click here to download the model. Be sure that the variab K (=5), num (=[1 2]), and den (=[1 0]) are defined in MATLAB.
Suppose we would like to use both the output signal and the control signal for calculations in MATLAB. We will save these two variab as well as a time signal from our Simulink model. First, you need to generate a time signal. Open the Sources window by double-clicking the Sources icon in the main Simulink window. Drag the Clock block from the Sources window to the lower portion of your Simulink model.

Now, open the Sinks window and drag three instances of the To Workspace block to your Simulink window, arranged approximately as shown below.

Before connecting these blocks to the rest of your system, first you will name the variab to which they output. The lower To Workspace block will output the time signal to the MATLAB variable t. Double-click on this block and enter the following in the Variable Name field.

```
t
```

Close the dialog box. Notice that the lower To Workspace block shows a t.
The To Workspace block near the Plant block will output the control signal to the MATLAB variable u. Edit this block to output to the variable u. The last To Workspace block will output the output signal to the MATLAB variable y. Edit this block to output to the variable y. Also, for better clarity, change the labels (by clicking on the exiting labels "To Workspace") of these blocks to "time", "control", and "output".

Now, you will connect these blocks to the rest of your system. Draw a line from the Clock block to the time (t) block. Tap a line off of the control signal (the line between the PI Controller block and the Plant block) and connect it to the control (u) block. Remember, to tap off an existing line, hold the Ctrl key while drawing the line. Tap a line off the output signal line (the line which enters the Scope block) and connect it to the output (y) block. Your system should appear as follows.

Start the simulation (Start from the Simulation menu). You can still view the output in the Scope window (remember autoscale).

You can now examine the outputted variab in the MATLAB window. Plot u and y vs. t by entering the following command.
```plot(t,u,t,y);
```
Note that it is important to plot each of these variab against the time vector generated by Simulink, since the time between elements in the signal vectors u and y may be unequal, particularly near a discontinuity such as the step input. Your plot of u (blue) and y (green) should appear as follows.

## Extracting Models From Simulink into MATLAB

Sometimes, we may build a complicated model in simulink and would like to derive either a transfer function or a state space model of the entire system. In order to do this, you first need to define the input and output signals of the model to be extracted. These virtual signals can be any signal in a model, for example, if we can generate an input-to-output transfer function or a disturbance-to-error transfer function. These signals are defined using the In and Out Connection Blocks.

Once the input/output model is defined, the Simulink model must be saved to a .mdl file. This file is then referenced in the MATLAB command window by the linmod command.

To demonstrate this, bring up your model from the previous section of this tutorial (or click here to download it). Be sure that the variab K (=5), num (=[1 2]), and den (=[1 0]) are defined in MATLAB.

You will be extracting a closed-loop reference-to-output model. Therefore, The virtual input will be put in place of the step input to the system. First, delete the Step block (click on it and hit the delete key). The previous line will remain with an an open input terminal where it used to connect to the Step. Open the Connections window from the main Simulink window. Drag an In Block from the Connections window to your model window in place of the Step block you just deleted. Move the In block until the output terminal of the In block touches the open input terminal of the left over line. The line should attach to the In block.

The virtual output does not need to replace an existing block - the signal can be tapped off an existing line. Drag an Out block from the Connections window and place it just above the Scope block. Tap a line off the output signal (hold Ctrl) and connect it to the out block.

Now, save this model under a new name. Call it mymodel.mdl. You can download a version here.

At the MATLAB prompt, enter the following command to extract a state-space model from your model file.

```[A,B,C,D]=linmod('mymodel')
```
You should see the following output.
```A =

-2    -9     2
1     0     0
0    -5     0

B =

5
0
5

C =

0     1     0

D =

0
```
This can, of course, be converted to a transfer function with the following command.
```[numcl,dencl]=ss2tf(A,B,C,D)
```
You should get the following output.
```numcl =

0         0    5.0000   10.0000

dencl =

1.0000    2.0000    9.0000   10.0000
```
To verify that the model transfered properly, you can obtain a step response of the extracted model.
```step(numcl,dencl)
```
You should see the following plot which is similar to the previous Simulink Scope output.

Tutorials
MATLAB Basics | MATLAB Modeling | PID | Root Locus | Frequency Response | State Space | Digital Control | Simulink Basics" | Simulink Modeling | Examples