Defining Block Parameters Using MATLAB Variables
Exchanging Signals With MATLAB
Extracting Models From Simulink Into MATLAB
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 ----- sSuppose 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.
numEnter the following into the Denominator field.
K=5Start 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.
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.
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.
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 = 0This 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.0000To 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.