You are on page 1of 21

MEC522/FKM-UiTM/OCT2014

Introduction to MATLAB and MATLAB/Simulink


By S. Kushairi and M. A. Azizan
PART I: MATLAB
Introduction
MATLAB is a very useful tool in the wide field of control engineering. These days, it is
indispensable to engineers and researchers alike.
This tutorial aims to provide an introduction to students without prior experience to
MATLAB by covering each step in detail. The MATLAB version used is R2011b (v7.13).
Starting the Program
Locate the desktop shortcut or search in the computers list of installed programs and click
the icon to run MATLAB. The following window should appear:

2
4

6
3

Figure 1: The main window in MATLAB with labels for each partition.
Where;
1. Menu bar: This is where you can save, undo, create new script or function, etc.
2. Current folder: The directory where your MATLAB files (.m) or Simulink files
(.mdl) is saved or opened for the current session.
1

MEC522/FKM-UiTM/OCT2014

3. Details: A description of your file will be displayed here (when the individual file in
the current folder partition is highlighted).
4. Command window: This part will display the operation(s) performed by MATLAB,
including matrices, calculations, errors, warnings, etc. You can also define variables
and type some commands here, which is effective only for the current session.
5. Workspace: This space shows the stored data/variables defined in the current session.
They can be saved as well as imported.
6. Command history: As the name suggests, it displays the commands ran at previous
sessions. Useful in recalling the commands used previously. They can be deleted to
save some clutter when no longer required.
Creating and Running a Script
In the menu bar, click the New script (Ctrl+N) icon or File > New > Script. A new window
should pop-up:

Figure 2: MATLAB script editor with labels for each section.


Where;
1. Editor menu bar: Same as previous menu bar, you can save, undo, create new script
or function, etc. here.

MEC522/FKM-UiTM/OCT2014

2. Text editor: This is where you define all variables and type all commands to be used
(programming). The file must be saved before it can be run.
3. Text checker: Fully automated, it checks for any error real-time as you move on to
type a new line.
In order to run a script, look for the green Save and run (F5) icon in the editor menu bar
and click it. Alternatively, you can also click Debug > Save File and Run.
Example 1: Obtaining Cascaded, Parallel and Feedback (Closed-loop) Transfer
Functions [1]
Consider the following transfer functions:
G1 ( s)

10
num1
,

s 2s 10 den1
2

G2 ( s)

5
num 2

s 5 den 2

Figure 3: (a) cascaded/series, (b) parallel and (c) feedback (closed-loop) systems.

MEC522/FKM-UiTM/OCT2014

Type the following in the text editor (new script):

Figure 4: MATLAB program for Example 1.


Save (compulsory in order to run the script), and then run the script. Name the file without
any spaces to avoid possible errors on execution.
The following answers should be displayed in the command window:
(a) G( s)

50
s 7 s 20s 50
3

5s 2 20s 100
(b) G( s) 3
s 7 s 2 20s 50
(c) G( s)

10s 50
s 7 s 2 20s 100
3

Note that the command printsys(num,den) displays the transfer function (num/den) of
the system considered.

MEC522/FKM-UiTM/OCT2014

Example 2: Transformation from Transfer Function to State-space Representation [1]


Consider the transfer function system:
Y ( s)
s

2
U (s) ( s 10)(s 4s 16)
s
3
2
s 14s 56s 160

Type the following in the text editor (new script):

Figure 5: MATLAB program for Example 2.

MEC522/FKM-UiTM/OCT2014

MATLAB will produce the matrices A, B, C and D (look in the command window):

14 56 160
A 1
0
0
0
1
0
1
B0
0
C0 1 0
D0
The state-space representation can then be written as follows:
x1 14 56 160 x1 1
x 1
0
0 x2 0 u
2
x3 0
1
0 x3 0
x1
y 0 1 0 x2 0 u
x3

Note that the state-space representation for any system is not unique. There are infinitely
many state-space representations for the same system. The MATLAB command gives one
possible such state-space representation.

