Computer Exercises
Computer Exercises
SITB
Course on System Identification, Spring 2016
Lennart Ljung
• There are easily more exercises here than you will have time to do dur-
ing the course. Concentrate on the exercises marked with an asterisk
(*), and then you can save the others for later training.
• The exercises are intended for MATLAB version 8.4 and the System
Identification Toolbox version 9.1 (R2014b).
• It is recommended to run Exerises 1.1 – 2.8 and 3.7 with the GUI –
the System Identification app. Most of the problems in Exercises 3
and 4 are better suited for command line commands.
1
1.1 * The GUI
In this first exercise we shall get familiar with the basic functions of the
SITB in its GUI mode.
The sampling interval is 80 ms, and the input was chosen as a binary random
signal.
Follow the demo, and then try to find good models yourself.
Let’s simulate our own system, and try out various methods with the data:
The system we generate and simulate is
y(t)−1.5y(t−1)+0.7y(t−2) = u(t−1)+0.5u(t−2)+e(t)−e(t−1)+0.2e(t−2)
where e(t) is white Gaussian noise with variance 0.25.
To generate this model and the data, do the following in the MATLAB
command window:
m0 = idpoly(a,b,c);
m0.NoiseVariance = 0.25;
2
y = sim(u,m0,simOptions(’AddNoise’,true)); % The output
Import the data u and y using the Import Data pop-up menu, and also
import the model m0 using the Import Model pop-up menu.
Then, first try to estimate a model in the ”correct” structure, which is armax
with nn=[2 2 2 1], and evaluate the model’s properties (zeros and poles,
the frequency function, the step response and so on), and compare with the
true, simulated system’s properties.
Also try different model structures with oe, armax, arx, bj to see how
well the parameters, the zeros and poles, the step response, and the fre-
quency function of the system can be estimated in other structures.
How did we come up with the structure for the hair-dryer arx, [2 2 3] in
Section 1.1? A suitable path is the following one:
First try correlation analysis to get a feeling for the delay and basic dynam-
ics. That is, use Correlation Model in the Estimate pop-up and look
at the transient response. It is good to look at the impulse response as a
stem plot and add confidence interval lines. Use the menus in the transient
response window to achieve this.
We see fairly clearly that the delay is 3 samples. (Note that the first element
of the impulse response corresponds to delay 0.)
Second, let us get a quick feeling for whether it will be ”easy” to find a good
linear black box model by trying a fourth order ARX model with the delay
we found by correlation analysis. To evaluate its properties we just simulate
it with the validation set:
3
Suppose we go for the best fit which is obtained for [4 4 3]. (This is
however only slightly better than [2 2 3]).
Then, fourth we will check whether the high order model actually is necessary
to describe the dynamics from u to y, or whether it is the noise properties
that have pushed the need for more poles. This we do by checking possible
pole-zero cancellations in the transfer function from u to y. Look at poles
and zeros with confidence intervals. The significance of a higher order model
is also revealed by the discrepancy between the second and fourth order
models in the Bode Plot and in the Model Output.
iddata1
iddata2
.
.
.
iddata8
The data object in each of them is called z1, z2, .. z8. These are simu-
lated data. There are thus ”correct solutions” to them. (These are revealed
by z1.note, z2.note etc. Note that idpoly(A,B,C,D,F) is the polynomial
linear model Ay = B/F u + C/De )
Examine several of them, and try to figure out how they have been generated.
If you like, you can use the four step start-up procedure that we outlined in
Section 1.3.
4
1.5 * Real Process Data
Load the mat-file realdata.mat. It contains several data sets from real
applications. Select a few and try to build good models.
dry2: Data from a “hairdryer”. The input is the power of the heater and
the output is the temperature of the air at the outlet of the dryer. The
sampling interval is 0.08 seconds.
steamdat: Data from the Control Division’s model steam engine driving an
electric generator. This process has two inputs and two outputs. The inputs
are (1) Pressure of the ”steam” (compressed air) and (2) the magnetization
voltage to the generator. The outputs are (1) the frequency and (2) the
voltage of the generated current. The experiment was performed in open
loop and the sampling interval was 50 ms.
ecnmtr: These are the sales data, given by Box and Jenkins (1970). The
input is the leading sales indicator, while the output is the actual sales.
fouga: These are the gas furnace data, given by Box and Jenkins(1970).
The input is the methane (CH4 ) flow, while the output is the percentage of
the carbondioxide (CO2 ) in the fumes.
glass: This data sets is taken from a glass drawing manufacturing company
in France. The glass data contains the drawing speed as input and the
thickness of the manufactured glass tube as output. The drawing speed is
the speed at which the glass tube is drawn at the production line.
5
jas: These are data from one of the test flights of JAS 39 Gripen. The
output is the pitch rate and the three inputs are various control surfaces on
the wings.
6
Exercise # 2: Special Methods for Linear System
Models
by
y = sim(randn(500,1),m0);
Import the signal and the true system into the GUI as before.
Let’s now try and estimate the spectrum of this signal by Spectral Model.
Use different window sizes and methods and examine the resulting spectra.
Compare with the true spectrum.
The mat file current contains the variable i4r. It contains the current
signal in the R-phase when a 400 kV transformer is energized. The sampling
interval is 1ms. The signal is very oscillatory and quite wide time windows
may be needed.
If time permits, try and get a good estimate of the spectrum of current via
AR-models.
Load any of the simulated data sets iddata1 ... iddata8 that you worked
with in Exercise 1.4. Use (in the GUI) Estimate>Spectral Models for spec-
tral analysis estimates of the transfer function. Try different window sizes.
Try also a “raw” (unsmoothed) ETFE (by choosing ETFE with default
resolution) Compare with what parametric models give!
7
2.3 Pre-filtering
Load again the signal i4r from current.mat. First try AR-models with
increasing orders and look at the spectra. You will need orders around 20
before a good spectrum with most of the harmonics is obtained. (You may
have to increase the plotting resolution of the spectra – using the Options
menu) to see all details.
Then try out pre-filtering of the signal, and see if you can estimate certain
parts of the spectrum well, using lower order models.
Estimate, with parametric techniques, the 4th and 5th harmonics of the
signal!
Load one of the simulated systems in exercise 1.4. Try the IV-method
(found under Estimate -> Polynomial models -> ARX) and the N4SID
method (found under Estimate -> State Space models -> Estimation
Options). Study how they compare with the prediction error methods for
the same structures.
In particular you may try the ARX orders [2 2 1] for iddata1 with ARX and
IV (why is there a difference?).
If time allows and you have interest you may study how the N4SID method
behaves with different “horizons” (under Estimation Options; see page
531 in textbook – these are the orders of the ARX models used to compute
the states).
Load the data set marx22.mat. It contains simulated data (called z with
two outputs and two inputs. Find a good ARX-model for these data. Split
the data set into estimation and validation sets in the usual way. What
is the best structure? Try also N4SID on these data! Evaluate frequency
functions, zeros and poles, simulation behavior, residuals and the like!
8
2.6 Multi-output Systems, cont’d
Add to the simulated outputs white Gaussian measurement noise with zero
mean and unit variance.
Estimate models from these data using the arx command. Evaluate their
properties. Why are they not so good?
2.8 * Regularization
Load the file Reg. It contains the data object eData and the true system
that generated it, trueSys. Import both into the GUI. Try FIR models (i.e.
ARX models with na=0) to estimate the impulse response for different or-
ders ([0 nb 0]) using Estimate -> Polynominal Models -> ARX. Compare
with the true system’s impulse response (using the model view Transient
Response, Option: Impulse Response); turn the confidence region on
and off.
9
Why is it difficult to get a good fit? Try the regularization option offered in
the estimation panel (e.g. with the TC kernel) also for quite high nb-orders
(like 100).
Although the system is of quite low order it is not very well excited for high
order FIR models. Try also oe([3 2 0]) for a “correct model order”.
10
Exercise # 3: Grey-box and Nonlinear Models
[It may be easier to do this exercise in command mode, than in the GUI.]
A simple DC motor with outputs angle and angular velocity can be modeled
as discussed on page 95 in the textbook. The dynamics of the motor can –
without noise – be described by
d
0 1 0
x(t) = x(t) + u(t)
dt 0 θ1 θ2
1 0
y(t) = x(t)
0 1
Use idss and its object properties to create a model structure for this. Use
the initial/nominal values θ1 = −1 and θ2 = 0.28.
You can create this model in continuous time using the idss object com-
mand:
m = idss(A,B,C,D,’ts’,0)
In the MAT-file dcsim there is a simulated data set from this DC motor. The
first output is the angular position of the motor shaft and the second output
is the angular velocity. The input is the applied voltage. The sampling
interval is 0.1 seconds. As usual, split the data into estimation and validation
data:
ze = dcsim(1:200), zv = dcsim(201:end)
compare(dcsim,m)
Use arx, n4sid, and ssest with suitable structures to determine a good
model of the system from the estimation data. Check poles and zeros,
frequency functions and simulation and prediction behavior on the validation
data.
11
Then build a grey box model using the idss structure with certain param-
eters fixed according to the model above.
Hint: The model parameters can be fixed and unfixed with commands as
m.Structure.a.Free = [0 0; 0 1]
Use ssest to estimate such a grey box model with parameters fixed accord-
ing to the above structure.
Now load the real data in dcreal and try the same model structures. Any
problems?
• The initial value of the position may not be zero. Can it be estimated?
[ssest(ze,m,ssestOptions(’InitialState’,’Estimate’));]
• Output error methods are a bit tricky in this case when the system is
not asymptotically stable. How can we ”let loose” a noise model?
[ssest(ze,m,’DisturbanceModel’,’Estimate’,...
ssestOptions(’InitialState’,’Estimate’));]
Consider again the simulated data dcsim. Suppose that we accurately know
the static gain of the DC motor (from input voltage to angular velocity), for
12
example from a previous step-response experiment. If the static gain is G
and the time constant of the motor is τ , then the state-space model becomes
d
0 1 0
x(t) = x(t) + u(t)
dt 0 −1/τ G/τ
1 0
y(t) = x(t)
0 1
With G known, there is a dependence between the entries of the different
matrices. In order to describe that, the earlier used way to free and fix
parameters will not be sufficient.
Write your own m-file that describes this new model structure. Let the static
gain G be ”an auxiliary argument” (so that it can be changed without editing
the file).
[Help: There is such a function with the name motor in the toolbox. Type
“type motor” to see it.]
Then use idgrey to define the structure. Study help idgrey for the format.
Estimate the parameter τ under the assumption that the static gain is G =
0.25. Compare the obtained model with the models obtained in the other
structures.
Some measured data are given in the file zvib. The output is the translata-
tional acceleration of the table (in 103 cm/ms2 ). The input is the applied
force (N). The sampling frequency is 300 Hz.
Let y be the position of the table. Let m be its mass, k the spring constant
and b the damping factor (viscous damping). Then we have
˙ b k 1
ẏ(t) = − ẏ(t) − y(t) + F (t)
m m m
13
With the state vector
y(t)
x(t) =
ẏ(t)
we have
0 1 0
ẋ(t) = x(t) + F (t)
−k/m −b/m 1/m
1
ÿ(t) = ( −k/m −b/m ) x(t) + F (t)
m
For the experimental setup, m is of the order of magnitude 1, k is of the
order of magnitude 100000 and b is of the order of magnitude 100.
Estimate m, k and b from the data! Evaluate the obtained model, and
compare it with what arx and n4sid and other input-output models give!
A well known friction model proposed by Makkar, et al (2005), links the slip
speed v(t) of a body in contact with another body to the friction force f(t)
via the static relationship
At our disposal are 2 different (simulated) data sets where the input slip
speed was swept from -10 m/s to 10 m/s in a ramp-type manner. We load
the data and create two IDDATA objects for our identification experiments,
ze for estimation and zv for validation purposes. The data sets are given in
the mat-file friction.
14
0.12; 110; 0.015]}; % Initial parameters.
InitialStates = []; % Initial initial states.
Ts = 0; % Time-continuous system.
nlgr = idnlgrey(FileName, Order, Parameters, InitialStates, Ts, ...
’Name’, ’Static friction model’, ...
’InputName’, ’Slip speed’, ’InputUnit’, ’m/s’, ...
’OutputName’, ’Friction force’, ’OutputUnit’, ’N’, ...
’TimeUnit’, ’s’);
nlgr = setpar(nlgr, ’Minimum’, {zeros(6, 1)}); % All
% parameters must be >= 0.
To save some time for you, this idnlgrey object was also contained in the
mat-file friction. Use compare to check how well the initial model nlgrey
describes the validation data. Use pem to estimate the model parameters
from the estimation data and check if the fit to validation data is improved.
Check with m.parameters.Value how much the parameters have been ad-
justed.
In this case the initial parameters were in reasonable vicinity of the true
ones. Test different initial values by
Type iddemo in the command window and select nonlinear grey box exam-
ples - Example # 9. Go through the demo by clicking the next button.
15
3.5 Linear Regressions With Physical Insight: Solar Heated
House
First load the data from the solar heated house in the file solardat.mat.
The iddata object z contains the temperature of the heat storage, the pump
velocity (on/off), and the solar radiation. The sampling interval is 10 min-
utes. Examine the data and try out some standard (linear) models!
Do the physical transformations take care of all effects? Look at the ”bump”
in the temperature around hour 12-15 (sample 72-90). All models have diffi-
culties to explain that. What do you think is the cause of this phenomenon?
Consider the following process. An electric heater is put into a fluid, whose
temperature varies (basically due to the influx of new fluid). The internal
resistance of the heater is 5 ohms. The temperature of the fluid (tf), the
temperature of the heater (y) and the applied voltage to the heater (u) is
measured. Construct a model of the system (i.e. how does y depend on
u and tf ?) based on the (simulated) data in the file zheat!. Use as few
parameters as possible!
Load the file nldata. It contains the data object znl. Import it into the
GUI and find a nonlinear model that describes a validation part of the data
16
as well as possible.
Hints: Estimate -> Nonlinear models shows rather well what is available
for black-box nonlinear models. Note the different options, e.g. the possi-
bility to randomly restart the search for neural network models to come to
grips with local minima.
17
Exercise # 4: Recursive Algorithms
Load the data in the mat-file recdata. The data is called zrec. It has been
simulated by the system
In on-line applications the estimates are required at the same time as the
data are observed. The recursive algorithms in the SITB can be used also
in that way, provided relevant information is stored from sample to sample.
The conceptual algorithm becomes
2. Update:
[th,yh,P,phi] = rarx([y u],[na,nb,nk],’ff’,0.98,th’,P,phi);
4. Go to 1.
Thus the previous estimate th is fed back into the algorithm along with the
previous value of the matrix P and the data vector phi.
Try out this mode by plotting the estimates sequentially. It is useful to fix
the axis of the plot by a separate first step, which also produces start-up
values of th, P and phi:
18
axis([1 50 -2 2])
plot(1,th(1),’*’,1,th(2),’+’,1,th(3),’o’,1,th(4),’*’),hold
for k=2:50
[th,yh,P,phi]=rarx(z(k,:),[2 2 1],’ff’,0.98,th’,P,phi);
plot(k,th(1),’*’,k,th(2),’+’,k,th(3),’o’,k,th(4),’*’)
end
If you prefer, you may write an m-file function for this (taking for example
arguments for the adaptation mechanism and adaptation gain), so that you
easily can test different variants.
Load the data in the mat-file fail. They are called zfail. The data
are generated by a first order system that at some time point undergoes a
”failure” in that the delay nk changes from 1 to 2. Models of ARX-type will
do well. Test how this failure can be detected by recursive identification.
Try out different adaptation mechanisms and adaptation gains that allow
”quick and certain” detection. If you prefer, you can use on-line plotting as
in Exercise 4.2. This will increase the ”realism” (but is somewhat slower).
19
5 Command-Line Commands
Most execises can be run in the System Identication APP (“the GUI”). But
some are better suited for command line work. This short list may help
you find relevant commands. Use “Help command” or “doc command” for
syntax information.
Most execises can be run in the System Identication APP (“the GUI”). But
some are better suited for command line work. This short list may help
you find relevant commands. Use “Help command” or “doc command” for
syntax information.
• polyest, arx, or, iv, bj, armax: commands for estimating poly-
nomial models
• zpkdata, iopzmap: Compute and plot poles and zeros (same as CSTB)
20