0% found this document useful (0 votes)
92 views

Lecture 6 - State-Space Systems in Matlab: (T) A (T) + B (T) X X U C + D y X U

The document discusses entering state-space systems into MATLAB and Python. It provides examples of defining the A, B, C, and D matrices for state-space models. One example models a submarine system, while another models a simple mass-spring-damper system to demonstrate defining the state-space matrices from the system's equations of motion. Guidelines are given for properly dimensioning the matrices based on the number of states, inputs, and outputs.

Uploaded by

Pythonraptor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views

Lecture 6 - State-Space Systems in Matlab: (T) A (T) + B (T) X X U C + D y X U

The document discusses entering state-space systems into MATLAB and Python. It provides examples of defining the A, B, C, and D matrices for state-space models. One example models a submarine system, while another models a simple mass-spring-damper system to demonstrate defining the state-space matrices from the system's equations of motion. Guidelines are given for properly dimensioning the matrices based on the number of states, inputs, and outputs.

Uploaded by

Pythonraptor
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 33

6/30/2020 Practicon Exercise System TU Delft

Lecture 6
ae2204 Aerospace Systems and Control Theory

With answers and notes by Anthony Cummins (4436253)

Lecture 6 -- State-Space systems in Matlab


Introduction
Hello, and welcome back to my e-lecture. In the (live) lecture 5, we discussed the use of state-space models
in control theory. State-space models are a natural fit to CACSD programs like Matlab, since these programs
are basically built around numerical algorithms for matrices.

One of the obvious advantages of a state-space representation is that it can describe systems with multiple
inputs and multiple outputs (MIMO). When working with Matlab or Python, a state-space description of a
system is often the preferred starting point for controller design and analysis. The system to be controlled is
modelled as a state-space system, and as additional control loops are tuned, the state-space system is
extended to include these control loops.

The first step is of course getting a system into Matlab or Python, and that is easiest when the system is
given.

Entering state-space systems


In Matlab, a state-space system can be created with the command ss. This command needs the four
matrices that define the state-space system, remember these?

˙
x (t) = A x (t) + B u (t)
− − −
y = C x + Du
− − −

Right, A, B, C and D.

OK, so let's enter a state-space system, and start with entering the a, b, c and d matrices. Matlab variant first:

a = [ 0 1 0 ; ...
-0.0071 -0.111 0.12; ...
0 0.07 -0.3]
b = [ 0 ; -0.095; 0.072]
c = [ 1 0 0 ]
d = [ 0 ]

Remember the three dots for Matlab ...? These indicate that the statement continues on the next line. Now
the Python variant:

https://practicon.lr.tudelft.nl/practicon/index.html?extauth=1 1/33
6/30/2020 Practicon Exercise System TU Delft

import numpy as np
from control.matlab import *

a = np.matrix(""" 0 1 0 ;
-0.0071 -0.111 0.12;
0 0.07 -0.3""")
b = np.matrix(""" 0 ; -0.095; 0.072""")
c = np.matrix(""" 1 0 0 """)
d = np.matrix(""" 0 """)

T
This, incidentally, is the state-space equation for a submarine. The state vector contains: x = [θ, q, α] .

Here θ is the pitch attitude of the submarine, q is the pitch rate (rotation rate around its y axis), α is the angle
of attack (angle between the nose and the velocity vector). The input vector has one element, the elevator
(control surface) deflection u = [δe ] . You can imagine that without this information, your set of state matrices

is essentially useless, so always document what you are doing!
Creating a state-space system is simple, and exactly the same for both Matlab and Python:

sys = ss(a, b, c, d)

As long as you do not specify a time step as 5th parameter, Matlab will assume that you are creating a
continuous-time system. If you enter a time step as 5th parameter, Matlab assumes you are entering a
discrete-time system. For a discrete-time system, the state equation changes to:

x ((k + 1)Δt ) = A x (kΔt ) + B u (kΔt )


− − −

With Δt as the time step for the discrete-time system. However, in this course we will only discuss
continuous-time systems.

Of course, the dimensions of the matrices you enter must fit; B must have as many rows as A, A must be a
square matrix, C must have as many columns as A, and the D matrix must have as many columns as B and as
many rows as C.

If you make a mistake there, Matlab will spit out something like the following:

>> d = [0 0] % intentional mistake


d =
0 0
>> sys = ss(a, b, c, d)
??? Error using ==> ltipack.ssdata.utCheckData
The column sizes of B and D must agree with the number of inputs.

Python gives you slightly more blurp:

>>> d = np.matrix(""" 0 ; 0 """)


>>> sys = ss(a, b, c, d)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "build/bdist.linux-x86_64/egg/control/matlab.py", line 447, in ss
File "build/bdist.linux-x86_64/egg/control/statesp.py", line 165, in __init__ ValueError: D m

https://practicon.lr.tudelft.nl/practicon/index.html?extauth=1 2/33
6/30/2020 Practicon Exercise System TU Delft

>

The fun of a state-space system begins when you have multiple inputs or outputs. Now we have attitude of
the submarine as output, but in this system, it is of interest also to look at the pitch rate, the angle of attack
and particularly the climb or descent angle, γ .

As with aircraft, the relationship between α , θ and γ is:

γ = θ − α

And so we can define a new output vector:

θ
⎡ ⎤

⎢ q ⎥
y = ⎢ ⎥
− ⎢ α⎥

⎣ ⎦
γ

For the last element in the output vector we use the relationship outlined above, and since θ and α are part of
the state vector, a new c matrix can be defined:

newc = [ 1 0 0; ... % theta


0 1 0; ... % q
0 0 1; ... % alpha
1 0 -1] % gamma = theta - alpha
newd = zeros(4, 1);
sys2 = ss(a, b, newc, newd)

In Python variant:

newc = np.matrix("1.0 0 0;" # theta


"0 1 0;" # q
"0 0 1;" # aplha
"1 0 -1") # gamma = theta - alpha
newd = np.zeros((4,1))
sys2 = ss(a, b, newc, newd)

A simple state-space system