MEC522/FKM-UiTM/OCT2014

Example 3: Transformation from State-space Representation to Transfer Function [1]


Consider the following state-space equations:
1
0 x1 0
x1 0
x 0
0
1 x2 25 u
2
x3 5 25 5 x3 120
x1
y 1 0 0 x2
x3

Type the following in the text editor (new script):

Figure 6: MATLAB program for Example 3.


The answer yielded by MATLAB (in the command window) will be:

Y ( s)
25s 5
3
U (s) s 5s 2 25s 5
Note that the percentage symbol % is used to insert comments/explanations. MATLAB will
ignore the line(s) that begins with such symbol.
The semicolon ; at the end of each line is used to suppress output in the command window.
7

MEC522/FKM-UiTM/OCT2014

PART II: MATLAB/Simulink


Introduction
MATLAB/Simulink is a graphical tool in MATLAB, used for modelling and simulation of
(control) systems. Its main task is to solve ordinary differential equations (ODE) numerically
[2]. It helps engineers and researches alike to visualise their system.
The basics of using MATLAB/Simulink to model and simulate a system will be covered in
this tutorial.
Starting the Program
Open MATLAB (main window) and locate the Simulink icon in the menu bar.
MATLAB/Simulink should load:

Figure 7: MATLAB/Simulink Library Browser with labels for each division.


Where;
1. Library Browser menu bar: Here is where you can create or open Simulink files
(.mdl), other than setting display preferences.

MEC522/FKM-UiTM/OCT2014

2. Library tree: Libraries available for use is listed here, where they are further divided
into subcategories when expanded.
3. Graphical display: Simulink blocks available for use are displayed here as a
subcategory (any) is selected.
The main elements used in MATLAB/Simulink are blocks and lines. Blocks have the
function to generate, modify, combine, output and display signals [3]. Lines on the other
hand, are simply used to transfer signals from one block to another [3].
Creating and Simulating a Model
In the Library Browser menu bar, click the New model icon or File > New > Model. A
new window will open:

Figure 8: Simulink model editor/simulator with labels.


Where;
1. Model menu bar: You can perform operations for the current session/model such as
save, undo, print, start simulation, set preferences, etc. here.
2. Modelling space: This space is the drawing canvas for your model. It is where you
create and visualise your model using Simulink blocks.
To begin a model simulation, enter the simulation stop time desired (default is 10 seconds) in
the model menu bar and press the Start simulation button (two buttons left). Simulation >
Start also does the same thing.

MEC522/FKM-UiTM/OCT2014

Example 1: Multiplied Sinusoidal Waves [3]


This simple example demonstrates the result of continuous sinusoidal waves after being
multiplied.
To start building the system, insert Sine Wave (a type of sources), Gain and Scope (a
type of sinks) blocks and connect them as follows:

Figure 9: The Simulink model for the current example.


The blocks can be connected by dragging the arrow head to each other.
Edit the properties of the Sine Wave block (double-click on the block or Right-click > Gain
Parameters) and enter its amplitude as 1, frequency as pi and phase as pi/2. Leave
the sample time as zero for a continuous signal.
Now set the gain value to 5 for the Gain block.
Start the simulation. It will be done almost instantaneously since it is a simple system.
Double-click the Scope block to view the result:

Figure 10: Multiplied sinusoidal waves generated by the model after auto-scaled.
10

MEC522/FKM-UiTM/OCT2014

You might need to click the Autoscale button (a black pair of binoculars icon) in the scope
window to get the exact scale as Figure 10.
For comparison purposes, you would want to add another scope to the system:

Figure 11: The same Simulink model with another scope added.
You can now compare between the original signal and the multiplied one by viewing both
Scope and Scope1 side by side.
Modify the gain and see what happens. Remember to re-run the simulation each time you
modify the gain value.
Note that the functions of each block are explained in its properties. Double-click (or Rightclick > Gain parameters) the block to edit its properties.

