-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathslidemo2.m
182 lines (114 loc) · 4.71 KB
/
slidemo2.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
echo on,
% This demonstration example uses a 4-th order model for the flexible
% robot arm, also identified by slidemo1.m demonstration file. Given
% this model (the "true system"), output trajectories are generated,
% by simulation. Then, based on pairs of input and output trajectories,
% both deterministic and stochastic systems are identified using
% SLICOT system identification toolbox, and the results are assessed.
echo off
% RELEASE 2.0 of SLICOT System Identification Toolbox.
% Based on SLICOT RELEASE 5.7, Copyright (c) 2002-2020 NICONET e.V.
%
% V. Sima 31-12-2000.
%
% Revisions: 04-03-2009.
%
echo on
global pause_wait % This could be used in pause(n) command.
% If pause_wait < 0, standard command pause is used (default).
% Any key should then be pressed to continue.
if ~exist('pause_wait', 'var') || isempty(pause_wait), pause_wait = -1; end
echo off
disp('Deterministic system example:')
disp('-----------------------------')
echo on
if pause_wait < 0, pause, else pause(pause_wait), end
% Load the input-output data:
load robot_arm_dat; u = robot_arm_dat(:,1); y = robot_arm_dat(:,2);
% Find a 4-order model based on all data using slmoen4 (alg = 1).
s = 20; n = 4; sys = slmoen4(s,y,u,n);
if pause_wait < 0, pause, else pause(pause_wait), end
% Print the model:
sys
if pause_wait < 0, pause, else pause(pause_wait), end
% Generate an output trajectory using the model and a random input:
time_int = size(u,1);
u = rand(time_int,size(u,2));
y = lsim(sys,u);
if pause_wait < 0, pause, else pause(pause_wait), end
% Estimate a new model using the input and output trajectories:
syse = slmoen4(s,y,u);
if pause_wait < 0, pause, else pause(pause_wait), end, close(gcf)
% Compute the output error:
x0 = inistate(syse,y,u);
ye = lsim(syse,u,[],x0);
if pause_wait < 0, pause, else pause(pause_wait), end
% Compute the first nMpar Markov parameters of the two models:
nMpar = 100;
mar_par = markov(sys, nMpar);
mar_pare = markov(syse,nMpar);
if pause_wait < 0, pause, else pause(pause_wait), end
echo off
disp(' ')
disp( 'Relative output error,')
disp([' norm(y - ye)/norm(y) = ',...
sprintf('%0.2e',norm(y - ye)/norm(y))])
disp(' ')
disp( 'Relative error in Markov parameters,')
disp([' norm(mar_par - mar_pare)/norm(mar_par) = ',...
sprintf('%0.2e',norm(mar_par - mar_pare)/norm(mar_par))])
echo on
if pause_wait < 0, pause, else pause(pause_wait), end
echo off
disp(' ')
disp('Stochastic system example:')
disp('--------------------------')
echo on
if pause_wait < 0, pause, else pause(pause_wait), end
% Perturb the output trajectory:
y = y + 10^-2*rand(time_int,size(y,2));
if pause_wait < 0, pause, else pause(pause_wait), end
% Generate another model:
[sysn,K] = slmoen4(s,y,u);
if pause_wait < 0, pause, else pause(pause_wait), end, close(gcf)
% Compute the output errors without, and with a Kalman predictor:
[err,ye] = find_err(y,u,sysn);
[errK,yeK] = find_err(y,u,sysn,K);
if pause_wait < 0, pause, else pause(pause_wait), end
% Compute the first nMpar Markov parameters of the new model:
mar_parn = markov(sysn,nMpar);
if pause_wait < 0, pause, else pause(pause_wait), end
echo off
disp(' ')
disp( 'Relative output error,')
disp([' without predictor: norm(y - ye)/norm(y) = ',...
sprintf('%0.2e',norm(y - ye)/norm(y))])
disp([' with predictor: norm(y - yeK)/norm(y) = ',...
sprintf('%0.2e',norm(y - yeK)/norm(y))])
disp(' ')
disp( 'Relative error in Markov parameters,')
disp([' norm(mar_par - mar_parn)/norm(mar_par) = ',...
sprintf('%0.2e',norm(mar_par - mar_parn)/norm(mar_par))])
echo on
if pause_wait < 0, pause, else pause(pause_wait), end
% Plot the estimated output, without predictor:
plot_ye(y,ye);
% Plot the estimated output, with predictor:
plot_ye(y,yeK);
% Compute the autocorrelation of the output error ("residuals"):
res = y - ye;
acc = xcorr(res);
if pause_wait < 0, pause, else pause(pause_wait), end
plot(acc);
title('Autocorrelation of the residuals, without Kalman predictor')
xlabel('Lag'); ylabel('Autocorrelation values')
if pause_wait < 0, pause, else pause(pause_wait), end, close(gcf)
% Also, with Kalman predictor:
resK = y - yeK;
accK = xcorr(resK);
if pause_wait < 0, pause, else pause(pause_wait), end
plot(accK);
title('Autocorrelation of the residuals, with Kalman predictor')
xlabel('Lag'); ylabel('Autocorrelation values')
if pause_wait < 0, pause, else pause(pause_wait), end, close(gcf)
echo off