1
2
3
4
5
6
7
8
|
function [y,x,u,t,U,U_c,U_l] = ppp_nlin_sim (system_name,A_u,tau,t_ol,N_ol,w,extras)
## usage: [y,x,u,t,U,U_c,U_l] = ppp_nlin_sim (system_name,A_u,tau,t_ol,N,w)
##
##
## Simulate nonlinear PPP
## Copyright (C) 2000 by Peter J. Gawthrop
|
|
|
1
2
3
4
5
6
7
8
|
function [y,x,u,t,UU,UU_c,UU_l] = ppp_nlin_sim (system_name,i_ppp,i_par,A_u,w_s,N_ol,extras)
## usage: [y,x,u,t,U,U_c,U_l] = ppp_nlin_sim (system_name,A_u,tau,t_ol,N,w)
##
##
## Simulate nonlinear PPP
## Copyright (C) 2000 by Peter J. Gawthrop
|
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
|
endif
## Names
s_system_name = sprintf("s%s", system_name);
## System details
par = eval(sprintf("%s_numpar;", system_name));
x_0 = eval(sprintf("%s_state;", system_name))
[n_x,n_y,n_u] = eval(sprintf("%s_def;", system_name));
## Sensitivity system details
sympars = eval(sprintf("%s_sympar;", s_system_name));
pars = eval(sprintf("%s_numpar;", s_system_name));
## Times
n_tau = length(tau);
dtau = tau(2)-tau(1); # Optimisation sample time
Tau = [0:dtau:tau(n_tau)+eps]; # Time in the moving axes
n_Tau = length(Tau);
dt = t_ol(2)-t_ol(1);
n_t = length(t_ol);
T_ol = t_ol(n_t)+dt
## Weight and moving-horizon setpoint
weight = [zeros(n_y,n_Tau-n_tau), ones(n_y,n_tau)];
ws = w*ones(1,n_tau);
## Create input basis functions
[n_U,junk] = size(A_u);
## For moving horizon
eA = expm(A_u*dtau);
u_star_i = ones(n_U,1);
u_star_tau = [];
for i = 1:n_Tau
u_star_tau = [u_star_tau, u_star_i];
u_star_i = eA*u_star_i;
endfor
## and for actual implementation
eA = expm(A_u*dt);
u_star_i = ones(n_U,1);
u_star_t = [];
for i = 1:n_t
u_star_t = [u_star_t, u_star_i];
u_star_i = eA*u_star_i;
endfor
if extras.verbose
title("U*(tau)")
xlabel("tau");
plot(Tau,u_star_tau)
endif
## Check number of inputs adjust if necessary
if n_u>n_U
disp(sprintf("Augmenting inputs with %i zeros", n_u-n_U));
u_star_tau = [u_star_tau; zeros(n_u-n_U, n_Tau)];
u_star_t = [u_star_t; zeros(n_u-n_U, n_t)];
endif
if n_u<n_U
error(sprintf("n_U (%i) must be less than or equal to n_u (%i)", \
n_U, n_u));
endif
## Indices of U and sensitivities
FREE = "[";
free_name = "ppp";
for i=1:n_U
FREE = sprintf("%ssympars.%s_%i sympars.%s_%is", FREE, free_name, \
i, free_name, i);
if i<n_U
symbol = "; ";
else
symbol = "];";
endif
FREE = sprintf("%s%s", FREE, symbol);
endfor
FREE
free = eval(FREE);
## Compute linear gains
[A,B,C,D] = eval(sprintf("%s_sm(par);", system_name));
B = B(:,1); D = D(:,1);
[k_x,k_w,K_x,K_w] = ppp_lin(A,B,C,D,A_u,0,tau);
## Main simulation loop
y = [];
x = [];
u = [];
t = [];
t_last = 0;
|
|
>
|
|
>
>
|
|
|
|
|
|
<
|
>
>
>
>
>
|
|
|
|
>
>
>
|
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
|
|
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
|
endif
## Names
s_system_name = sprintf("s%s", system_name);
## System details -- defines simulation within ol interval
par = eval(sprintf("%s_numpar;", system_name));
simpar = eval(sprintf("%s_simpar;", system_name))
x_0 = eval(sprintf("%s_state(par);", system_name))
[n_x,n_y,n_u] = eval(sprintf("%s_def;", system_name));
## Sensitivity system details -- defines moving horizon simulation
simpars = eval(sprintf("%s_simpar;", s_system_name))
sympars = eval(sprintf("%s_sympar;", s_system_name));
pars = eval(sprintf("%s_numpar;", s_system_name));
## Times
## -- within opt horizon
n_Tau = round(simpars.last/simpars.dt)
dtau = simpars.dt
Tau = [0:n_Tau-1]'*dtau;
[n_tau,n_w] = size(w_s);
tau = Tau(n_Tau-n_tau+1:n_Tau);
w = w_s(length(w_s)); # Final value of setpoint
## -- within ol interval
n_t = round(simpar.last/simpar.dt)
dt = simpar.dt;
t_ol = [0:n_t-1]'*dt;
T_ol = n_t*dt;
## Weight and moving-horizon setpoint
# weight = [zeros(n_y,n_Tau-n_tau), ones(n_y,n_tau)];
# w_s = w*ones(n_tau,1);
## Create input basis functions
[n_U,junk] = size(A_u);
## For moving horizon
eA = expm(A_u*dtau);
u_star_i = ones(n_U,1);
u_star_tau = [];
for i = 1:n_Tau
u_star_tau = [u_star_tau; u_star_i'];
u_star_i = eA*u_star_i;
endfor
## and for actual implementation
eA = expm(A_u*dt);
u_star_i = ones(n_U,1);
u_star_t = [];
for i = 1:n_t
u_star_t = [u_star_t; u_star_i'];
u_star_i = eA*u_star_i;
endfor
if extras.verbose
title("U*(tau)")
xlabel("tau");
plot(Tau,u_star_tau)
title("U*(t)")
xlabel("t_ol");
plot(t_ol,u_star_t)
endif
## Check number of inputs adjust if necessary
if n_u>n_U
disp(sprintf("Augmenting inputs with %i zeros", n_u-n_U));
u_star_tau = [u_star_tau; zeros(n_u-n_U, n_Tau)];
u_star_t = [u_star_t; zeros(n_u-n_U, n_t)];
endif
if n_u<n_U
error(sprintf("n_U (%i) must be less than or equal to n_u (%i)", \
n_U, n_u));
endif
## Compute linear gains
[A,B,C,D] = eval(sprintf("%s_sm(par);", system_name));
B = B(:,1); D = D(:,1);
[k_x,k_w,K_x,K_w] = ppp_lin(A,B,C,D,A_u,0,tau');
## Main simulation loop
y = [];
x = [];
u = [];
t = [];
t_last = 0;
|
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
|
elseif strcmp(extras.U_initial,"zero")
U = zeros(n_U,1);
else
error(sprintf("extras.U_initial = \"%s\" is invalid", extras.U_initial));
endif
## Reverse time to get "previous" U
U = expm(-A_u*T_ol)*U;
for i = 1:N_ol
## Compute initial U from linear case
U_l = K_w*w - K_x*x_0;
## Compute initial U for next time from continuation trajectory
U_c = expm(A_u*T_ol)*U;
## Create sensitivity system state
x_0s(1:2:2*n_x) = x_0;
## Set next U (initial value for optimisation)
if strcmp(extras.U_next,"linear")
U = U_l;
elseif strcmp(extras.U_next,"continuation")
U = U_c;
elseif strcmp(extras.U_next,"zero")
U = zeros(n_U,1);
else
error(sprintf("extras.U_next = \"%s\" is invalid", extras.U_next));
endif
## Put initial value of U into the parameter vector
pars(free(:,1)) = U;
## Compute U
tick = time;
if extras.max_iterations>0
[U, U_all, Error, Y] = ppp_nlin(s_system_name,x_0s,u_star_tau,tau,pars,free,ws,extras);
else
Error = [];
endif
opt_time = time-tick;
printf("Optimisation %i took %i iterations and %2.2f sec\n", i, \
length(Error), opt_time);
# title(sprintf("Moving horizon trajectories: Interval %i",i));
# grid;
# plot(tau,Y)
## Generate control
u_ol = U'*u_star_t(1:n_U,:);
## Simulate system
## (Assumes that first gain parameter is one)
U_ol = [u_ol; zeros(n_u-1,n_t)]; # Generate the vector input
[y_ol,x_ol] = eval(sprintf("%s_sim(x_0, U_ol, t_ol, par);", system_name));
## Extract state for next time
x_0 = x_ol(:,n_t);
y = [y y_ol(:,1:n_t)];
x = [x x_ol(:,1:n_t)];
u = [u u_ol(:,1:n_t)];
UU = [UU, U];
UU_l = [UU_l, U_l];
UU_c = [UU_c, U_c];
t = [t, t_ol(:,1:n_t)+t_last*ones(1,n_t) ];
t_last = t_last + t_ol(n_t);
endfor
## Rename returned matrices
U = UU;
U_l = UU_l;
U_c = UU_c;
endfunction
|
|
|
|
<
|
|
>
|
|
|
|
|
<
|
|
|
|
|
>
>
|
|
|
|
|
|
|
|
|
<
<
<
<
<
<
<
<
|
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
183
184
185
186
187
188
189
|
elseif strcmp(extras.U_initial,"zero")
U = zeros(n_U,1);
else
error(sprintf("extras.U_initial = \"%s\" is invalid", extras.U_initial));
endif
## Reverse time to get "previous" U
U = expm(-A_u*T_ol)*U;
for i = 1:N_ol # Main loop
## Compute initial U from linear case
U_l = K_w*w - K_x*x_0;
## Compute initial U for next time from continuation trajectory
U_c = expm(A_u*T_ol)*U;
## Create sensitivity system state
x_0s(1:2:2*n_x) = x_0;
## Set next U (initial value for optimisation)
if strcmp(extras.U_next,"linear")
U = U_l;
elseif strcmp(extras.U_next,"continuation")
U = U_c;
elseif strcmp(extras.U_next,"zero")
U = zeros(n_U,1);
else
error(sprintf("extras.U_next = \"%s\" is invalid", extras.U_next));
endif
pars(i_ppp(:,1)) = U; # Put initial value of U into the parameter vector
## Compute U by optimisation
tick = time;
if extras.max_iterations>0
[U, U_all, Error, Y] = ppp_nlin(system_name,x_0s,pars,simpars,u_star_tau,w_s,i_ppp,extras);
pars(i_ppp(:,1)) = U; # Put final value of U into the parameter vector
else
Error = [];
endif
opt_time = time-tick;
printf("Optimisation %i took %i iterations and %2.2f sec\n", i, \
length(Error), opt_time);
##title(sprintf("Moving horizon trajectories: Interval %i",i));
##grid;
##plot(tau,Y)
## Generate control
u_ol = u_star_t*U; # Not used - just for show
## Simulate system over one ol interval
## (Assumes that first gain parameter is one)
##U = [u_ol zeros(n_t,n_u-1)]; # Generate the vector input
[y_ol,y_s,x_ol] = eval(sprintf("%s_ssim(x_0s, pars, simpar, u_star_t);", s_system_name));
x_0 = x_ol(n_t+1,:)'; # Extract state for next time
y_ol = y_ol(1:n_t,:); # Avoid extra points due to rounding error
x_ol = x_ol(1:n_t,:); # Avoid extra points due to rounding error
y = [y; y_ol];
x = [x; x_ol];
u = [u; u_ol];
UU = [UU; U'];
UU_l = [UU_l; U_l'];
UU_c = [UU_c; U_c'];
t = [t; t_ol+t_last*ones(n_t,1) ];
t_last = t_last + T_ol;
endfor
endfunction
|