Simulink Basics Tutorial - Interaction With MATLAB

Defining Block Parameters Using MATLAB Variables
Exchanging Signals With MATLAB
Extracting Models From Simulink Into MATLAB


We will examine three of the ways in which Simulink can interact with 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.

For example, bring up the Simulink model built in the Basics tutorial (or click here to download it.)

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. 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.
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. Enter the following into the Denominator field.
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. 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.

To download the model with MATLAB variable parameters, click here.

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

To download the model with outputs to MATLAB variab, click here.

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.

You should see the following output. This can, of course, be converted to a transfer function with the following command. You should get the following output. To verify that the model transfered properly, you can obtain a step response of the extracted model. 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