Monash University MEC3457
Department of Mechanical and Aerospace Engineering Computer Lab 1
Laboratory 1
Introduction to Transfer Functions in MATLAB
MATLAB is a powerful numerical analysis package that is often used by engineers and scientists in
research and teaching environments. One of the many functions MATLAB is capable of is the numerical
analysis of control systems. Throughout the course, we will use MATLAB to analyse transfer functions,
simulate the system response to various input signals of feedback systems and investigate system
stability and response parameters.
The purpose of this computer lab is to allow students of all engineering and scientific backgrounds to
familiarise themselves to the MATLAB environment, regardless of their previous experience with this
computing package. All the basic commands and functions are introduced in this computer lab, so that
you, as a student, have all the basic tools required to define transfer function models in MATLAB and
to numerically simulate their response to various inputs.
Learning Outcomes
By the end of this computer lab, you will have the knowledge and tools to perform the following tasks
in MATLAB:
• Define transfer function models, based on polynomial computation
• Derive the closed loop transfer functions and characteristic equations of simple feedback systems
• Simulate basic input signals and investigating the system response for simple transfer function
models
Laboratory Requirements
This laboratory constitutes 4% of your unit mark. Full marks will be awarded upon completion of all
exercises with correct answers. It is in your best interest to finish these questions and be marked off
early to guarantee your marks and avoid the last minute rush. No students will be marked after the
laboratory session has concluded.
It is up to you to decide how you want to code in MATLAB. We recommend you use one script file per
part in this computer lab, with each question separated by cells. This is achieved by
% script for computer lab 1
%% Question 1
clear all
clc
% your code for the Question 1
%% Question 2
% your code for the question 2
1
Monash University MEC3457
Department of Mechanical and Aerospace Engineering Computer Lab 1
Double percentage mark (%%) will separate your script into cells so you can run sections of code at a
time. To run code in a cell, press CTRL + Enter. If needed, type clc and clear all at the top of each cell to
clear previously defined variables from other cells.
When being marked by demonstrators, make sure you have the relevant variables unsuppressed
(remove ‘;’ at certain lines) so that demonstrators can see your answers.
1. Defining a Transfer Function
In order to define a transfer function in MATLAB, we use the following function:
sys = tf(num, den)
The code snippet will create a transfer function model within the MATLAB environment named sys,
where num is a vector of the coefficients of the transfer function’s numerator and den is the vector of
the coefficients of the transfer function’s denominator. The first element in the vector is the highest
order coefficient of s, with subsequent elements indicating coefficients of s in the descending order.
Later in the laboratory, we will see the benefits of defining transfer functions as a model in MATLAB.
For example, to define the following transfer function in MATLAB,
2
𝐺(𝑠) =
4𝑠 + 3𝑠 + 1
we type the following commands in the command window:
>> num = 2;
>> den = [4 3 1];
>> G = tf(num,den)
G=
2
---------------------
4 s^2 + 3 s + 1
Continuous-time transfer function.
Factorised Polynomials
There may be times where the numerator or denominator terms of a given transfer function are
factorised. Although factorised terms are useful for transfer function performance analysis, they cannot
be plugged directly into the tf() function. The polynomial must be fully expanded first. To perform
polynomial expansion in MATLAB, we use the convolution function;
poly = conv(poly1, poly2)
where poly is a vector of polynomial coefficients of the product of polynomials poly1 and poly2. Again,
the first value of each vector is the highest-order coefficient. For example, if we were to expand the
following factorised polynomial
2
Monash University MEC3457
Department of Mechanical and Aerospace Engineering Computer Lab 1
p(s) = (s + 1) (s2 + 5s − 2)
then we use the following code,
>> p = conv([1 1], [1 5 -2])
P =
1 6 3 -2
This implies that
(s + 1)(s2 + 5s − 2) = s3 + 6s2 + 3s - 2.
We can then use this to define transfer functions as per normal.
Exercise 01
Define the following transfer functions in MATLAB:
a). 𝐺(𝑠) =
b). 𝐺(𝑠) =
c). 𝐺 (𝑠 ) = ( )( )
𝑠(𝑠 + 2)
d). 𝐺(𝑠) =
(𝑠 + 2) (𝑠 + 5)2
( )
e). 𝐺 (𝑠 ) = ( ) ( )( )
3
Monash University MEC3457
Department of Mechanical and Aerospace Engineering Computer Lab 1
2. Simulating System Response
Consider the following open loop transfer function
𝐺(𝑠) = (1)
We want to find the open loop response of G(s) with unit step, impulse and unit ramp inputs.
2.1. Unit Step Input via step()
We can use the function step() to plot the system response of a system subject to a unit step input. We
use the following code,
step(sys, tfinal)
where inputs sys and tfinal are the transfer function of the system and final time to be simulated,
respectively. This will automatically plot the output that is fully annotated in a suitable time resolution.
However, if you wish to generate the plot manually, you can define the outputs for the step() function,
i.e.
[y, t] = step(sys, tfinal)
The outputs y and t are vectors representing the output magnitude of the system and the time vector
respectively. You can then use plot(t, y) to plot the output manually.
To plot the system response of G(s) to a unit step expressed in Equation (1) over 10 seconds, we type
the following commands into the window
>> G = tf([4 5], [1 5 8])
G=
4s + 5
----------------------
s^2 + 5 s + 8
Continuous-time transfer function
>> step(G,10)
If you wish to control the time resolution of your plots, you may define a time vector, t, from 0 to 10
with 0.1 intervals and replace tfinal with t. Here, you should carefully choose the time interval. If the
time interval is too big then the graph will look wriggle. In that case, you should decrease the time
interval, possible by 10 times.
>> G = ft([4 5], [ 1 5 8]);
>> t = 0 : 0.1: 10; % time vector with 0.1 intervals
>> step(G, t)
4
Monash University MEC3457
Department of Mechanical and Aerospace Engineering Computer Lab 1
2.2. Impulse Input via impulse()
Like step(), we can utilise the function impulse() in exactly the same way to plot the system response of
a system subject to an impulse input. For G(s) defined in Equation (1), this is achieved by the following
code
>> G = tf([4 5], [1 5 8] );
>> impulse(G, 10) % plot impulse response over 10 sec
For manual control over the output data y or time samples, the same method applies as seen for step().
2.3. Ramp Input via lsim()
For obvious reasons, step() and impulse() can only be used for simulating their respective inputs.
Unfortunately there is no ramp() function for simulating a ramp signal for a transfer function model.
Instead, we use lsim(), a generalised linear simulation function that allows simulation of any input! The
only caveat is that we must define the input and time vectors ourselves. To achieve this, we can use the
following code
>> G = tf([4 5], [1 5 8] );
>> t = 0:0.1:10; % time vector at interval of 0.1s
>> u = t; % input vector
>> x0 = 0; % initial condition
>> lsim(G, u, t, x0)
Note that t, the time vector, has an interval of 0.1, which is sufficient for most situations. However, if
you were to plot over, say, 1 second, then an interval of 0.01 might be more sufficient. A unit ramp
input follows a magnitude of 1 every second, hence the reason why we set u = t. The function lsim()
accepts the transfer function sys, the input vector u and the time vector t in that order. The final input
is the initial condition x0, which we assume to be 0 unless stated.
Exercise 02
For the transfer function used in the example
4𝑠 + 5
𝐺(𝑠) =
𝑠 + 5𝑠 + 8
a. Obtain all the plots for the unit step, impulse and unit ramp inputs.
b. Plot the system response for a unit step input using lsim() over the same time period. Choose a
suitable time resolution to ensure a smooth plot.
c. Plot the system response when the input is oscillatory, u(t) = sin(10 t). Choose a suitable time
resolution.
5
Monash University MEC3457
Department of Mechanical and Aerospace Engineering Computer Lab 1
3. Closed Loop Transfer Functions
Consider the following block diagram:
Figure 1: A block diagram of a unity feedback system.
where K1 is a constant gain, G1 and G2 are open loop transfer functions and R and C are the input and
output ports of the system respectively. This is an example of a feedback system, signified by the output
signal being subtracted from the input signal at the negative summing junction between R and K1. The
aim of a feedback system is to maintain a desired output by minimising the error between the input
signal and the observed output. This is contrary to an open loop system observed in part 2 of this
computer lab where the output is unregulated.
In order to model the system response of this system at C to a given input signal at R, we must use the
closed loop transfer function (CLTF). This is a transfer function that relates the output signal to the
input signal as a ratio. In class, we would normally use the following equation
( ) ( )
= (2)
( ) ( ) ( )
where G(s) is the transfer function of the entire top branch of the feedback loop and H(s) is the transfer
function along the feedback branch. In this case, G(s) can be calculated and defined in MATLAB as
>> K1 = 2.8;
>> G1 = tf([1 3], [1 1]);
>> G2 = tf([1 2 ], [1 6 2]);
>> G = K1*G1*G2
G =
2.8 s^2 + 14 s + 16.8
-------------------------------------
s^3 + 7 s^2 + 8 s + 2
Continuous-time transfer function
Notice how transfer function models can be multiplied together as if they were standard variables.
MATLAB handles transfer function arithmetic like standard arithmetic for standard variables, hence
you can multiply, add, subtract or even divide transfer function blocks conveniently without additional
code.
6
Monash University MEC3457
Department of Mechanical and Aerospace Engineering Computer Lab 1
Now that we have the top branch transfer function G(s) defined and calculated in MATLAB, Equation
(2) requires the transfer function in the feedback branch also to be defined. Because this is a unity
feedback system where there is no transfer function in the feedback branch, H(s) = 1.
Finally, to get the closed loop transfer function of the block diagram in Figure 1, we use the MATLAB
function
sys = feedback(G, H)
Note that by default, it assumes that the system is feedback. For feedforward loops (positive feedback
loops), use the following code
sys = feedback(G, H, 1)
where the 1 signifies positive feedback.
And so to calculate the closed loop transfer function in MATLAB for Figure 1,
>> H = 1;
>> sys = feedback (G, H)
sys =
2.8 s^2 + 14 s + 16.8
----------------------------------------------
s^3 + 9.8 s^2 + 22 s + 18.8
Continuous-time transfer function
Therefore the CLTF for the block diagram in Figure 1 is
( ) . .
= (3)
( ) . .
where we have simplified its block diagram to
Figure 2: The equivalent block diagram of the system shown in Figure 1.
7
Monash University MEC3457
Department of Mechanical and Aerospace Engineering Computer Lab 1
3.1. Characteristic Equations
The characteristic equation of a closed loop transfer function is given by the denominator polynomial.
By studying the characteristic equation’s roots, we can get a fair idea about the output characteristics
of the system it represents. For example, it can tell us if the system output is stable for a given input
signal, its rise and peak times and its overshoot and oscillations, if they exist. To extract the
characteristic equation (denominator) of a transfer function model in MATLAB (using the CLTF sys
from the previous example), we simply type in the command window
>> ce = sys.den{:}
ce =
1.0000 9.8000 22.0000 18.8000
where this vector represents the coefficients of the polynomial in descending order of s-terms (as
observed as the denominator in Equation (3)). We can then use MATLAB tools such as roots(ce) to
determine the roots of the system so we can identify the output response parameters in detail.
Exercise 03
a. Find closed loop transfer functions (if they are not already CLTFs) and characteristic equations of
the following systems
( ) ( )
i. =
( ) ( )( )
ii.
b. The system below features the open loop transfer function defined in the example in part 2, but
placed in a negative feedback loop.
Find the closed loop transfer function and characteristic equation, along with the closed loop
system response plots, subject to the following inputs:
i. r(t) = 1 (unit step)
ii. r(t) = δ (impulse)
iii. r(t) = t (unit ramp)
iv. r(t) = sin(10t) (sinusoidal signal)
Compare these plots to the open loop response as found in part 2.