Let's start simple, with creating a state equation for a mass-spring and damper system. Here is the system.
Now, what you need to do is find the differential equations for the following system of a mass, a spring and a
damper:

https://practicon.lr.tudelft.nl/practicon/index.html?extauth=1 3/33
6/30/2020 Practicon Exercise System TU Delft

Here the mass m is 3.5 [kg], the damping coefficient b = 9[Ns/m] and the spring coefficient is
k = 60[N/m] . The input is the force on the mass, as outputs the position y, the velocity dy/dt and the

acceleration d 2 y/dt2 (in that order) are required.

My suggested approach is to take a piece of paper, write down the differential equations governing this
system, convert them to a state-space form and enter that in Matlab or Python.

Note: to enter the numerical matrix values, enter them as you would in Matlab, or as you would enter them in
a Python matrix, e.g.

A = [ 1 0; 0 4]

or

A = [[1, 0], [0. 4]]

Your answer
Enter the A, B, C and D matrices of this state-space system
A = [ 0 1; -17.1428 -2.5714 ]

B = [ 0 ; 0.2857 ]

C = [ 1 0; 0 1; -17.1428 -2.5714 ]

https://practicon.lr.tudelft.nl/practicon/index.html?extauth=1 4/33
6/30/2020 Practicon Exercise System TU Delft

D = [ 0; 0; 0.2857 ]

The model answer


Enter the A, B, C and D matrices of this state-space system
A = [ -2.5714286, -17.142857;
1.0, 0.0 ]

B = [ 0.28571429;
0.0 ]

C = [ 0.0, 1.0;
1.0, 0.0;
-2.5714286, -17.142857 ]

D = [ 0.0;
0.0;
0.28571429 ]

Explanation / script

Used or unused hints


unused If you forgot about your vibrations ... 5 pts
Simple Newtonian mechanics tells you that:

ÿ m = F − bẏ − ky

Apparently you can calculate the acceleration of the mass. You can set this equal to the derivative of a
state vector element; ÿ = x˙1 , or ẏ = x1 . You can use the above equation for both the state equation,
and for the output equation.

https://practicon.lr.tudelft.nl/practicon/index.html?extauth=1 5/33
6/30/2020 Practicon Exercise System TU Delft

Now, at this point you have ẏ = x1 in the state vector. In the equation given above, you see that you
need y to calculate ÿ . And since you already have its derivative (ẏ = x1 ), you can simply add y = x2
to the state vector.

Feedback
Very good, a perfect score.
Simple Newtonian mechanics tells you that:

ÿ m = F − bẏ − ky

In order to put an element in the state vector x , it must be possible to calculate its derivative with the

elements in the state vector and the input vector. The velocity of the mass, ẏ can be put in the state vector
because we can calculate its derivative (i.e., the derivative of ẏ , being ÿ ) with the equation given above. This
calculation uses F (in the input vector), y and ẏ , that both must be in the state vector.

Choosing x1 = ẏ and x2 = y , we get

˙1
x −b/m −k/m x1 1/m
˙
x = [ ] = [ ][ ] + [ ] u
− −
˙2
x 1 0 x2 0

Required outputs are y, ẏ and ÿ , so the output equation becomes:

0 1 0
⎡ ⎤ ⎡ ⎤

y = ⎢ 1 0 ⎥ x + ⎢ 0 ⎥ u
− − −
⎣ ⎦ ⎣ ⎦
−b/m −k/m 1/m

Note that the last equation simply copies the first line of the state equation; acceleration of the mass is also
calculated there.

Note that this can be done in many more variations; x1 and x2 may be interchanged, and other variants are
possible.

The solution in Matlab then becomes (note that this is only one of the infinitely many example solutions
possible):

clear
% parameter values
m = 3.5
b = 9
k = 60
% matrices
A = [ -b/m -k/m; 1 0]
B = [ 1/m; 0]
C = [ 0 1; 1 0; -b/m -k/m]
D = [ 0; 0; 1/m]
% system
sys = ss(A, B, C, D)

In Python the process is not very different:

https://practicon.lr.tudelft.nl/practicon/index.html?extauth=1 6/33
6/30/2020 Practicon Exercise System TU Delft

import numpy as np
from control.matlab import *
# parameter values
m = 3.5
b = 9
k = 60
# matrices
A = np.matrix([[ -b/m, -k/m], [ 1, 0]])
B = np.matrix([[ 1/m], [ 0]])
C = np.matrix([[ 0, 1], [ 1, 0], [ -b/m, -k/m]])
D = np.matrix([[ 0], [ 0], [ 1/m]])
# to continue, and make the system
sys = ss(A, B, C, D)

Testing the system


At this point, let's play around a bit with the state-space system you entered in Matlab. In case you were
careless and already deleted your results, here it is again:

clear
% parameter values
m = 3.5
b = 9
k = 60
% matrices
A = [ -b/m -k/m; 1 0]
B = [ 1/m; 0]
C = [ 0 1]
D = [ 0 ]
% system
sys = ss(A, B, C, D)

For the Pythonistas:

import numpy as np
from control.matlab import *
# parameter values
m = 3.5
b = 9
k = 60
# matrices
A = np.matrix([[ -b/m, -k/m], [ 1, 0]])
B = np.matrix([[ 1/m], [ 0]])
C = np.matrix([[ 0, 1]])
D = np.matrix([[ 0]])
# system
sys = ss(A, B, C, D)

https://practicon.lr.tudelft.nl/practicon/index.html?extauth=1 7/33
6/30/2020 Practicon Exercise System TU Delft

You might notice that in comparison with the model answer for the previous question, the C and D matrices
are now only one row; this state-space system only has the position as output. This is quite common,
depending on what outputs you want to study, you can trim or adapt the C and D matrices.

You can get a response to an initial condition, an initial state of the state-space system. The state vector of
T
the model solution given above contains the velocity and position of the mass as states; x = [ẏ , y] . Try

to calculate and plot the response to an initial state of ẏ = 1 and y = 0, with the input F = 0. Calculate and
plot this response, plot only the position. Try to figure out the time step you need for this (look at the
eigenfrequencies, as discussed in the introduction).