11

MEC522/FKM-UiTM/OCT2014

Example 2: Open-Loop Response of a Quarter Bus Suspension Model [4]


A quarter model is used to simplify a bus suspension system to a one-dimensional, multiple
spring-damper system:

Figure 12: A schematic representing the quarter bus suspension model.


It has the following system parameters:
(m1)
(m2)
(k1)
(k2)
(b1)
(b2)
(u)

body mass
2500 kg
suspension mass
320 kg
spring constant of suspension system
80,000 N/m
spring constant of wheel and tire
500,000 N/m
damping constant of suspension system
350 N.s/m
damping constant of wheel and tire
15,020 N.s/m
control force = force from the controller we are going to design

The following statement describes the design requirements for this particular system:
A good automotive suspension system should have satisfactory road holding
ability, while still providing comfort when riding over bumps and holes in
the road. When the vehicle is experiencing any road disturbance (i.e. pot
holes, cracks, and uneven pavement), the vehicle body should not have large
oscillations, and the oscillations should dissipate quickly. Since the
distance X1-W is very difficult to measure, and the deformation of the tire
(X2-W) is negligible, we will use the distance X1-X2 instead of X1-W as the
output in our problem. Keep in mind that this is an estimation.
The road disturbance (W) in this problem will be simulated by a step input.
This step could represent the vehicle coming out of a pothole. We want to
design a feedback controller so that the output (X1-X2) has an overshoot
less than 5% and a settling time shorter than 5 seconds. For example, when
the vehicle runs onto a 10 cm high step, the vehicle body will oscillate
within a range of +/- 5 mm and return to a smooth ride within 5 seconds.

12

MEC522/FKM-UiTM/OCT2014

Create a script that defines the system parameters, i.e. m1, m2, k1, k2, b1 and b2. We will
be using this (saved) script in order to run the simulation.
The integrals of the masses accelerations will be modelled first:
d 2 x1
dx
dt 2 dt dt1 dt x1
d 2 x2
dx
dt 2 dt dt2 dt x2

Insert the Integrator blocks (drag them from the Library Browser or Right-click > Add to
untitled) in the modelling space (new model) as follows:

Figure 13: The blocks correspond to the integral equations.


The labels on top of each arrow can be added by double-clicking the empty space above the
respective arrows.
Next, the equations of motion will be modelled:

d 2 x1
1
F

M1 1
dt 2
1
M2

2 F

d 2 x2
dt 2
13

MEC522/FKM-UiTM/OCT2014

Insert two Gain blocks and two Sum blocks to represent the equations:

Figure 14: Simulink block representation for the equations of motion.


The labels below each block can be renamed by clicking on the name underneath the
respective blocks.
Double-click on Mass 1 block (or Right-click > Gain Parameters) to edit its properties.
Change the gain value to 1/m1 and 1/m2 for Mass 2 block.
You can resize the Simulink blocks by clicking the respective block to highlight it and drag
one of the corners to the desired size.
Change the signs in the Sum block to +-- and Sum1 block to ++-++.

14

MEC522/FKM-UiTM/OCT2014

We will now model the forces acting on each mass, starting with the first spring (k1). Its
force is equal to k1(X1-X2) refer Figure 12.
Insert a Sum block to connect the x1 and x2 signals. Remember to change the signs to
+- beforehand:

Figure 15: Simulink suspension model with the first spring added.
Insert a Gain block and edit its value to k1. Also, label this block as Spring 1.
To flip a block: Right-click > Format > Flip Block.
Connect the input and output of the Spring 1 block exactly as shown in Figure 15.

15

MEC522/FKM-UiTM/OCT2014

Next is the suspensions damper b1. Its force is equal to b1(V1-V2) refer Figure 12.
First, insert a Sum block as shown in the figure below:

