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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
|
-
+
+
+
+
+
-
+
+
+
+
-
+
+
+
+
+
+
|
## Generate input to actual system
u_star_t = ppp_ustar(A_u,1,t_ol',0,0,n_u-n_U);
## Tune parameters/states
if (estimating_parameters==1)
## Save up the estimated parameters
par_est = pars(i_par(:,1))
par_est = pars(i_par(:,1));
p = [p; par_est'];
## Set up according to interval length
if (T_ol>T_ol_0) ## Truncate data
simpar_est.last = T_ol_0;
y_est = y_ol(1:n_t+1,:);
else
simpar_est.last = T_ol;
y_est = y_ol;
endif
simpar_pred.last = T_ol_0; # Predicted length of next interval
pars(i_ppp(:,1)) = U_old; # Update the simulation ppp weights
## Optimise
tick = time;
[pars,Par,Error,Y,its] = \
ppp_optimise(s_system_name,x_0_models,pars,simpar_est,u_star_t,y_est,i_par,extras);
if extras.visual
figure(2);
title("Parameter optimisation");
II = [1:length(y_est)]; plot(II,y_est,"*", II,Y)
II = [1:length(y_est)]; plot(II,y_est,"*", II,Y);
endif
est_time = time-tick;
t_est = [t_est;est_time];
its_est = [its_est; its-1];
endif
## Update internal model
par(i_ppp(:,3)) = U_old; # Update the internal model ppp weights
if (estimating_parameters==1)
par(i_par(:,3)) = pars(i_par(:,1)); # Update the internal model params
endif
simpar_model.last = T_ol;
[y_model,x_model] = eval(sprintf("%s_sim(x_0_model, par, simpar_model, \
u_star_t);",system_name));
x_0 = x_model(n_ol+1,:)'; # Initial state of next interval
## x_0 = x_model(n_ol-1,:)'; # Initial state of next interval
x_0_model = x_0;
x_0_models(1:2:(2*n_x)-1) = x_0_model;
## Compute U by optimisation
tick = time;
## Predict state at start of next interval
par(i_ppp(:,3)) = U;
[y_next,x_next] = eval(sprintf("%s_sim(x_0, par, simpar, \
u_star_t);",system_name));
x_next = x_next(n_t+1,:)'; # Initial state for next time
x_nexts(1:2:(2*n_x)-1) = x_next; # And for internal sensitivity model
## Optimize for next interval
U_old = U; # Save previous value
U = expm(A_u*T_ol)*U; # Initialise from continuation trajectory
pars(i_ppp(:,1)) = U; # Put initial value of U into the parameter vector
[U, U_all, Error, Y, its] = ppp_nlin(system_name,x_nexts,pars,simpars,u_star_tau,w_s,i_ppp,extras);
if extras.visual
figure(3);
title("PPP optimisation");
II = [1:length(w_s)]; plot(II,w_s,"*", II,Y);
figure(1);
endif
ppp_time = time-tick;
t_ppp = [t_ppp;ppp_time];
its_ppp = [its_ppp; its-1];
## Total execution time
T_total = time - t_start;
|