Creating transfer functions
In MATLAB, access to the Control Systems Toolbox allows one to create transfer functions, make feedback and system interconnections as well as attempt some block diagram algebra.
The basic command to create a system model is the tf
command which stands for transfer function. The signature of the tf
command is as follows (click on the link to see documentation):
sys = tf(numerator, denominator)
In the above numerator and denominator are MATLAB vectors that stand for the polynomial coefficients of the numerator and denominator of the transfer function respectively. That is, in other words, a poynomial like $ax^2+bx + c$ is represented as MATLAB vector [a, b, c]
.
Exercise 1
Create a transfer function model in MATLAB of:
$$G(s) = \dfrac{s+5}{2s^2+3s+4}$$
Click to reveal
This can be done as follows:
G = tf([1, 5], [2, 3, 4])
Questions:
How do you enter a factored polynomial? E.g. $(s+1)^2$?
True or false: The first element in the vector goes with the lowest degree term in the polynomial.
How do you enter a zero coefficient ?
Click to reveal
The polynomials have to be expanded out.
False
Any missing coefficients have to be explicitly zeroed out.
Block diagram algebra
MATLAB allows three basic types of system interconnections: feedback
, parallel
and series
.
Click on the links to see the documentation.
Exercise 2
Connect $G_1$ and $G_2$ in series:
$$G_1(s) = \dfrac{s}{2 s^2-5 s-3} \qquad G_2(s) = \dfrac{s-3}{s+1}$$
Is that the simplest form of the transfer function possible?
Hint: Check minreal
Click to reveal
This can be done as follows:
g1_num = [1, 0]
g1_den = [2, -5, -3]
g1 = tf(g1_num, g1_den )
g2_num = [1, -3]
g2_den = [1, 1]
g2 = tf(g2_num, g2_den)
sys_series = series(g1, g2)
However, that is not the simplest form. We can obtaine the simplest form using
minreal(sys_series)
Exercise 3
Connect $G_1$ and $G_2$ in parallel and simplify the transfer function as much as possible.
Click to reveal
Assuming the started code is already entered as above, we simply add:
sys_parallel = minreal(parallel(g1, g2))
Generating Bode plots (toolbox)
In MATLAB the transfer function model is useful for constructing Bode plots automatically. The function to use is called bode
and takes the signature
bode(sys)
or
[mag,phase,wout] = bode(sys)
The difference is that the former automatically creates a Bode plot for us using the the output variables where as the latter only outputs the variables and plotting is left to us.
Exercise 4
Construct the Bode plot of the feedback interconnection $G_1$ above with $G_2$ in the feedback loop.
Question: How do you make a unity feedback interconnection?
Click to reveal
Again, assuming g1
and g2
have been already created as transfer function models, this can be done as follows:
bode(minreal(feedback(g1, g2)))
Evaluating transfer functions at frequencies
Let us solve Problem 2 of Homework 7 in MATLAB. Remember that the system diagram we need to reduce is:

Click to reveal answer - simple way
This method allows you to verify your answers in MATLAB when solving things analytically.
top = tf([1], [1, 0]);
forward = parallel(top, 5);
lower = tf([1, 0], [1]);
sys = feedback(forward, lower);
syms s;
ssym_num = poly2sym(cell2mat(sys.Numerator), s);
ssym_den = poly2sym(cell2mat(sys.Denominator), s);
sys_sym = ssym_num / ssym_den;
sys_at5 = subs(sys_sym, 's', 1j*5);
M = abs(sys_at5);
phi = angle(sys_at5);
Click to reveal answer - advanced way
In this method we actually are simulating the system with a real input. The system output gets stored in the variable out
.
top = tf([1], [1, 0]);
forward = parallel(top, 5);
lower = tf([1, 0], [1]);
sys = feedback(forward, lower);
t = linspace(0, 2*pi, 100);
x = 5*cos(5*t-pi/6);
out = lsim(sys, x, t)