Вы находитесь на странице: 1из 8

ECE 351 Linear Systems II

MATLAB Tutorial #5

Modeling Discrete Time Systems in Simulink


This tutorial describes the use of Simulink, a graphical user interface within MATLAB. Using
Simulink, the block diagrams of discrete time systems may be entered graphically into
MATLAB and analyzed.
Getting Started with Simulink
In this section we will illustrate a very simple use of Simulink to display the sum of two
sequences.
1. Open MATLAB and in the command window, type: simulink at the prompt. Alternatively,
there is a Simulink icon in the menu bar.
2. After a few seconds Simulink will open and the Simulink Library Browser will open as
shown in figure 1. It is important to note that the list of libraries may be different on your
computer. The libraries are a function of the toolboxes that you have installed. At a
minimum you should have the Simulink and Simulink Extras libraries on your list.

Figure 1. Simulink Library Browser

3. Click on the Create a New Model icon in the Library Browser window. An additional
window will open. This is where you will build your Simulink models.
4. Click on the + sign next to Simulink in the Library Browser. A list of sub-libraries will
appear including Continuous, Discrete, etc. These sub-libraries contain the most common
Simulink blocks.
5. Click once on the Sources sub-library. You should see a listing of blocks as shown in the
right column of figure 2.

Figure 2. Source Blocks in the Simulink Library


6. Drag the simin block from the Simulink: Sources library into the model window. This source
block is used to bring data from the MATLAB workspace into the Simulink environment.
7. Return to the MATLAB command window. Type the following commands to generate two
discrete time sequences, x1 and x2.

>>
>>
>>
>>

clear
k=0:10;
x1=(1/2).^k;
x2=(-1/2).^k;

8. In order to transfer the sequence x1 into the Simulink model, double click on the simin block
and enter the values as shown in figure 3. The data field input is a matrix with the time or
sequence index (column 1) and values (column 2) vectors that were set up in the workspace.
Additionally, the time sampling rate that was used to generate these values is entered. Note
that the k and x1 vectors are converted to column vectors in the parameters window.

Figure 3. Parameters for simin Block


9. Add a second simin block to the workspace and apply the sequence x2 to this input.
10. Open the Math Operations Library and drag the sum block into the model window. The
model window should now appear similar to the one shown in Figure 4.

Figure 4. Simulink Model Window


11. Next we want to connect the inputs to the summer. Move the mouse over the output terminal
of the top simin block until it becomes a crosshair. Click and drag the wire to one of the
summer input terminals. The cursor will become a double cursor when it is in the correct
position. Release the mouse button and the wire will snap into place. Repeat to connect the
second simin block to the second summer input. Your model should now appear as shown in
figure 5.

Figure 5. Simulink Model Window


12. In order to display the sum of the two sequences we want to export the output of this model
back to the MATLAB workspace. Open the Sinks Library and drag the simout block to
model window. Connect to the output of the summer as shown in figure 6.

Figure 6. Simulink Model Window


13. Double click on the simout sink and change the save format selection to: Array.
14. Select Simulation Configuration Parameters from the model window. Note that the
default Stop Time is 10, which is fine for our example. Under Solver Options set the Type
to Fixed-Step and the Solver to Discrete. Set the Fixed Step Size to 1 to match the increment
between input values. Click OK and run the simulation by selecting Simulation Start.
15. Return to the MATLAB command window. Note that a sequence simout is now present in
the workspace. Plot the two inputs and the output with the following MATLAB commands:
>>
>>
>>
>>
>>
>>

subplot(3,1,1)
stem(k,x1)
subplot(3,1,2)
stem(k,x2)
subplot(3,1,3)
stem(k,simout)

Modeling a Discrete Time System in Simulink


1. Open a new Simulink model window. Input the diagram shown in figure 7. Note the
following:
a. The unit delay is found in the Discrete Library.
b. After dragging a summer to the model window, double click on its icon. Modify the List
of Signs in the window that opens to be: +- instead of the default ++.
c. The Gain block is found in the Math Operations Library. After dragging the Gain block
to the model window, select Format Flip block to change its orientation as shown.
d. Double click on the Gain block to change its gain to .

Figure 7. Discrete Time System


2. Return to the MATLAB command window and type the following commands to set the input
sequence to be a unit step sequence from 0 k 10.
>> clear
>> k=0:10;
>> x=ones(1,11);
3. Apply this input to the simin block as described in the previous section.
4. Set the simulation parameters as described in the previous section and run the simulation.
Again, the stop time should be 10. Return to the MATLAB command window and plot the
output sequence. Confirm that your output is as shown in figure 8 below.

Figure 8. Output of Discrete Time System


State Variable Representation of Discrete Time Systems
The discrete time system in figure 7 can be represented using the following state variable
equations:

qk +1 = Aqk + Bxk
yk = Cqk + Dxk
where
A=[-1/2]

B = [1]

C=[-1/2]

D = [1]

Note that in this simple case A, B, C, and D are simple 1x1 elements, but in general they will be
matrices. Before performing the steps below, DO NOT clear the MATLAB workspace as you
will continue to need the inputs and output found in the previous section.
1. From the MATLAB command window, enter the following commands to input these four
matrices:
>>
>>
>>
>>

A=[-1/2];
B=[1];
C=[-1/2];
D=[1];

2. Open a new Simulink window and create the model shown below in figure 9. Note that the
Discrete State-Space block is found in the Discrete Library.

Figure 9. State Variable Simulink Model


3. Double click on the simin block and apply the same step sequence input as used in the
previous example.
4. Double click on the Discrete State Space block and set the parameters as shown in figure 10
below. Note that rather than inputting the A, B, C, D matrices directly we are referring to
those set in the MATLAB workspace.

Figure 10. Discrete State Space Block Settings


5. Double click on the simout block and change the variable name to simout2 so that the
previous output will not get overwritten. Remember to set the save format as an Array rather
than the default, structure. Set the configuration parameters as described in the previous
example and run the simulation.
6. Enter the following MATLAB commands in the command window to compare the outputs of
the systems in figures 7 and 9.
>>
>>
>>
>>

subplot(2,1,1)
stem(k,simout)
subplot(2,1,2)
stem(k,simout2)

Hopefully you will observe that both outputs are the same and hence the systems are
equivalent.