If you look at the plot, you see that the system response oscillates several times before the output returns to
zero. The frequency of this oscillation is called the "damped eigenfrequency". Zoom in if necessary, and
determine the oscillation period of the system, i.e. the duration of a single oscillation.

Your answer
What is the oscillation period (in seconds) of the response? 1.596485

The model answer


What is the oscillation period (in seconds) of the response? 1.5964562
Explanation / script
%Q1
m = 3.5;

Used or unused hints


unused How to calculate an initial-condition response in Matlab or Python? 5 pts
You have already seen that lsim is the command to calculate a response. Matlab is nice enough to list
associated and similar commands towards the end of the help text. Try help lsim and look at what is
listed under ``See also''

When using python and control, the lsim command in the control.matlab module can do the trick. In this
case enter:

from control.matlab import *


help(lsim)

Feedback
Very good, a perfect score.
The command initial calculates the response to initial conditions. The first element of our example solution
for the state equation (in your solution it might be the second) contains the velocity.

This gives the system from the previous question, with only the position as output:

https://practicon.lr.tudelft.nl/practicon/index.html?extauth=1 8/33
6/30/2020 Practicon Exercise System TU Delft

clear
% parameter values
m = 3.5
b = 9
k = 60
% matrices
A = [ -b/m -k/m; 1 0]
B = [ 1/m; 0]
C = [ 0 1]
D = [ 0]
% system
sys = ss(A, B, C, D)

Now, with that, the initial response can be calculated with:

x0 = [1; 0]
t = 0:0.01:20;
y = initial(sys, x0, t);
plot(t, y)

Since you need the accuracy for your answer, pick a fairly precise time step. 0.01 [s] should do it.

The same thing in Python:

import numpy as np
from control.matlab import *
from matplotlib.pyplot import plot, show

# parameter values
m = 3.5
b = 9
k = 60
# matrices
A = np.matrix([[ -b/m, -k/m], [ 1, 0]])
B = np.matrix([[ 1/m], [ 0]])
C = np.matrix([[ 0, 1]])
D = np.matrix([[ 0]])
# system
sys = ss(A, B, C, D)

x0 = np.matrix([[ 1.0], [0.0]])


t = np.arange(0.0, 4.01, 0.01)
y = initial(sys, t, x0)
plot(t, y[0])
show()

Verify that what you see in the plot matches what you can expect from the assignment; you should be able to
see that the derivative is ẏ (0) = 1 (from the slope!), and y(0) = 0 .

Now read off the period. See the example plot for that (NOTE that this plot is only an example, it might have
been generated with different parameters!) The frequency with which the system oscillates is called the
damped eigenfrequency. This will return in Lecture 7.

https://practicon.lr.tudelft.nl/practicon/index.html?extauth=1 9/33
6/30/2020 Practicon Exercise System TU Delft

The correct answer, by the way, was:

Resonance
Now, I have an additional thing for you to find out. You have (I hope) observed that the system's response to
an initial condition looks like a decaying sine function. What would happen if we "excite" the system, by also
giving it a sine function as input?

Create two sine functions as input, one of a frequency 3.9357078 radians per second (corresponding to the
solution from the previous question), and one with a slightly lower frequency of 3.7197762 [rad/s], use an
amplitude of 1 for the sine functions. Take at least 20 seconds, calculate the response of the system and plot.
Then determine the amplitude of the sine function you get out of the system.

Note that the response has to start up, this is called the "transient", but you see that after some (simulated)
time the sine response achieves a constant amplitude.

Your answer
https://practicon.lr.tudelft.nl/practicon/index.html?extauth=1 10/33
6/30/2020 Practicon Exercise System TU Delft

What is the amplitude of the response to the sine function with a frequency
0.02786
of 3.9357078 [rad/s]?
What is the amplitude of the response to the sine function with a frequency
0.02823
of 3.7197762 [rad/s]?

The model answer


What is the amplitude of the response to the sine function with a frequency
0.02786231
of 3.9357078 [rad/s]?
What is the amplitude of the response to the sine function with a frequency
0.028231545
of 3.7197762 [rad/s]?
Explanation / script
m = 3.5;
b = 9;

Feedback
Very good, a perfect score.
Now, I hope you observed that the strongest response to a sine signal occurred for sine signals with a slightly
lower frequency than the "damped eigenfrequency", the frequency by which the system naturally oscillates
when excited.

This second, slightly lower, frequency was the "resonance frequency", if you feed the system with a sine
signal it responds strongest to a sine signal at that frequency. This will come back in Lecture 11.

Now, in case you did not manage to (completely) get the correct responses out, an example script.

First, to enter the system again:

clear
% parameter values
m = 3.5
b = 9.0
k = 60.0
% matrices
A = [ -b/m -k/m; 1 0]
B = [ 1/m; 0]
C = [ 0 1]
D = [ 0]
% system
sys = ss(A, B, C, D)

Now, calculating the input signals and the response

https://practicon.lr.tudelft.nl/practicon/index.html?extauth=1 11/33
6/30/2020 Practicon Exercise System TU Delft

% the two frequencies, note they are already in rad/s


w1 = 3.9357078
w2 = 3.7197762
% the time vector, and the input signal
t = 0:0.01:10;
u1 = sin(t*w1);
u2 = sin(t*w2);
% calculate the response, need the first input, position
y1 = lsim(sys, u1, t);
y2 = lsim(sys, u2, t);
% plot the two nicely alongside each other
plot(t, y1, t, y2)
% you can now use the data cursor to get the maximum values. Or simply
% run:
max(y1)
max(y2)

The python variant is:

import numpy as np
from control.matlab import *
from matplotlib.pyplot import plot, show

# parameter values
m = 3.5
b = 9.0
k = 60.0
# matrices
A = np.matrix([[ -b/m, -k/m], [ 1, 0]])
B = np.matrix([[ 1/m], [ 0]])
C = np.matrix([[ 0, 1]])
D = np.matrix([[ 0]])
# system
sys = ss(A, B, C, D)
w1 = 3.9357078
w2 = 3.7197762

