Академический Документы
Профессиональный Документы
Культура Документы
Problem-Solving Exercises
Fall 2008
This exercise is intended to give a basic introduction to Matlab. The main focus will
be on the use of Control Systems Toolbox for control system analysis and design.
A short Matlab reference guide and a guide to the most commonly used commands
from Control Systems Toolbox can be found at the end of this exercise.
Getting started
Matlab is started by issuing the command
> matlab
which will bring up a Java-based interface with three different frames showing the
current directory, your defined variables, and the Matlab command window. This
gives a good overview, but may be slow on some computers.
An alternative way to start Matlab is by the command
which will only open up the command window. You can then use the commands ls
and whos to examine the current directory and your defined Matlab variables.
All Matlab commands have a help text, which is displayed by typing
>> A = [1 2;3 4]
A =
1 2
3 4
with semi-colons being used to separate the lines of the matrix. The (complex con-
jugate) transpose of a matrix is written as
>> A’
ans =
1 3
2 4
3
Exercise 0. Control in Matlab
Enter the system matrices A, B, and C in Matlab and use the command
eig to determine the poles of the system.
In Control System Toolbox, the basic data structure is the linear time-invariant
(LTI) model. There are a number of ways to create, manipulate and analyze models
(type, e.g., help ltimodels). Some operations are best done on the LTI system, and
others directly on the matrices of the model.
0.2 Define an LTI model of the pendulum system with the command ss. Then
use the command tf to determine the transfer function of the system.
The command tf is used to create an LTI model from a transfer function. This is
done by specifying the coefficients of the numerator and denominator polynomials,
e.g.
>> %% Continuous transfer function G(s) = 1/(2s+1)
>> G = tf(1,[2 1])
Another convenient way to create transfer function models is by the following com-
mands
>> s = tf(’s’); % or: s = zpk(’s’);
>> G = 1/(2*s+1)
To specify a time delay, set the property InputDelay of the LTI model:
>> G.InputDelay = 0.5
To display all properties of an LTI model and their respective values, type
>> get(G)
4
Exercise 0. Control in Matlab
u(t) = − Lx(t)
such that the closed loop system gets the characteristic equation
s2 + 1.4s + 1 = 0
d x̂(t)
= A x̂(t) + Bu(t) + K ( y(t) − C x̂(t))
dt
such that the observer error decays according to the characteristic equation
s2 + 2.8s + 4 = 0
d x̃(t)
= ( A − K C) x̃(t)
dt
Connecting systems
LTI systems can be interconnected in a number of ways. For example, you may
add and multiply systems (or constants) to achieve parallel and series connections,
respectively. Assume that the system (0.2) without time delay is controlled by a PID
controller
1
Gc (s) = K 1 + + sTd (0.3)
sTi
with K = 2, Ti = 1.5 and Td = 0.5, according to the standard block diagram to the
left in Figure 0.1:
l
uc u y +
Σ Gc Σ Gp Σ SYS1
–
−1 SYS2
5
Exercise 0. Control in Matlab
0.7 Define an LTI model of the PID controller (0.3) and plot its Bode diagram.
Then find the amplitude margin and phase margin for the loop transfer
function G p (s) Gc (s) using the command margin.
There is a function feedback for constructing feedback systems. The block diagram
to the right in Figure 0.1 is obtained by the call feedback(SYS1,SYS2). Note the
sign conventions. To find the transfer function from the setpoint uc to the output y,
we identify that SYS1 is G p (s) Gc (s) and SYS2 is 1.
0.8 Compute the transfer function for the closed-loop system, both using the
feedback command and by direct computation (use minreal to simplify)
according to the formula
G p (s) Gc (s)
Gcl (s) =
1 + G p (s) Gc (s)
0.9 Plot the step response of the closed-loop system. What is the stationary gain
from uc to y (use dcgain)?
Discrete-time systems
Discrete-time systems are handled in much the same way as continuous-time sys-
tems. The main difference is that a sample time Ts must be specified when a
discrete-time LTI system is created. (The special case Ts = −1 denotes an un-
specified sample time.)
Extra problems
0.11 Using Matlab, calculate the poles of the following systems
1
G1 (s) =
s3
+ 3s2 + 3s + 1
1
G2 (s) =
(s + 1)3
6
Exercise 0. Control in Matlab
During calculations, numerical round-off errors can arise, which can lead
to changed dynamics of the system. Calculate the poles of the following
systems where one coefficient has been slightly modified.
1
G3 (s) =
s3
+ 2.99s2 + 3s + 1
1
G4 (s) =
(s + 0.99)3
7
Exercise 0. Control in Matlab
Matrix commands
1 2
[ 1 2; 3 4] create the matrix .
3 4
zeros(n) creates an nxn matrix whose elements are zero.
zeros(m,n) creates a m-row, n-column matrix of zeros.
ones(n) creates a n x n square matrix whose elements are 1’s
ones(m,n) creates a mxn matrix whose elements are 1’s.
ones(A) creates an m x n matrix of 1’s, where m and n are based on the
size of an existing matrix, A.
zeros(A) creates an mxn matrix of 0’s, where m and n are based on the
size of the existing matrix, A.
eye(n) creates the nxn identity matrix with 1’s on the diagonal.
A’ (complex conjugate) transpose of A
diag(V) creates a matrix with the elements of V on the diagonal.
blkdiag(A,B,C) creates block matrix with the matrices A, B, and C on the di-
agonal
8
Exercise 0. Control in Matlab
Plotting commands
plot(x,y) creates a plot of the vectors x & y.
stairs(x,y) creates a stairstep plot of the vectors x & y.
stem(x,y) creates a discrete sequence plot of vectors x & y.
semilogx(x,y) plots log(x) vs y.
semilogy(x,y) plots x vs log(y)
loglog(x,y) plots log(x) vs log(y).
grid creates a grid on the graphics plot.
title(’text’) places a title at top of graphics plot.
xlabel(’text’) writes ’text’ beneath the x-axis of a plot.
ylabel(’text’) writes ’text’ beside the y-axis of a plot.
gtext(’text’) writes text according to placement of mouse
hold on maintains the current plot in the graphics window while
executing subsequent plotting commands.
hold off turns off the ’hold on’ option.
print filename -dps writes the contents of current graphics to ’filename’ in
postscript format.
Misc. commands
length(x) returns the number elements in a vector.
size(x) returns the size m(rows) and n(columns) of matrix x.
rand returns a random number between 0 and 1.
randn returns a random number selected from a normal
distribution with a mean of 0 and variance of 1.
rand(A) returns a matrix of size A of random numbers.
fliplr(x) reverses the order of a vector. If x is a matrix,
this reverse the order of the columns in the matrix.
flipud(x) reverses the order of a matrix in the sense of
exchanging or reversing the order of the matrix
rows. This will not reverse a row vector!
reshape(A,m,n) reshapes the matrix A into an mxn matrix
from element (1,1) working column-wise.
squeeze(A) remove empty dimensions from A
A.x access element x in the struct A
9
Exercise 0. Control in Matlab
Sampling/discretization of systems.
c2d - Continuous to discrete conversion.
d2c - Discrete to continuous conversion.
Model dynamics.
pole, eig - System poles.
zero - System zeros.
pzmap - Pole-zero map.
State-space models.
ctrb, obsv - Controllability and observability matrices.
Time response.
step - Step response.
impulse - Impulse response.
initial - Response of state-space system to given initial state.
lsim - Response to arbitrary inputs (and initial state).
Frequency response.
bode - Bode plot of the frequency response.
margin - Bode plot with phase and gain margins.
allmargin - All crossover freqs. and related gain/phase/delay margins.
nyquist - Nyquist plot.
System interconnections.
+ and - - Add and subtract systems (parallel connection).
* - Multiplication of systems (series connection).
/ and \ - Division of systems (right and left, respectively).
inv - Inverse of a system.
[ ] - Horizontal/vertical concatenation of systems.
feedback - Feedback connection of two systems.
10
Exercise 1. Sampling of Systems
dx
= −ax + bu, a>0
dt
y = cx
Sample the system assuming zero-order hold and the sampling interval h.
Discuss how the pole of the discrete-time system varies with the sampling
interval.
Sample the system assuming zero-order hold and the sampling interval
h = 0.01. Check your results using the Matlab command c2d.
Hint: Use the Laplace transform method to compute eAh .
a.
d2 y dy
2
+3 + 2y = u
dt dt
Hint: Do a partial fraction decomposition into two first-order systems: G (s) =
G1 (s) + G2 (s). Sample each system and add the results.
b.
d3 y
=u
dt3
Hint: Transfer the system into state-space form (e.g. using Matlab) before
sampling. Then use the Laplace transform method.
11
Exercise 1. Sampling of Systems
a.
x( kh + h) = 0.5x( kh) + 6u( kh)
b.
x( kh + h) = −0.5x( kh) + 6u( kh)
Extra problems
1.6 Consider a linearized model of a harmonic oscillator
dx 0 1 0
= x+
u
dt −1 0 1
y = 1 0 x
Use Matlab to compute the discrete-time step response when the sampling
period is (a) h = π /2, (b) h = π /4. Compare with the continuous-time step
response.
12
Exercise 2. Input-Output Models
Sampling the system using a zero-order hold gives the discrete-time model
e−h 0 1 − e−h
x( kh + h) =
x( kh) +
u( kh)
1 − e−h 1 h − 1 + e−h
(2.2)
y( kh) = 0 1 x( kh)
c. The variation of the poles and zeros of the pulse-transfer function with the
sampling period.
dx(t)
= u(t − τ )
dt
y(t) = x(t)
is sampled using zero-order hold. Solve problems (a)–(c) by hand and then
verify your results using Matlab.
d. Plot the pulse response of the sampled system using the Matlab command
impulse.
y( k + 2) − 1.5y( k + 1) + 0.5y( k) = u( k + 1)
a. Write the system as a discrete-time transfer function and then convert the
system into state-space form using Matlab.
13
Exercise 2. Input-Output Models
b. Use the state-space form from (a) to determine the output sequence when
u( k) is a unit step function, y(0) = 0.5 and y(−1) = 1. Use the fact that
Y ( z) = C( zI − Φ)−1 zx(0) + C( zI − Φ)−1 Γ + D U ( z)
Extra problems
2.4 Use the Matlab command lsim to verify the results from Problem 2.3 (b).
b. Simulate the step response for different pole and zero locations.
14
Exercise 3. State Feedback and Observers
3.1 Recall the equations for the DC-motor from Problem 2.1:
e−h 0 1 − e−h
x( k + 1) = x( k) +
u( k)
1 − e−h 1 h − 1 + e−h
y( k) = 0 1 x( k)
u( k) = − Lx( k),
such that the poles are placed in the origin (“deadbeat control”).
b. Assume that x(0) = [1 1]T . Determine the sampling interval such that the
control signal is less than one in magnitude. It can be assumed that the
largest magnitude occurs for k = 0.
Hint: Plot the control signal u(0) as a function of the sampling period h.
such that the observer poles are placed in the origin (“deadbeat observer”).
Do the calculations by hand and then verify the results using the Matlab
command acker (place does not work for poles with multiplicity > 1).
Hint: For this type of observer, the dynamics of the observer error x̃ = x − x̂
is given by x̃( k + 1) = (Φ − K C) x̃( k).
b. The observer in (a) has a delay of one sample, because x̂( k) depends only
on measurements up to time k − 1. The following observer can be used to
avoid the delay:
x̂( k) = Φ x̂( k − 1) + Γ u( k − 1) + K y( k) − C(Φ x̂ ( k − 1) + Γ u( k − 1)
= ( I − K C) Φ x̂( k − 1) + Γ u( k − 1) + K y( k)
Determine an observer in this form such that the observer poles are placed
in the origin. Do the calculations by hand and then verify the results using
Matlab.
Hint: For this type of observer, the dynamics of the observer error is given
by x̃( k + 1) = (Φ − K C Φ) x̃ ( k).
15
Exercise 3. State Feedback and Observers
for the extended system. Place the observer poles in 0.4, 0.5, and 0.6.
where x̂( k) and v̂( k) are given by the observer in (a). Use Matlab to de-
termine L such that the controller poles are placed in 0.3 ± 0.3i. Choose
Lv (think!) such that the influence of the disturbance is eliminated when
v̂ = v.
ẑ( k + 1) = (Φ e − K e Ce − Γ e L e) ẑ( k) + K e y( k)
u( k) = − L e ẑ( k)
Enter the controller and the process into Matlab using the command ss.
Assume the sampling period h = 1. Simulate the response of the closed-
loop system to a step input disturbance. Is the influence of the disturbance
eliminated in steady state?
Plot the Bode diagram of the loop transfer function using margin. How
robust is the control system?
16
Exercise 3. State Feedback and Observers
Extra problems
3.4 Consider the discrete-time process
0.9 0 1
x( k + 1) =
x( k) +
u( k)
1 0.7 0
y( k) = 0 1 x( k)
u( k) = L c uc ( k) − Lx( k)
3.5 Consider the deadbeat controller that was derived in Problem 3.1. Assume
that h = 3 and x(0) = [1 1]T . Formulate the closed-loop system and
plot the resulting discrete-time control signal. Use the Matlab command
initial. Does the system behave as expected?
17
Exercise 4. Approximation of
Continuous-Time Controllers. PID Control
4.1 Assuming the sampling interval h, use the various methods below to deter-
mine discrete-time approximations of the stable transfer function
a
G (s) = , a>0
s+a
For what values of h is the discrete-time system stable? For what values of
h is the pole on the positive real axis? (What is the qualitative behavior of
the discrete-time system if the pole is on the negative real axis?)
b. Backward difference
c. Tustin’s method
1+s
G (s) =
(1 + s/10)2
Use Matlab to discretize the controller assuming h = 0.1 using the various
methods below. Plot the Bode diagram of the continuous-time controller and
the discrete-time approximations. Which approximation is the best?
Hint: help c2d. For cases that c2d cannot handle, use e.g.
>> h = 0.1;
>> z = tf(’z’,h);
>> sp = (z-1)/h; % Euler’s method
>> H = (1+sp)/(1+sp/10)^2; % discrete approximation
a. Forward difference
b. Backward difference
c. Tustin’s method
18
Exercise 4. Approximation of Continuous-Time Controllers. PID Control
Td s
Td s (
1 + Td s/ N
Redo the discretization for the new transfer function. Are there any prob-
lems with the obtained difference equation?
Extra problems
4.5 Given the continuous-time system
dx −3 1 0
= x+
u
dt 0 −2 1
y = 1 0x
19
Exercise 5. Implementation
1 − e−sh 1 − 1 + sh − (sh)2 /2 + ⋅ ⋅ ⋅ sh
= =1− + ⋅⋅⋅
sh sh 2
The first two terms correspond to the Taylor series expansion of exp(−sh/2).
That is, for small h, the hold can be approximated by a time delay of half a
sampling interval.”
Assume that the phase margin can be decreased by 5○ to 15○ . Show that
this implies the following rule of thumb for the sampling period h:
hω c ( 0.15 to 0.5
Here, ω c is the crossover frequency (in radians per second) of the continuous-
time loop transfer function.
(We ignore the antialiasing filter in this problem.)
Two steps
Zero−order hold
1
s
+ =
1− e−sh
s
− e−sh
s
5.2 Assume that you have designed a PD-controller with the sampling interval
h = 0.05. The controller, written in a numerically well-conditioned state-
space form, is given by
0.6 0.5 0
x( kh + h) =
x( kh) +
y( kh)
0 0.6 2.34
u( kh) = 0.788 −0.875 x( kh) − 1.64 y( kh)
20
Exercise 5. Implementation
a. Assume that you use n = 3 fractional bits for the encoding of the coefficients.
Convert the controller coefficients to integers using this representation.
b. Using three fractional bits, what real values will the controller coefficients
effectively be rounded off to? Enter the original and the “rounded” state-
space models in Matlab and plot their step responses and Bode diagrams.
How similar are they?
c. Assume that 16-bit signed integers are used throughout in the control cal-
culations (even for intermediate results). Is there any risk of overflow?
5.3 A signal that is going to be sampled has the spectrum shown in Figure 5.2.
Of interest are the frequencies in the range from 0 to f1 Hz. A disturbance
has a fixed known frequency with f2 = 5 f1 .
a. In what choice of sampling intervals will the disturbance not be aliased into
the useful spectrum?
0 f1 f2
5.4 Figure 5.3 shows the Bode diagram for an inverted pendulum being con-
trolled by a regulator with the sampling interval h = 0.2 s. The phase
margin is ϕ m = 49○ and the cross-over frequency is ω c = 1.7 rad/s. Ap-
proximately how large extra delay can be introduced by the computer im-
plementation before the system becomes unstable?
Extra problems
5.5 Sketch the implementation of a Java class Servo that simulates, in real-
time, a DC servo process. The class should have the following signature:
public class Servo {
/** Initialize and start the servo simulator. The internal
"sampling" interval (in milliseconds) is T. */
public Servo(long T)
21
Exercise 5. Implementation
Bode Diagrams
Gm=−6.6345 dB (at 0 rad/sec), Pm=49.463 deg. (at 1.7001 rad/sec)
10
−20
−120
−140
−160
−180 0 1
10 10
Frequency (rad/sec)
dx1 (t)
= −1368x1 (t) − 17.5x2 (t) + 175u(t)
dt
dx2 (t)
= 5000x1 (t) − 0.5x2 (t)
dt
y(t) = x2 (t)
x̂( k + 1 p k) = Φ c x̂( k p k − 1) + Γ c y( k)
u( k) = Cc x̂( k p k − 1) + Dc y( k)
What are the expressions for the matrices Φ c , Γ c , Cc , and Dc ? (Notice that
if the process has one input and one output, Dc will be a scalar.)
22
Exercise 5. Implementation
You may assume that Java supports matrix data types (class Matrix) and
matrix operations (e.g., you are allowed to write y = A*x + B*u).
23
Exercise 6. Scheduling
Task name Ti Di Ci
A 3 3 0.6
B 4 4 1.2
C 5 5 1.5
b. Will the tasks meet their deadlines according to the approximate analysis
in rate-monotonic scheduling theory?
c. Will the tasks meet their deadlines according to the exact analysis in rate-
monotonic scheduling theory?
Task name Ti Di Ci
A 5 5 2
B 2 2 1
24
Exercise 6. Scheduling
Task name Ti Di Ci
A 5 5 2
B 7 7 4
a. Check the schedulability of the task set using the sufficient rate-monotonic
schedulability condition.
b. Check the schedulability of the task set using the necessary and sufficient
rate-monotonic schedulability condition.
c. Show the execution trace (schedule) for the worst case (both tasks arrive
(i.e., want to start their execution) at the same time). Assume that the
actual execution times for the tasks are the same as the worst case execution
times.
d. Verify the schedulability of the task set using the EDF schedulability con-
dition.
e. Show the execution trace (schedule) for the worst case (both tasks arrive
at the same time) in the EDF case. Assume that the actual execution times
for the tasks are the same as the worst-case execution times.
Extra problems
6.5 Three tasks, A, B and C should be scheduled on a single processor using
the STORK Modula 2 kernel. The tasks have the following characteristics
(a low numeric priority value means a high priority for the task):
25
Exercise 6. Scheduling
Will the tasks meet their deadlines? Motivate your answer. (Hints: In STORK,
the main program runs as a thread with the highest priority. A thread cre-
ated with CreateProcess runs at one priority level lower that the main
program until it does a SetPriority. The time returned by CurrentTime is
given in multiples of 1 msec. Assume that the kernel is ideal and that no
other threads are running.)
b. Modify the main program so that the tasks all will meet their deadlines.
(Hint: The primitive WaitTime(1) is equivalent to sleep(1) in Java.)
M
X “
y− ȳi
”2
σi
f ( y) = wi ⋅ e (6.1)
i=1
26
Exercise 6. Scheduling
Nonlinear
Compensation
f(y)
.
u x = f(x,u) y
Initial Control Σ
Algorithm y = h(x)
Physical System
Figure 6.1 Initial control algorithm augmented with a nonlinear compensation term.
M Additions
M Subtractions
M Divisions
2M Multiplications
M Evaluations of exp( z).
The controller hardware is based on a Motorola 68040 processor working at
fc = 25 MHz. On this platform, the required operations take the following
times
Additions 4 fc−1
Subtractions 4 fc−1
Divisions 40 fc−1
Multiplications 10 fc−1
Evaluations of exp( z) 400 fc−1
a. The controller can guarantee stability provided that the extra time delay
introduced by the compensation algorithm is less than 0.1 ms. Would you
advise the engineer to implement his compensation scheme with M = 10
in the compensation (6.1)?
b. Without the compensation for the nonlinearity, the sampling time is set to
1 ms and the CPU utilization for all the tasks in the system is estimated
to be 60%. Assuming only periodic tasks and rate-monotonic scheduling,
what is the largest M for which the engineer can still guarantee that all
deadlines will be met when the compensation is included?
27
Solutions to Exercise 0. Control in Matlab
ans =
-1
1
Transfer function:
1
–––-
s^2 - 1
Zero/pole/gain:
1
–––––-
(s+1) (s-1)
Transfer function:
1
exp(-1.5*s) * –––––––-
s^2 + 0.6 s + 1
>> step(G)
b. >> bode(G)
>> nyquist(G)
The Nyquist curve encircles the critical point −1, which means that the
closed-loop system will be unstable.
L =
1.4000 2.0000
28
Solutions to Exercise 0. Control in Matlab
K =
5.0000
2.8000
Transfer function:
1.5 s^2 + 3 s + 2
––––––––––––––-
1.5 s^3 + 2.4 s^2 + 4.5 s + 2
Transfer function:
2.25 s^5 + 5.85 s^4 + 7.95 s^3 + 6.3 s^2 + 3 s
––––––––––––––––––––––––––––––
2.25 s^6 + 4.95 s^5 + 11.16 s^4 + 10.65 s^3 + 8.55 s^2 + 3 s
Transfer function:
s^2 + 2 s + 1.333
–––––––––––––-
s^3 + 1.6 s^2 + 3 s + 1.333
ans =
1.0000
ans =
1
0
0.11
29
Solutions to Exercise 0. Control in Matlab
>> G1 = 1/((s+1)^3)
Transfer function:
1
––––––––––-
s^3 + 3 s^2 + 3 s + 1
>> pole(G1)
ans =
-1.0000
-1.0000 + 0.0000i
-1.0000 - 0.0000i
>> G2 = zpk(1/((s+1)^3))
Zero/pole/gain:
1
–––-
(s+1)^3
>> pole(G2)
ans =
-1.0000
-1.0000 + 0.0000i
-1.0000 - 0.0000i
>> G3 = 1/(s^3+2.99*s^2+3*s+1);
>> pole(G3)
ans =
-1.0888 + 0.2131i
-1.0888 - 0.2131i
-0.8124
>> G4 = zpk(1/((s+0.99)^3));
>> pole(G4)
ans =
-0.9900 + 0.0000i
-0.9900 - 0.0000i
-0.9900
30
Solutions to Exercise 1. Sampling of Systems
ẋ = −ax + bu
y = cx
Sampling the system using Eqs. (8) from the IFAC PB gives
b
x( kh + h) = e−ah x( kh) + 1 − e−ah u( kh)
a
y( kh) = cx( kh)
The pole of the sampled system is located in e−ah , i.e., the pole is located
on the positive real axis and within the unit circle. For small values of h
the pole is close to 1, i.e., the discrete system behaves like an integrator.
For large values of h the pole is close to the origin, i.e., the discrete system
behaves like a one-sample time delay.
x( kh + h) = Φ x( kh) + Γ( kh)
y( kh) = Cx( kh)
Hence
1
Ah −1 −1 1 8 (1 − e−8h ) 1 0.00961
Φ=e = L (sI − A) =
=
0 e−8h 0 0.9231
and
Z h Z h
5 −8s ) 5 1 −8h
As 8 (1 − e 8 ( 8 (e − 1) + h) 0.0002435
Γ= e Bds =
ds =
5
=
0 0 5e−8s 8 (1 − e−8h ) 0.04805
Matlab solution:
31
Solutions to Exercise 1. Sampling of Systems
1 1 1
G (s) = = −
(s + 1)(s + 2) s+1 s+2
1 − e−h 1 − e−2h
H ( z) = −
z − e−h 2( z − e−2h )
b. The system can be transfered into state-space form by the following Matlab
commands:
>> s = zpk(’s’);
>> G = 1/s^3;
>> G = ss(G)
Hand calculations and conversion back into transfer function form give
h3 ( z2 + 4z + 1)
H ( z) =
6( z − 1)3
dx(t)
= Ax(t) + Bu(t)
dt
We are looking for scalars A and B that fulfill
Φ = eAh = 0.5
Zh
Γ=
eAs B ds = 6
0
32
Solutions to Exercise 1. Sampling of Systems
ln 2
Ah = ln 0.5 [ A=−
h
6A 12 ln 2
[ B= =
eAh −1 h
b. In this case we get the equations
Ah
Φ = e = −0.5
Z h
Γ=
eAs B ds = 6
0
The first equation has no (real) solution, which implies that no equivalent
first-order continuous system exists. In fact, the discrete system has a pole
on the negative real axis and will hence oscillatate, which no continuous
first-order system can do.
x( k + 1) = Φ x( k) + Γ u( k)
y( k) = Cx( k)
where
cos h sin h
Φ(h) =
− sin h cos h
1 − cos h
Γ(h) =
sin h
In Matlab:
>> A = [0 1 ; -1 0]; % Define system matrices
>> B = [0 ; 1];
>> C = [1 0];
>> D = 0;
>> G = ss(A,B,C,D) % Define continuous-time system
>> H1 = c2d(G,pi/2); % Sample the oscillator
>> H2 = c2d(G,pi/4);
>> step(G,H1,H2,20) % Plot the step response of all systems until 20 s
33
Solutions to Exercise 1. Sampling of Systems
Step Response
From: U(1)
2.5
Amplitude 1.5
To: Y(1)
0.5
−0.5
2 4 6 8 10 12 14
Time (sec.)
1
G (s) = kc
s+a
is then given as
1
H ( z) = kd
z − e−ah
The gain kd of the discrete system is given by matching stationary gains,
i.e., by putting G (0) = H (1). This gives
1 − e−ah
kd = kc ⋅
a
and
kc 1 − e−ah
⋅ H ( z) =
a z − e−ah
So, for this problem we can do a partial fraction decomposition and then
sample each term. This gives with h = 1:
1 1 1 1 1 1
G (s) = = ⋅ − + ⋅
s(s + 1)(s + 2) 2 s s+1 2 s+2
1 1 1 − e−1 1 1 − e−2
H ( z) = − +
2 z−1 z − e−1 4 z − e−2
Matlab solution:
>> G = tf([1],[1 3 2 0]);
>> G = ss(G);
>> H = c2d(G,1);
>> tf(H)
34
Solutions to Exercise 2. Input-Output Models
H ( z) = C( zI − Φ)−1 Γ
− 1
z − e−h 0 1 − e−h
= (0 1)
−1 + e−h z − 1 h + e−h − 1
(0 1) z−1 0 1 − e−h
=
( z − e−h )( z − 1) 1 − e−h z − e−h h + e−h − 1
c. The poles are located in z = 1 (integrator) and z = e−h . The second pole
will move from 1 to the origin when h goes from zero to infinity (see the
solution to Problem 1.1).
The zero is located in
1 − e−h − he−h
z=−
h + e−h − 1
2.2 a. We first note that τ < h. The discrete-time system in this case is given by
(see the IFAC PB Eqs. (12) and (13))
x( k + 1) = Φ x( k) + Γ 0 u( k) + Γ 1 u( k − 1)
Φ = eAh = e0 = 1
Z h−τ Z 0.5
As
Γ0 = e ds B = 1 ds 1 = 0.5
0 0
Z τ Z 0.5
A(h−τ ) As
Γ1 = e e ds B = ds = 0.5
0 0
[ x( k + 1) = x( k) + 0.5u( k) + 0.5u( k − 1)
35
Solutions to Exercise 2. Input-Output Models
−0.5 −1 0.5
−1 z−1
H ( z) = C( zI − Φ̄) Γ̄ = ( 1 0 )
0 z 1
1 z 0.5 0.5
= (1 0)
z( z − 1) 0 z − 1 1
1 0.5 0.5( z + 1)
= ( z 0.5 ) =
z( z − 1) 1 z( z − 1)
Matlab solution:
>> zpk(H)
c. The poles are located in z = 0 (time delay) and z = 1 (integrator). The zero
is located in z = −1.
Matlab solution:
>> pole(H)
>> zero(H)
The result, shown to the left in Figure 2.1, is a bit confusing because of the
stairstep plot. A better plot, shown to the right in Figure 2.1, uses plot and
stem to display the continuous-time pulse response (dashed) together with
the sampled response (stem plot).
2.3 a.
z
H ( z) =
z2 − 1.5z + 0.5
>> z = tf(’z’);
>> H = z/(z^2-1.5*z+0.5);
>> H = ss(H)
36
Solutions to Exercise 2. Input-Output Models
1 1
0.8 0.8
Amplitude
Amplitude
0.6 0.6
0.4 0.4
0.2 0.2
0 0
0 1 2 3 4 5 0 1 2 3 4 5
Time (sec) Time (sec)
where
C( zI − Φ)−1 Γ + D = H ( z)
is the pulse transfer function. The initial state is
y(0) 0.5
x(0) = =
y(−1) 1
This gives
0.5z z2 0.5z 1 2
Y ( z) = + 2
= + +
z − 0.5 ( z − 1) ( z − 0.5) z − 0.5 z − 0.5 ( z − 1)2
Inverse transformation using the table gives
−1 z
Z 0.5 = 0.5 ⋅ 0.5k = 0.5k+1
z − 0.5
−1 −1 z
Z z = 0.5k−1
z − 0.5
−1 −1 z
Z 2z = 2( k − 1)
( z − 1)2
Summing up, we have
37
Solutions to Exercise 2. Input-Output Models
2.5 a.
s+β
is lead if β <α
s+α
Consider the discrete time system
z+b
z+ a
eiω h + b cos ω h + b + i sin ω h
arg iω h = arg =
e +a cos ω h + a + i sin ω h
sin ω h sin ω h
= arctan − arctan
b + cos ω h a + cos ω h
Phase lead if
sin ω h sin ω h
arctan > arctan 0 < ωh < π
b + cos ω h a + cos ω h
sin ω h sin ω h
>
b + cos ω h a + cos ω h
We thus get phase lead if b < a.
Simulation of the system for b = −0.9, −0.75, −0.5, 0, 0.5 and 1 is shown in
Figure 2.2.
38
Solutions to Exercise 2. Input-Output Models
10
Output
0
0 10 20
Time
Figure 2.2 Simulation of the step response of the system in Problem E-5.6 for b = −0.9
(upper solid), −0.75 (upper dashed), −0.50 (dash-dotted), 0 (dotted), 0.5 (lower solid), and 1
(lower dashed).
39
Solutions to Exercise 3. State Feedback and
Observers
det( zI − Φ + Γ L)
z − e−h + l1 (1 − e−h ) l2 (1 − e−h )
= det
−(1 − e−h ) + l1 (h − 1 + e−h ) z − 1 + l2 (h − 1 + e−h )
= z2 + (−1 − e−h + (1 − e−h )l1 + (h − 1 + e−h )l2 ) z
b. If x(0) = [ 1 1 ]T , then
−2 + 2e−h + he−2h
u(0) = −l1 − l2 = .
h(1 − e−h )2
Figure 3.1 shows u(0) as a function of h. From the plot we see that the
control signal is less than one in magnitude if h > 2.2.
0
Output u(0)
−2
0 1 2 3 4 5
Sampling period h
40
Solutions to Exercise 3. State Feedback and Observers
Matlab solution:
Matlab solution:
41
Solutions to Exercise 3. State Feedback and Observers
The response to an input step disturbance and the Bode diagram of the
loop gain are shown in Figure 3.2. The disturbance is eliminated in steady
state. The system is not very robust, since the phase margin is only 19○ .
Bode Diagram
Step Response Gm = 3.8236 dB (at 1.1034 rad/sec), Pm = 19.463 deg (at 0.55708 rad/sec)
0.5 30
Magnitude (dB)
20
0.4
10
0
0.3
Amplitude
−10
225
0.2
180
Phase (deg)
135
0.1 90
45
0 0
−2 −1 0
0 5 10 15 20 10 10 10
Time (sec) Frequency (rad/sec)
Figure 3.2 The response of the closed-loop system to an input step disturbance (left) and
the Bode diagram of the loop gain (right).
I.e.,
−1.6 + {1 = 0 0.63 − 0.7{1 + {2 = 0
or
L = 1.6 0.49
The stationary gain of the closed loop system is given by stationary gain of
the
L c ⋅ C( I − Φ + Γ L)−1 Γ = 1
To get unit steady state gain we choose L c = 1
42
Solutions to Exercise 3. State Feedback and Observers
x( k + 1) = (Φ − Γ L) x( k)
u( k) = − Lx( k)
>> h = 3;
>> Phi = [exp(-h) 0; 1-exp(-h) 1];
>> Gamma = [1-exp(-h); h-1+exp(-h)];
>> L = acker(Phi,Gamma,[0 0]);
>> Hcl = ss(Phi-Gamma*L,[0;0],-L,0,h);
>> x0 = [1;1];
>> [u,t] = initial(Hcl,x0,15);
>> stairs(t,u)
w( k + 1) = w( k)
v( k) = w( k)
The process can thus be described in the form given in IFAC PB p. 65, with
Φ w = 1, Φ xw 1
=
0
a. If the state and v can be measured then we can use the controller
x( k + 1) = Φ x( k) + Φ xw w( k) − Γ Lx( k) − Γ Lww( k)
= (Φ − Γ L) x( k) + (Φ xw − Γ Lw)w( k)
y( k) = Cx( k)
Hw(1) = 0
43
Solutions to Exercise 3. State Feedback and Observers
and
−0.142 −0.114
Φ − ΓL =
0.179 0.142
and
Lw = 5.356
b. In this case is the state but not the disturbance measurable. The distur-
bance can now be calculated from the state equation
Φ xww( k − 1) = x( k) − Φ x( k − 1) − Γ u( k − 1).
The first element in this vector equation gives
w( k − 1) = [1 0]( x( k) − Φ x( k − 1) − Γ u( k − 1))
Since w( k) is constant and x( k) is measurable it is possible to calculate
ŵ( k) = w( k − 1). The following control law can now be used
u( k) = − Lx( k) − Lwŵ( k)
where Lw is the same as in (a). Compared with the controller in (a) there
is a delay in the detection of the disturbance.
c. If only the output is measurable then the state and the disturbance can be
estimated using an observer of the form
x̂( k + 1) Φ Φ xw x̂( k) Γ K
=
+
u( k) +
ε ( k)ε ( k) = y( k) − C x̂( k)
ŵ( k + 1) 0 1 ŵ( k) 0 Kw
The gain vector can now be determined such that the error goes to zero
provided the augmented system is observable. The error equation is
x̃( k + 1) Φ − K C Φ xw x̃( k)
=
w̃( k + 1) − Kw C 1 w̃( k)
The characteristic equation of the system matrix for the error is
z3 + ( k1 − 2.2) z2 + (1.05 − 1.7k1 + k2 + kw) z + 0.7k1 + 0.15 − 0.7kw − k2 = 0.
The eigenvalues are in the origin if
k1 = 2.2
k2 = −0.6433
kw = 3.3333.
The controller now has to be
u( k) = − L x̂( k) − Lwŵ( k)
where L and Lv are the same as in (a). The solutions above have the draw-
back that there may be an error in the output due to the disturbance if
there are small errors in the model. Figure 3.3 show that the output when
the controllers in (a), (b) and (c) are used.
44
Solutions to Exercise 3. State Feedback and Observers
0.2 2
Output
Output
0 0
−0.2 −2
0 5 10 15 0 5 10 15
2
Estimate ve
Output
0 2
−2 0
0 5 10 15 0 5 10 15
Time Time
Figure 3.3 The output of the system in Problem 3.6 for the regulators in a) (upper left), b)
(upper right) and c) (lower left and right). The estimate of v is also shown for case c). Notice
the difference in scale in the upper left curve.
45
Solutions to Exercise 4. Approximation of
Continuous-Time Controllers. PID Control
a ah( z + 1) ah( z + 1)
H ( z) = G (s′ ) = 2 z−1
= =
h z+1 +a 2( z − 1) + ah( z + 1) (2 + ah) z + (ah − 2)
a tan(ah/2)( z + 1)
H ( z) = G (s′ ) = a z−1
= =
tan(ah/2) z+1
+a ( z − 1) + tan(ah/2)( z + 1)
tan(ah/2)( z + 1)
(1 + tan(ah/2)) z + (tan(ah/2) − 1)
1−tan(ah/2)
The discrete-time pole is located in z = 1+tan(ah/2) . The system stable for all
values of h.
π
The pole ends up on the positive real axis for h < 2a .
46
Solutions to Exercise 4. Approximation of Continuous-Time Controllers. PID Control
c. >> H3 = c2d(G,h,’tustin’);
>> bode(H3)
d. >> H4 = c2d(G,h,’foh’);
>> bode(H4)
The Tustin and ramp invariance methods give the best approximations for
this example.
z−1
s′ =
h
This gives
h Td ( z − 1)
U ( z) = K 1 + + E( z)
( z − 1)Ti h
h Td 2
( z − 1) U ( z) = K z − 1 + + ( z − 1) E( z)
Ti h
Notice that, in the time domain, on the left hand side we have terms involv-
ing u( k) and u( k + 1), while on the right hand side we have terms involving
e( k), e( k + 1) and e( k + 2). The difference equation is non-causal and thus
impossible to implement.
47
Solutions to Exercise 4. Approximation of Continuous-Time Controllers. PID Control
L =
17.0000 3.0000
b. >> h = 0.1;
>> Ltilde = L*(eye(2)+(A-B*L)*h/2)
Ltilde =
11.9000 3.1000
>> H1 = ss(A-B*L,[0;0],C,0);
>> [Phi,Gamma]=c2d(A,B,h);
>> H2 = ss(Phi-Gamma*L,[0;0],C,0,h);
>> H3 = ss(Phi-Gamma*Ltilde,[0;0],C,0,h);
>> [y1,t1] = initial(H1,[1;0],2.5);
>> [y2,t2] = initial(H2,[1;0],2.5);
>> [y3,t3] = initial(H3,[1;0],2.5);
>> plot(t1,y1)
>> hold on
>> stem(t2,y2,’r’)
>> stem(t3,y3,’g’)
48
Solutions to Exercise 5. Implementation
5.1
1 − e−sh 1 − 1 + sh − (sh)2 /2 + ⋅ ⋅ ⋅ sh
= =1− +⋅⋅⋅
sh sh 2
The argument of
sh
1−
2
at the cross-over frequncy, wc , is given by
wc h
arctan(− )
2
Decreasing the phase margin with 15○ (0.26 rad) gives
wc h
arctan(− ) = −0.26 [ wc h = 0.53
2
Decreasing the phase margin with 5○ (0.087 rad) gives
wc h
arctan(− ) = −0.087 [ wc h = 0.17
2
ans =
5 4
0 5
>> round(B*2^n)
ans =
0
19
>> round(C*2^3)
ans =
6 -7
>> round(D*2^3)
ans =
-13
49
Solutions to Exercise 5. Implementation
b. Three fractional bits means that the coefficients will effectively be rounded
off to the nearest multiple of 1/23 = 0.125. In Matlab, the rounded coffi-
cients can be found by e.g.
>> round(A*2^3)/2^3
ans =
0.6250 0.5000
0 0.6250
The systems behave quite similarly, although the fixed-point version has
too low static gain.
c. 16-bit signed integers can take values from −32768 to 32767. The input is
in the range −512 to 511. Simulated step responses (or gain computations
using the command norm) show that the gain from the input to the states
may be as large as 8.5. Since 512 ⋅ 8.5 ⋅ 23 = 34816 > 32768, there is a small
risk of overflow. A more detailed analysis would have to consider the exact
order in which the various terms in the control law are added.
Matlab code for simulating input-to-state gain:
>> Hx1 = ss(A3,B3,[1 0],0,h); % model with x1 as output
>> step(Hx1)
>> Hx2 = ss(A3,B3,[0 1],0,h); % model with x2 as output
>> step(Hx2)
5.3 Let fs = h1 be the sample frequency. The sampling theorem states that if
the continuous-time signal has a spectrum G ( f ), the sampled signal gets
the spectrum
X∞
H( f ) = G ( f + k fs ).
k=−∞
50
Solutions to Exercise 5. Implementation
π
5.4 The delay margin is approximately ϕ m /ω c = 49○ 180○ /1.7 = 0.50 s.
// monitor methods
public synchronized void setU(double u) {
this.u = u;
}
public synchronized double getY() {
return y;
}
private synchronized double getU() {
return u;
}
private synchronized void setY(double y) {
this.y = y;
}
// constructor
51
Solutions to Exercise 5. Implementation
public Servo(long T) {
ServoThread st = new ServoThread(T);
st.start();
}
// internal thread
private class ServoThread extends Thread {
private long duration, T, t;
private double h, u, x1 = 0.0, x2 = 0.0, newx1, newx2;
ServoThread(long T) {
this.T = T;
h = 0.001*(double)T;
}
t += T;
duration = t - System.currentTimeMillis();
if (duration < 1) duration = 1;
try {
sleep(duration);
} catch (Exception e) {}
}
}
}
}
52
Solutions to Exercise 5. Implementation
LQGController() {
// create matrices, etc.
...
}
53
Solutions to Exercise 6. Scheduling
The schedule is shown in Figure 6.1. The worst-case response times of the
tasks are R A = 3, R B = 2, R C = 9, i.e., task A will not meet its deadlines.
A
0 2 4 6 8 10 12 14 16 18 20
Arrival
B
0 2 4 6 8 10 12 14 16 18 20
Completion
C
0 2 4 6 8 10 12 14 16 18 20
The schedule is shown in Figure 6.2. The worst-case response times of the
tasks are R A = 1, R B = 3, R C = 9, i.e. all tasks will meet their deadlines.
A
0 2 4 6 8 10 12 14 16 18 20
Arrival
B
0 2 4 6 8 10 12 14 16 18 20
Completion
C
0 2 4 6 8 10 12 14 16 18 20
6.2 a.
3
X Ci 0.6 1.2 1.5
U= = + + = 0.8
Ti 3 4 5
i=1
54
Solutions to Exercise 6. Scheduling
In this case n(21/n − 1) = 0.7798 < 0.8, and we cannot conclude from this
criterion whether the tasks are schedulable or not.
c. Here A has high priority, B has medium priority, and C has low priority.
The worst-case response time R i of task i satisfies
X Ri
R i = Ci + Cj
Tj
∀ j ∈hp(i)
where hp(i) is the set of tasks of higher priority than i, and ⌈⋅⌉ is the ceiling
function. The equation can be solved by fix-point iterations.
Start with calculating the worst-case response time for A ( R A ): as A has
the highest priority it will not be interrupted, thus
R A = CA = 0.6.
Task B can be interrupted by task A. Start the fix-point iterations with the
initial value R1B = CB = 1.2:
R1B
R2B = CB + CA = CB + CA = 1.8.
TA
R2B
R3B = CB + CA = CB + CA = 1.8.
TA
Thus R B = 1.8.
Task C can be interrupted both by A and B. Start with the initial value
R1C = 1.5.
& ' & '
R1C R1C
R2C = CC + CA + CB = CC + CA + CB = 3.3.
TA TB
& ' & '
R2C R2C
R3C = CC + CA + CB = CC + 2CA + CB = 3.9.
TA TB
& ' & '
R3C R3C
R4C = CC + CA + CB = CC + 2CA + CB = 3.9.
TA TB
Thus R C = 3.9.
As R i ≤ Di for all i, all the tasks will meet their deadlines with rate mono-
tonic scheduling.
55
Solutions to Exercise 6. Scheduling
6.3 a. The utilization is 90%. This is above the limit (n(21/n − 1) = [ n=2 ] =
0.828) for the sufficient condition and the exact analysis must be applied.
The tasks will meet their deadlines if and only if
R i ≤ Di , ∀i
where
X Ri
R i = Ci + Cj
Tj
∀ j ∈hp(i)
where hp(i) is the set with all tasks of higher priority than task i and ⌈ x⌉
is the ceiling function that returns the smallest integer ≥ x.
The calculation of R i is a recurrence equation that can be solved by the
iteration
X Rn
n+1
Ri = Ci + i
Cj , R0i = 0
Tj
∀ j ∈hp(i)
For task B the response time becomes equal to the execution time, i.e, = 1.
For task A the following results are obtained.
R0A = 0, R1A = CA = 2
1
2 RA
R A = CA + CB = CA + CB = 3
TB
2
RA
R3A = CA + CB = CA + 2CB = 4
TB
& '
3
R
R4A = CA + A
CB = CA + 2CB = 4
TB
Hence, the response times are smaller than the deadlines for both tasks,
and the task set is schedulable.
b. Task A has higher priority than task B. We draw the schedule for the worst-
case when both tasks are released simultaneously, at time 0. The maximum
0 1 2 3 4 5 6 7 8 9 10
response time is obtained for the first invocation that is released at time
0 and does not become finished until time 3, i.e., the maximum response
time is 3, well above the deadline that is 2.
c. For task B with the highest priority the worst-case and the best-case input-
output latencies are both equal to the execution time, i.e. = 1. Consider
now task A. The worst case occurs when task A is preempted by task B
immediately after it has performed the sampling. Then first task B executes
for 1 time unit, then task A is resumed and continues for another time unit
until it again is preempted by task B. After yet another time unit task A
may continue and finish. The worst case latency for task A is thus equal
to 4. Using similar reasoning one can easily see that the best-case input
latency for task A is equal to 3.
56
Solutions to Exercise 6. Scheduling
6.4 a.
U = 2/5 + 4/7 = 0.9714 > 2(21/2 − 1) = 0.8284
It is not possible to prove schedulability using the sufficient RM schedula-
bility test.
c.
A
0 5 10 15 20 25 30 35
Arrival
B
0 7 14 21 28 35
Completion
e.
A
B
0 5 10 15 20 25 30 35
Here, we assume that when two processes are equally close to their dead-
lines, the task that was added to the ready queue latest has priority. This
occurs at time=30 when both tasks have 5 time units to their deadlines.
Here, task A interrupts task B. The task set would, however, be schedulable
also with the opposite policy of letting the currently executing task finish.
6.5 a. The tasks will not meet their deadlines. When all the threads have been
created and executed their SetPriority, process B will execute, calculate
the next execution time and do a waituntil. The worst case execution time
is 0.8 msec. At that time thread A will start executing. The first thing it
does is that it measures the current time which will return the same tick
as for thread B. This means that thread B and A both will try to run in the
same clock time tick. The situation is shown in Fig. 6.4.
Thread A and B will both be scheduled for execution at time t = 0, 3, 6, 9, . . ..
Thread C will be scheduled for execution at time t = 1, 4, 7, 10, . . .. The
response time of B will be 0.8 msec. The response time of A will be 1.6
msec, i.e., larger than the deadline. The response time of C will be 1.4
msec, also larger than the deadline.
57
Solutions to Exercise 6. Scheduling
C C
A A
B B
1 ms 2 ms 3 ms 4 ms 5 ms 6 ms
b. The load on the CPU is balanced if the execution of the three threads are
spread out evenly in time. A simple way of ensuring this for this simple
case is to do a simple WaitTime between the calls to CreateProcess.
BEGIN
...
...
CreateProcess(TaskprocessA,1000,"A");
WaitTime(1);
CreateProcess(TaskprocessB,1000,"B");
WaitTime(1);
CreateProcess(TaskprocessC,1000,"C");
Wait(Terminate);
END Main.
seconds.
For fc−1 = 4 ⋅ 10−8 s and M = 10, this evaluates to
0.187 ms
b. For rate monotonic scheduling, it holds that all deadlines will be met if the
CPU utilization is less than 69%.
Since the CPU utilization with the original control algorithm is 60 %, the
additional compensation may use at maximum 9% of the CPU time. We
thus have
C 468M fc−1
Ucomp = = ≤ 0.09
T 0.001
from which we obtain
M ≤4.8
58