Figure 16: Simulink suspension model with the first damper added.
Flip the Sum block left-to right after labelling it Sum5. Its signs should also be +-.
The input for Sum5 block is tapped from v1 (positive sign) and v2 (negative sign) line.
This is achieved by dragging a line from the input of Sum 5 block to the respective v1
and v2 lines.
Insert a Gain block and edit its value to b1. Also, label this block as Damper 1 and flip
it left-to-right.
Connect the input and output of the Damper 1 block exactly as shown in Figure 16.

16

MEC522/FKM-UiTM/OCT2014

The force from the second spring (k2) will now be added. Its force is equal to X2-W (refer
Figure 12).
Insert a Step block (a type of sources) and label it W as follows:

Figure 17: Simulink suspension model with the second spring added.
For the W block, change the value of the Step time and Final value to zero. This is to
assume that the road is flat.
Insert yet another Sum block and label it Sum3. Connect its input as shown in Figure 17.
The output of Sum3 block connects to another Gain block. This block should have k2
as the value and labelled Spring 2.
Proceed to connect the Spring 2 block to the fourth (positive sign) input of Sum1 block.

17

MEC522/FKM-UiTM/OCT2014

It is now time to add the second damper b2 to the model. Based on Figure 12, this damper
has a force of b2(dW/dt).
Locate a derivative block in the Library Browser and insert it as in the figure below:

Figure 18: The use of Derivative block in the current model.


Insert a final Sum block (labelled Sum4) and connect it as shown in Figure 18.
Connecting the output of this block requires the insertion of a Gain block containing the
value b2 and labelled Damper 2.
After achieving the exact state as in Figure 18, you can proceed to the final part: the input
force U between the two masses (refer Figure 12).

18

MEC522/FKM-UiTM/OCT2014

Insert another Step block and label it U, like so:

Figure 19: The completed Simulink model of a quarter bus suspension.


Change the value of the Step time to zero for the U block. Leave the Final value as
1.
Connect the output of the U block to the Sum (positive sign) and Sum1 (negative sign)
blocks.
The final block to be added is the Scope block (a type of sinks). Insert it to the model and
make sure that your completed model looks exactly as in Figure 19.
Now comes the simulation part (open-loop). Run the previously saved script containing the
system parameters.
Enter 50 in the stop time field, located in the model menu bar. This will cause
MATLAB/Simulink to stop simulation after 50 seconds, which is appropriate to see the openloop response of this model.
Start simulation and wait until it is completed.

19

MEC522/FKM-UiTM/OCT2014

Double-click the Scope block and the following graph should open up:

Figure 20: Open-loop response of the model after auto-scaled.


Click the Autoscale button (a black pair of binoculars icon) in the scope window to get the
exact scale as Figure 20.
Note that MATLAB allows the output graph to be retouched cosmetically other than focusing
on certain parts of the graph.
Conclusion
It is hoped that this introductory tutorial provides students a basic but sufficient insight into
the uses of MATLAB and MATLAB/Simulink in control engineering, other than a strong
encouragement to explore MATLAB.

20

MEC522/FKM-UiTM/OCT2014

References
[1]

K. Ogata, Mathematical Modelling of Control Systems, in Modern Control


Engineering, 5th ed. New Jersey: Pearson, 2010, pp. 30-52.

[2]

O. Hinton, Chapter 5: Introduction to Simulink, class notes for EEE 305, School
of Electrical, Electronic and Computer Engineering, University of Newcastle Upon
Tyne, August 1, 2002.

[3]

R. D. M., Simulink Basics Tutorial, class notes for EECS 401, Department of
Electrical Engineering and Computer Science, University of Michigan, May 18,
2000.

[4]

B. Messner, D. Tilbury, R. Hill and J. D. Taylor. (2011). Suspension: Simulink


Modeling in Control Tutorials for MATLAB and Simulink (CTMS). University of
Michigan, MI, Carnegie Mellon University, PA and University of Detroit Mercy,
MO. [Online]. Available:
http://ctms.engin.umich.edu/CTMS/index.php?example=Suspension&section=Simu
linkModeling

21

You might also like