t = np.arange(0.0, 20.001, 0.01)


u1 = np.sin(t*w1)
u2 = np.sin(t*w2)
y1, tdum, xdum = lsim(sys, U=u1, T=t)
y2, tdum, xdum = lsim(sys, U=u2, T=t)
plot(t, y1, t, y2)
show()
print(y1.max())
print(y2.max())

Testing the system

https://practicon.lr.tudelft.nl/practicon/index.html?extauth=1 12/33
6/30/2020 Practicon Exercise System TU Delft

Now is a good time to experiment a little. What would happen to the system's response if you increase (for
example double) the mass m of the block? And what happens if you increase the stiffnes k? And what
happens if you increase the damping b?

Your answer
When the stiffness is increased, the oscillations of the initial condition response have a shorter period.
When the damping is increased, oscillations of the initial condition response die out earlier.
When the mass is increased, the oscillations of the initial condition response have a larger period.

The model answer


When the stiffness is increased, the oscillations of the initial condition response have a shorter period.
When the damping is increased, oscillations of the initial condition response die out earlier.
When the mass is increased, the oscillations of the initial condition response have a larger period.
Explanation / script

Feedback
Very good, a perfect score.
I guess again that the handywork should not be a problem. Here is a file with the Matlab commands to plot all
these figures

https://practicon.lr.tudelft.nl/practicon/index.html?extauth=1 13/33
6/30/2020 Practicon Exercise System TU Delft

clear
% parameter values
m = 3
b = 6
k = 20
% matrices
A = [ -b/m -k/m; 1 0]
B = [ 1/m; 0]
% C and D to only plot position
C = [ 0 1]
D = [ 0]
% base system
sys = ss(A, B, C, D)
% increased mass
m = 2*m; A = [ -b/m -k/m; 1 0]; B = [ 1/m; 0]; m = m / 2;
sys_2m = ss(A, B, C, D)
% increased stiffness
k = 2*k; A = [ -b/m -k/m; 1 0]; B = [ 1/m; 0]; k = k / 2;
sys_2k = ss(A, B, C, D)
% increased damping
b = 2*b; A = [ -b/m -k/m; 1 0]; B = [ 1/m; 0]; b = b / 2;
sys_2b = ss(A, B, C, D)
% responses
x0 = [1; 0]
t = 0:0.01:10;
y = initial(sys, x0, t);
y_2m = initial(sys_2m, x0, t);
y_2k = initial(sys_2k, x0, t);
y_2b = initial(sys_2b, x0, t);
% plotting
figure; plot(t', [y y_2m])
figure; plot(t', [y y_2k])
figure; plot(t', [y y_2b])

You can observe that increasing the mass makes oscillations in the response slower, increasing the stiffness
makes the oscillations faster, and increasing the damping makes the oscillations die out more quickly.

Python variant:

https://practicon.lr.tudelft.nl/practicon/index.html?extauth=1 14/33
6/30/2020 Practicon Exercise System TU Delft

import numpy as np
from control.matlab import *
from matplotlib.pyplot import plot, show, figure, title

# parameter values
m = 3.0
b = 9.0
k = 60.0
# matrices
A = np.matrix([[ -b/m, -k/m], [ 1, 0]])
B = np.matrix([[ 1/m], [ 0]])
C = np.matrix([[ 0, 1]])
D = np.matrix([[ 0]])

# base system
sys = ss(A, B, C, D)

# increased mass
m = 2*m;
A = [[ -b/m, -k/m], [ 1, 0]]
B = [[ 1/m], [ 0]]
m = m / 2;
sys_2m = ss(A, B, C, D)

# increased stiffness
k = 2*k
A = [[ -b/m, -k/m], [ 1, 0]]
B = [[ 1/m], [ 0]]
k = k / 2;
sys_2k = ss(A, B, C, D)

# increased damping
b = 2*b
A = [[ -b/m, -k/m], [ 1, 0]]
B = [[ 1/m], [ 0]]
b = b / 2;
sys_2b = ss(A, B, C, D)

t = np.arange(0.0, 10.001, 0.01)

x0 = [1, 0]
y, tdum = initial(sys, X0=x0, T=t)
y_2m, tdum = initial(sys_2m, X0=x0, T=t)
y_2k, tdum = initial(sys_2k, X0=x0, T=t)
y_2b, tdum = initial(sys_2b, X0=x0, T=t)

for i, (l, y2) in enumerate(


(('mass', y_2m), ('damping', y_2b), ('spring', y_2k))):
figure(i)
plot(t, y, t, y2)
title('increased %s' % l)

https://practicon.lr.tudelft.nl/practicon/index.html?extauth=1 15/33
6/30/2020 Practicon Exercise System TU Delft

Converting between representations


Introduction
Descriptions of LTI systems given in a transfer function form can of course be converted to a state-space
format, and vice versa. The great part about using a CACSD package is that the computer can do it for you.

From state-space to transfer function


In Matlab, two different commands are used for creating transfer functions (tf) and state-space systems (ss).
Conversion is pretty easy here. To convert a state-space system to a transfer function, simply apply tf to the
state space system:

% can you recognise this one? ...


a = [ 0 1 0 ; ...
-0.0071 -0.111 0.12; ...
0 0.07 -0.3]
b = [ 0 ; -0.095; 0.072]
c = [ 1 0 0 ]
d = [ 0 ]
sys=ss(a, b, c, d)
% convert to transfer function
h = tf(sys)

In the above case, we converted a single-input, single output state-space system to a transfer function. Such
a system fits in a single transfer function. What happens if we convert a multi-input, multi-output state-space
model to transfer function form?

% create a random system. Look up rss to see its definition


sys = rss(3, 2, 3)
% convert to tf
h = tf(sys)
% see what we got
class(h)

It appears that h is still of type tf, so, a transfer function. By looking at h.num and h.den you can see that
Matlab gives a "cell array", with two rows, one row for each output, and three columns, one column per input.

In most cases however, we want to look at a single-input, single-output transfer function. How do we select a
single output and a single input from h?

There are of course several ways to do this. One is selecting the proper elements from h.num and h.den, and
using these in a tf call in Matlab:

h11 = tf(h.num{1}, h.den{1}) % Remember the curly braces!

Another method is more dashing. Think of h as a 2 rows (two outputs) by 3 columns (3 inputs) transfer
function matrix. If you pre-multiply that with a 2 column, one row transfer function, you get a single output,
three input transfer function matrix, so one row and 3 columns. And a simple matrix can substitute for a
"transfer function" that consists of only a gain:

https://practicon.lr.tudelft.nl/practicon/index.html?extauth=1 16/33
6/30/2020 Practicon Exercise System TU Delft

% select the first row of transfer functions (output 1)


[1 0] * h

In other words, it works just like matrix multiplication. The equivalent with normal matrix multiplication would
be:

>> m = [ 1 2 3; ...
4 5 6]
m =
1 2 3
4 5 6
>> firstrow = [1 0] * m
firstrow =
1 2 3

So, now to get the first output in response to (for example) the second input as a single transfer function from
h, we can do:

h11 = [1 0] * h * [0; 1; 0]

The nice thing about this method is that it also functions for a state-space system. Apply these multiplications
and you get a single-input, single output state-space system, that is equivalent to h11. I suggest you try and
check this:

h11b = tf([1 0] * sys * [1; 0; 0])


h11 - h11b

Python version of the scripts


In Python, the matrix multiplication does not work very well yet. All other methods should be OK though, here
is the script:

https://practicon.lr.tudelft.nl/practicon/index.html?extauth=1 17/33
6/30/2020 Practicon Exercise System TU Delft

from control.matlab import *


import numpy as np

# can you recognise this one?


a = np.matrix(""" 0 1 0 ;
-0.0071 -0.111 0.12;
0 0.07 -0.3""")
b = np.matrix(""" 0 ; -0.095; 0.072""")
c = np.matrix(""" 1 0 0 """)
d = np.matrix(""" 0 """)
sys = ss(a, b, c, d)
h = tf(sys)

# a random transfer function


sys = rss(3, 2, 3)
print sys

h = tf(sys)
print h
print h.__class__

# select one of the transfer functions


h11 = tf(h.num[0][0], h.den[0][0])

From transfer function to state-space


I guess that most of you, after having seen the command to convert a state-space system to a transfer
function, can imagine what the command is to do the reverse.

% invent a transfer function


s = tf('s')
h = tf((1 + 2*s)/(s*(s^2 + 0.5 *s + 4)))
% convert to state-space
sys = ss(h)

In Python:

# invent a transfer function


s = tf([1, 0], [1])
h = (1 + 2*s)/(s*(s ** 2 + 0.5 *s + 4))
# convert to state-space
sys = ss(h)

Now you saw that a multi-input, multi-output state-space system can be converted into a multi-input, multi-
output transfer function. Matlab makes "single" transfer function object, that has a cell array with numerator
polynomials, and a cell array with denominator polynomials. However, Matlab functions equally well with a
matrix of (individual) transfer functions. So you can do:

h2 = [ h ; ... % transfer function for output 1, output of h


h*s] % transfer function for output 2, derivative of
% output of h

https://practicon.lr.tudelft.nl/practicon/index.html?extauth=1 18/33
6/30/2020 Practicon Exercise System TU Delft

When you convert this to state-space, it creates a state-space system with one input and two outputs. Matlab
is clever enough to see that the denominators of the two transfer functions in h2 are the same, and efficiently
combine these transfer functions in the A matrix.

In Python, making an array of transfer functions does not work, so use:

# a multi-dimensional transfer function needs to be entered with num and


# den arrays in Python
H = tf( [ [h.num[0][0]], [(h*s).num[0][0]] ],
[ [h.den[0][0]], [(h*s).den[0][0]] ])

Enter the following state-space system with two outputs and one input, and convert it to two different transfer
functions, one giving the first system output, and the other giving the second system output.

A = [ -1.9 0 0; ...
0.4 -1.2 -5.0; ...
0 1 0]
B = [ 0.3; 0; 0]
C = [ 0 1 0; ...
0.1 0 1]
D= zeros(2,1)

Of course, to give you a chance at copy and paste with Python:

A = [ [ -1.9, 0, 0 ],
[ 0.4, -1.2, -5.0],
[ 0, 1, 0 ] ]
B = [ [0.3 ], [0 ], [0 ] ]
C = [ [0, 1, 0 ],
[0.1, 0, 1 ]]
D= [ [0],
[0] ]

Answer the following questions:

Your answer
What is the order of the numerator of the first transfer function? 1

What is the value of the real pole of the transfer functions? -1.9

What is the order of the numerator of the second transfer function? 3

The model answer


What is the order of the numerator of the first transfer function? 1

What is the value of the real pole of the transfer functions? -1.9

What is the order of the numerator of the second transfer function? 2

https://practicon.lr.tudelft.nl/practicon/index.html?extauth=1 19/33
6/30/2020 Practicon Exercise System TU Delft

Explanation / script
A = [ -1.9 0 0; ...
0.4 -1.2 -5.0; ...

Feedback
Try to figure out what went wrong!
The procedure for this is pretty straightforward, use tf to convert the state-space system, inspect it and
determine the roots of the transfer function.

% Enter the state equation matrices


A = [ -1.9 0 0; ...
0.4 -1.2 -5.0; ...
0 1 0]
B = [ 0.3; 0; 0]
% and the output equation matrices
C = [ 0 1 0; ...
0.1 0 1]
D= zeros(2,1)
% create a state-space system out of this
sys = ss(A, B, C, D)
% now convert to transfer function
h = tf(sys)
% Take one of the denominators (they are the same), and calculate the
% location of the poles
roots(h.den{1})

And here the Python version:

from control.matlab import *


from scipy import roots

A = [ [-2, 0, 0 ],
[ 0.4, -1.2, -5.0],
[ 0, 1, 0 ] ]
B = [ [0.3 ], [0 ], [0 ] ]
C = [ [0, 1, 0 ],
[0.1, 0, 1 ]]
D= [ [0],
[0] ]

sys = ss(A, B, C, D)
H = tf(sys)
print(H)
roots(H.den[0][0])

You can see that the denominators of the two transfer functions are identical, while the numerators are
different. Matlab converts the state-space system to a set of transfer functions, one for each input-output
combination. Switching the rows in the C matrix will produce similar output, only with the numerators
switched.

https://practicon.lr.tudelft.nl/practicon/index.html?extauth=1 20/33
6/30/2020 Practicon Exercise System TU Delft

When the D-matrix contains non-zero elements, this is called "direct feedthrough". Signals in the input vector
u are then directly added to the output vector y .
− −

Can you guess what happens to the numerators of the transfer functions if the D matrix contains non-zero
elements?

Take the following transfer function, and convert it into a state-space system. The state-space system should
have two outputs, one is the output of the transfer function, and the second output is the derivative of the first
output.

2
b0 + b1 s + b2 s
H (s) =
2 3
a0 + a1 s + a2 s + a3 s

Here:

numerator coefficient b0 0.2


numerator coefficient b1 0.1
numerator coefficient b2 0.5
denominator coefficient a0 2.3
denominator coefficient a1 6.3
denominator coefficient a2 3.6
denominator coefficient a3 1
To help you a bit with entering the data in Matlab or Python:

b0 = 0.2
b1 = 0.1
b2 = 0.5
a0 = 2.3
a1 = 6.3
a2 = 3.6
a3 = 1.0

Your answer
Enter the A, B, C and D matrices of this state-space system
A=[ 0 0 -1.1500; 1.0000 0 -3.1500; 0 2.0000 -3.6000]

B = [ 0.0500 -0.2875 ;0.0250 -0.7375 ;0.2500 -0.8500]

https://practicon.lr.tudelft.nl/practicon/index.html?extauth=1 21/33
6/30/2020 Practicon Exercise System TU Delft

C=[ 002 ]

D = [ 0 0.5 ]

The model answer


Enter the A, B, C and D matrices of this state-space system
A = [ -3.5527578, 1.3498728, -3.3306691e-16;
-4.1197782, -0.10651575, 0.69614273;
-0.63963278, -0.82927933, 0.059273543 ]

B = [ -0.85522796;
-0.096781514;
0.14737188 ]

C = [ -0.58463944, 0.0, 5.5511151e-17;


2.0770823, -0.7891889, 2.7755576e-16 ]

D = [ 0.0;
0.5 ]

Explanation / script
b0 = 0.2;
b1 = 0.1;

Used or unused hints


unused How to couple the transfer functions? 5 pts
To get a transfer function system with two outputs, just make a column vector with two transfer
functions in it.

https://practicon.lr.tudelft.nl/practicon/index.html?extauth=1 22/33
6/30/2020 Practicon Exercise System TU Delft

Feedback
Try to figure out what went wrong!
The procedure for this is quite simple. First enter the transfer function. Then to get also the velocity out, a set
of transfer functions needs to be converted, note that by multiplying a function by s in the Laplace domain, so
H (s) ∗ s, is equivalent to differentiation in the time domain. By putting the transfer functions in a vector, and

then converting, Matlab nicely combines the two transfer functions into a single state-space system. I would
advise you to always check the conversion; a 3rd order transfer function should result in a 3rd order state-
space system, and thus a 3 by 3 A matrix.

% enter the coefficients


b0 = 0.2
b1 = 0.1
b2 = 0.5
a0 = 2.3
a1 = 6.3
a2 = 3.6
a3 = 1
% create a transfer function, several options
h = tf([b2 b1 b0], [a3 a2 a1 a0])
% or, my favorite
s = tf('s')
h = (b0 + b1*s + b2*s^2)/(a0+ a1*s + a2*s^2 + a3*s^3)
% and then combine
H = [h; h*s]
% or read the manual and do it with cell arrays, plenty of options ...
H = tf( { [b2 b1 b0] ; [b2 b1 b0 0] },{[a3 a2 a1 a0]} )
% convert to state-space
sys = ss(H)

You might see a difference between the model output given below, and the answer you got; state-space
systems can be created in infinitely many ways. As a safety check, you can verify that the A matrix
eigenvalues are equal to the transfer function poles:

eig(sys.A)
roots(h.den{1})

Python variant:

https://practicon.lr.tudelft.nl/practicon/index.html?extauth=1 23/33
6/30/2020 Practicon Exercise System TU Delft

from control.matlab import *


from scipy.linalg import eig

# first create Laplace variable


s = tf([1, 0], [1])
# the coefficients
b0 = 0.2
b1 = 0.1
b2 = 0.5
a0 = 2.3
a1 = 6.3
a2 = 3.6
a3 = 1.0
# create a basic transfer function
h = (b0 + b1*s + b2*s**2)/(a0 + a1*s + a2*s**2 + a3*s**3)
# this combined transfer function has velocity out and the signal itself
H = tf([[h.num[0][0]], [(h*s).num[0][0]]],
[[h.den[0][0]], [(h*s).den[0][0]]])

# convert to state-space
sys = ss(H)

print(eig(sys.A)[0])
print(h.pole())

Lecture 6 -- State-Space systems in Matlab


State-space systems from block diagrams
In many cases, a control system design is defined with a block diagram. The blocks in the diagram represent
controllers, sensors, system dynamics, etc. When working with transfer function, each transfer function only
describes the relation between one of the inputs and one of the outputs. A state-space description can have
multiple inputs and multiple outputs, and thus it can describe the complete system, which is a useful starting
point in your control system design endeavours.

Consider the following example of the control of a single axis with a satellite attitude control system for a rigid-
body satellite:

https://practicon.lr.tudelft.nl/practicon/index.html?extauth=1 24/33
6/30/2020 Practicon Exercise System TU Delft

The attitude of a satellite is controlled with a reaction wheel. This wheel generates a torque Tw on the
satellite, and, obeying Newton's third law, the torque on the wheel will be −Tw . As the torque wheel exerts a
torque on the satellite, it will also increase its own momentum hw . From the diagram you can see that
momentum is nothing more than integrated torque!

In this figure, assume I2 = 1100 [kg m2 ] , the controller gain K = 1 [rad


−1
] and the time constant of the
reaction wheel is τ = 15 [s] .

This system has two inputs; one is the reference attitude for the satellite, and the other is a disturbing torque
on the satellite. Several signals in this system are of interest as output signals; first of all there is the output
from the controller (ycntrl ), which is in the form of a command to the torque wheel. Then there is the actual
torque on the satellite. It is also interesting to know the state of the torque wheel, observing the signal hw
which is the momentum currently stored in the torque wheel. Finally, one would like to know the attitude of the
satellite (θ ) and the error signal (θc − θ) is an important measure for the performance of our controller.

To create a state-space system for this diagram, it is best to work with the individual transfer functions,
transform these to state-space systems, and then combine these.

Lets start with the controller. Its transfer function is:

K (s + 0.01)
Hc (s) =
s

You may observe that the order of the numerator is equal to the order of the denominator. Conversion of a
transfer function into a state-space system is discussed in Chapter 3 of Nise's book, and that includes cases
where order of the numerator and denominator are equal. However, that is a recipe that must be learned by
heart. We can also take a simple a simple approach, and re-write the transfer function as a new transfer
function where the numerator order m is smaller than the denominator order n, and a simple gain:

K (s + 0.01) K (s + 0.01) Ks K 0.01


Hc (s) = = − + K = + K
s s s s

Now after this step, the diagram can be slightly re-written.

https://practicon.lr.tudelft.nl/practicon/index.html?extauth=1 25/33
6/30/2020 Practicon Exercise System TU Delft

In this diagram, for the transfer function of the part of the controller that we just split off (K 0.01/s), the order
of the numerator is smaller than the order of the denominator, and this transfer function can be easily
converted to a state-space system.

Let's gather some fodder for the state equation; the new block with the gain $K$ only contains an algebraic
relation, and does not end up in the state equation. If we call the output of the new block with the transfer
function for part of the controller we just created x1 , then the derivative of x1 is given by:

ẋ 1 = 0.01K (θc − θ)

Call the output of the −1/s block in the reaction wheel assembly x2 = hw , then:

−1
ẋ 2 = (x 1 + K (θc − θ) + x 2 )
τ

˙
The satellite dynamics are second order; this needs two states, x3 = θ :

1 1
ẋ 3 = (T d + (x 1 + K (θc − θ) + x 2 ))
I2 τ

and x4 = θ , so the derivative is:

ẋ 4 = x 3

Combined, this leads to the following state equation:

⎡ 0 0 0 −0.01K ⎤ ⎡ 0.01K 0 ⎤

⎢ −1/τ −1/τ 0 K /τ ⎥ ⎢ −K /τ 0 ⎥
x = ⎢
˙ ⎢
⎥ x + ⎢
⎥ ⎢
⎥ u

− ⎢ 1/(τ I ) − ⎥−
2
1/(τ I2 ) 0 −K /(τ I2 ) ⎥ ⎢ K /(τ I )
2
1/I2
⎣ ⎦ ⎣ ⎦
0 0 1 0 0 0

The output equation becomes:

https://practicon.lr.tudelft.nl/practicon/index.html?extauth=1 26/33
6/30/2020 Practicon Exercise System TU Delft

ycntrl 1 0 0 −K K 0
⎡ ⎤ ⎡ ⎤ ⎡ ⎤

⎢ Tw ⎥ ⎢ 1/τ 1/τ 0 −K /tau ⎥ ⎢ K /τ 0⎥


⎢ ⎥ ⎢ ⎥ ⎢ ⎥
⎢T + T ⎥ ⎢ ⎥ ⎢ ⎥
1/τ 1/τ 0 −K /tau ⎢ K /τ 1⎥
y = ⎢
d w
⎥ = ⎢


⎥ x + ⎢ ⎥ u
⎢ ⎥ ⎢ 0 ⎥− ⎢ −
− ⎢ hw ⎥ 1 0 0 0 0⎥
⎢ ⎥ ⎢ ⎥
⎢ ⎥ ⎢ ⎥ ⎢ ⎥
⎢ θ ⎥ ⎢ 0 0 0 1 ⎥ ⎢ 0 0⎥
⎣ ⎦ ⎣ ⎦ ⎣ ⎦
θc − θ 0 0 0 −1 1 0

This can be entered in Matlab or Python as four matrices (A, B, C and D matrix), and then a state-space
system can be created with ss. This is left as an exercise to you.

If you do enter the matrices, and inspect the resulting system, you will find that one of the eigenvalues is zero.
That eigenvalue is associated with the reaction wheel spinning up. Try testing the system with a step input,
and you will see that it oscillates quite a lot before settling to a new position, it definitely needs some further
tuning!

Consider the block diagram in the following figure:

Here the parameters are:

gain parameter K1 2.8


gain parameter K2 1.7
gain parameter K3 2.5
gain parameter K4 0.6
Create a state-space model for this system, with two inputs, the reference signal r and the disturbance (in
that order!), and one output, .

Your answer

https://practicon.lr.tudelft.nl/practicon/index.html?extauth=1 27/33
6/30/2020 Practicon Exercise System TU Delft

Enter the A, B, C and D matrices of the resulting state-space system


A = [ 0, -1.0200, -1.5000;
1.0000, -1.0000, 0;
0, 1.0000 , 0]

B = [ 0.6000, 1.0000;
0, 0;
0, 0 ]

C = [ 2.8000, 0, 1.0000 ]

D=[0, 0 ]

The model answer


Enter the A, B, C and D matrices of the resulting state-space system
A = [ 0.0, -1.02, -1.5;
1.0, -1.0, 0.0;
0.0, 1.0, 0.0 ]

B = [ 0.6, 1.0;
0.0, 0.0;
0.0, 0.0 ]

C = [ 2.8, 0.0, 1.0 ]

https://practicon.lr.tudelft.nl/practicon/index.html?extauth=1 28/33
6/30/2020 Practicon Exercise System TU Delft

D = [ 0, 0 ]

Explanation / script
K1 = 2.8;
K2 = 1.7;

Feedback
You scored top marks! Great!
Define the first state variable as the output of the first integrator (seen from left to right), the second state
variable as the output of the block with , and the third state variable as the output of the last integrator in the
row.

Then one can write:


For the second state variable, first consider the transfer function:

This in differential equation form:

Thus:

Third state variable:

Output to be calculated by:

Now putting the whole thing in Matlab:

% fill in variables
K1 = 2.8
K2 = 1.7
K3 = 2.5
K4 = 0.6
A = [ 0 , -K4*K2, -K4*K3; ...
1 , -1 , 0 ; ...
0 , 1 , 0 ]
B = [ K4 1; ...
0 0; ...
0 0]
C = [K1 0 1]
D = [0 0]
% and create the system
sys = ss(A, B, C, D)

Or in Python:

https://practicon.lr.tudelft.nl/practicon/index.html?extauth=1 29/33
6/30/2020 Practicon Exercise System TU Delft

from control.matlab import *


import numpy as np

# fill in variables
K1 = 2.8
K2 = 1.7
K3 = 2.5
K4 = 0.6

A = np.matrix([[ 0.0, -K4*K2, -K4*K3],


[ 1, -1 , 0],
[ 0, 1 , 0]])
B = np.matrix([[ K4, 1.0],
[ 0, 0],
[ 0, 0]])
C = np.matrix([[K1, 0.0, 1]])
D = np.matrix([[0, 0.0]])

# and create the system


sys = ss(A, B, C, D)

Creating a state-space model of this block diagram is also possible using Append and Connect. Using the
following notation:

Note that when using this method, a dummy system (sys8) must be added to process the final summation
point.

In Matlab:

https://practicon.lr.tudelft.nl/practicon/index.html?extauth=1 30/33
6/30/2020 Practicon Exercise System TU Delft

% Define s variable
s=tf([1,0],[1]);

% fill in variables
K1 = 2.8
K2 = 1.7
K3 = 2.5
K4 = 0.6

% Define systems; the appended system type will the type of the first system
% given. If the following systems do not have a proper form they will be
% transformed automatically

% Sys1 of the block diagram is a constant which has to be transformed into a


% state-space system.

sys1 = ss(K4 * (s/s)); % Multiply with s/s to 'trick' the control module into thinking K4 is
sys2 = ss(1/s);
sys3 = ss(1/(s+1));
sys4 = ss(1/s);
sys5 = K1; % Will be transformed by the append function
sys6 = K2;
sys7 = K3;
sys8 = 1; % Add dummy sys8 in order to sum y4 and y5

sys = append(sys1,sys2,sys3,sys4,sys5,sys6,sys7,sys8);

Q = [1 -6 -7;... % u1 = -y6 - y7
2 1 0;... % u2 = y1
3 2 0;... % u3 = y2
4 3 0;... % u4 = y3
5 2 0;... % u5 = y2
6 3 0;... % u6 = y3
7 4 0;... % u7 = y3
8 4 5]; % u8 = y4 + y5

inputs = [1,2]; % R(s) input of sys1, D(s) input of sys2


outputs = [8]; % Y(s) output of sys8

syscon = connect(sys, Q, inputs, outputs)

Or in Python:

https://practicon.lr.tudelft.nl/practicon/index.html?extauth=1 31/33
6/30/2020 Practicon Exercise System TU Delft

from control.matlab import *


import numpy as np

# Define s variable
s=tf([1,0],[1])

# fill in variables
K1 = 2.8
K2 = 1.7
K3 = 2.5
K4 = 0.6

# Define systems; the appended system type will the type of the first system
# given. If the following systems do not have a proper form they will be
# transformed automatically

# Sys1 of the block diagram is a constant which has to be transformed into a


# state-space system.

sys1 = ss(K4 * (s/s)) # Multiply with s/s to 'trick' the control module into thinking K4 is
sys2 = ss(1/s)
sys3 = ss(1/(s+1))
sys4 = ss(1/s)
sys5 = K1 # Will be transformed by the append function
sys6 = K2
sys7 = K3
sys8 = 1 # Add dummy sys8 in order to sum y4 and y5

sys = append(sys1,sys2,sys3,sys4,sys5,sys6,sys7,sys8)

Q = np.matrix([[1,-6,-7], # u1 = -y6 - y7
[2, 1, 0], # u2 = y1
[3, 2, 0], # u3 = y2
[4, 3, 0], # u4 = y3
[5, 2, 0], # u5 = y2
[6, 3, 0], # u6 = y3
[7, 3, 0], # u7 = y3
[8, 4, 5]]) # u8 = y4 + y5

inputs = [1,2] # R(s) input of sys1, D(s) input of sys2


outputs = [8] # Y(s) output of sys8

syscon = connect(sys, Q, inputs, outputs)


print(syscon)

Lecture 6 -- State-Space systems in Matlab


Conclusion

https://practicon.lr.tudelft.nl/practicon/index.html?extauth=1 32/33
6/30/2020 Practicon Exercise System TU Delft

The state-space representation is a very efficient way of representing system models in CACSD packages
like Matlab.

For simple control systems, you can simply have parallel state space systems by summing them (``+''), put
them in series with multiplication (``*''), or apply the feedbackcommand for feedback loops.

For simple gains, constants or matrices with constants can be used instead of (and mixed with) state-space
systems. In many cases, a (constant) feedback matrix is appropriate, with the number of columns matching
the size of the systems output vector being fed back, and the number of rows matching the size of the input
vector.

When combining sub-systems into a larger system, always try to check your work. You can use the
knowledge that the poles of a transfer function correspond to the eigenvalues of the A matrix of the same
system in state-space form.

In Lecture 8 the construction of more complex state-space system will come back, when we use append and
connect, to flexibly connect any number of state-space systems, representing for example your controller(s),
the actuators, system dynamics and sensors in your control system.

Practicon web-based exercise system, version: 6.0 (Feb 2015)

https://practicon.lr.tudelft.nl/practicon/index.html?extauth=1 33/33

You might also like