-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFigure03.m
337 lines (274 loc) · 9.38 KB
/
Figure03.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
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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
%% Unicycle
clear;close all;%clc
clear functions
format short g
rng(3,'twister');
warning('off');
set(groot,'defaultlineLineWidth',2)
steps = 5;
flagZB = 1;
flagCZ = 0;
flagMVT = 0;
flagREGO = 1;
flagREGOMVT = 0;
flagCombined = 0;
% MVT_prediction = @mvx_prediction;
% MVT_update = @mvx_update;
MVT_prediction = @mvt2_prediction;
MVT_update = @mvt2_update;
%% Nonlinear Dynamics
number_of_states = 3;
phi_omega = 0.30; % Displacement Velocity
phi_theta = 0.15; % Angular Velocity
T0 = 1.00; % Sampling Period
func = @(x,w) [x(1) + T0.*phi_omega.*cos(x(3)) + w(1);...
x(2) + T0.*phi_omega.*sin(x(3)) + w(2);...
x(3) + T0.*phi_theta + w(3)];
% Ouput Equation
Sx1 = 5;
Sy1 = 15;
Sx2 = 15;
Sy2 = 15;
Dv = eye(4);
Outfunc = @(x,v) [sqrt((Sx1-x(1)).^2+(Sy1-x(2)).^2)+v(1);
x(3) - atan((Sy1-x(2))/(Sx1-x(1))) + v(2);
sqrt((Sx2-x(1)).^2+(Sy2-x(2)).^2)+v(3);
x(3) - atan((Sy2-x(2))/(Sx2-x(1))) + v(4)];
%% Process & Measurement Disturbances
w0 = [0.5*rand(1,steps)-0.3;
0.3*rand(1,steps)-0.2;
0.6*rand(1,steps)-0.4];
w0 = 0.2*eye(number_of_states)*w0;
W = conZonotope(interval([0.5*0-0.3;0.3*0-0.2;0.6*0-0.4],...
[0.5*1-0.3;0.3*1-0.2;0.6*1-0.4]));
v0 = [0.02*rand(1,steps)-0.01;
0.03*rand(1,steps)-0.01;
0.03*rand(1,steps)-0.02;
0.05*rand(1,steps)-0.03];
V = conZonotope(interval([0.02*0-0.01;0.03*0-0.01;0.03*0-0.02;0.05*0-0.03],...
[0.02*1-0.01;0.03*1-0.01;0.03*1-0.02;0.05*1-0.03]));
WM = conZonotope(interval([0;0;0],[0;0;0]));
v0 = v0*0;
w0 = w0*0;
%% Initial Conditions
x0 = [0.1;0.2;1];
percentage = [10;10;20];
x01 = [x0(1)*(1-sign(x0(1))*percentage(1)/100),x0(1)*(1+sign(x0(1))*percentage(1)/100)];
x02 = [x0(2)*(1-sign(x0(2))*percentage(2)/100),x0(2)*(1+sign(x0(2))*percentage(2)/100)];
x03 = [x0(3)*(1-sign(x0(3))*percentage(3)/100),x0(3)*(1+sign(x0(3))*percentage(3)/100)];
X0 = interval([x01(1);x02(1);x03(1)],[x01(2);x02(2);x03(2)]);
X0 = conZonotope(X0);
%% State Estimation
xk{steps} = [];
yk{steps} = [];
YK{steps} = [];
if(flagZB)
XbarZB{steps} = [];
XhatZB{steps} = [];
end
if(flagCZ)
XbarCZ{steps} = [];
XhatCZ{steps} = [];
end
if(flagMVT)
XbarMVT{steps} = [];
XhatMVT{steps} = [];
end
if(flagREGO)
XbarREGO{steps} = [];
XhatREGO{steps} = [];
end
if(flagREGOMVT)
XbarREGOMVT{steps} = [];
XhatREGOMVT{steps} = [];
end
if(flagCombined)
XbarCombined{steps} = [];
XhatCombined{steps} = [];
end
xs{steps} = [];
xk{1} = x0;
yk{1} = Outfunc(xk{1},v0(:,1));
YK{1} = yk{1}+(-Dv*V);
if(flagZB)
fprintf('\n ZB: %d\n',1);
XbarZB{1} = X0;
tic;XhatZB{1} = zb_update(XbarZB{1},YK{1},v0(:,1));toc
end
if(flagCZ)
fprintf('\n CZ: %d\n',1);
XbarCZ{1} = X0;
tic;XhatCZ{1} = cz_update(XbarCZ{1},YK{1},v0(:,1));toc
end
if(flagMVT)
fprintf('\n MVT: %d\n',1);
XbarMVT{1} = X0;
tic;XhatMVT{1} = MVT_update(XbarMVT{1},YK{1},v0(:,1));toc
end
if(flagREGO)
fprintf('\n REGO: %d\n',1);
XbarREGO{1} = X0;
tic;XhatREGO{1} = Rego_update(Outfunc,XbarREGO{1},YK{1},v0(:,1),number_of_states);toc;
end
if(flagREGOMVT)
fprintf('\n REGO+MVT: %d\n',1);
XbarREGOMVT{1} = X0;
if(flagMVT)
XhatREGOMVT{1} = XhatMVT{1};
else
XhatREGOMVT{1} = MVT_update(XbarREGOMVT{1},YK{1},v0(:,1));
end
end
clear functions
if(flagCombined)
fprintf('\n Combined: %d\n',1);
XbarCombined{1} = X0;
if(flagZB);c{1} = XhatZB{1};end
if(flagCZ);c{2} = XhatCZ{1};end
if(flagMVT);c{3} = XhatMVT{1};end
if(flagREGO);c{4} = XhatREGO{1};end
c = c(~cellfun('isempty',c));
XhatCombined{1} = mptPolytope(c{1});
for m = 2:length(c)
XhatCombined{1} = removeRedundancies(and(mptPolytope(c{m}),XhatCombined{1}));
end
XhatCombined{1} = conZonotope(XhatCombined{1});
end
clear functions
I0 = zonotope(interval(XhatZB{1}));
size_samples = 100; % Increasing samples will take a long time
xi = (-1+2.*rand(number_of_states,size_samples));
xs0 = [];
for i=1:length(xi)
x=I0.Z*[1;xi(:,i)];
if in(XhatZB{1},x)
xs0 = [xs0, x];
end
end
xs{1} = xs0;
figure(1);hold on;grid on
if(flagZB);plot(XhatZB{1},[1,2],'r');end
if(flagCZ);plot(XhatCZ{1},[1,2],'g');end
if(flagMVT);plot(XhatMVT{1},[1,2],'b');end
if(flagREGO);plot(XhatREGO{1},[1,2],'color',[0.9290, 0.6940, 0.1250]);end
if(flagREGOMVT);plot(XhatREGOMVT{1},[1,2],'c');end
if(flagCombined);plot(XhatCombined{1},[1,2],'m');end
scatter(xs{1}(1,:),xs{1}(2,:),10,'ko');
drawnow;
%%
for k = 2:steps
xk{k} = func(xk{k-1},w0(:,k));
yk{k} = Outfunc(xk{k},v0(:,k));
YK{k} = yk{k}+(-Dv*V);
xs{k} = zeros(number_of_states ,0);
for ii=1:size(xs{k-1},2)
xt=func(xs{k-1}(:,ii),w0(:,k));
if in(YK{k},Outfunc(xt,v0(:,k)))
xs{k} = [xs{k},xt];
end
end
if(flagZB)
% Zonotope Bundle Method
fprintf('\n ZB: %d\n',k);
tic;XbarZB{k} = zb_prediction(XhatZB{k-1},w0(:,k));toc;
tic;XhatZB{k} = zb_update(XbarZB{k},YK{k},v0(:,k));toc;
end
clear functions
if(flagCZ)
% Constrained Zonotopes Method
fprintf('\n CZ: %d\n',k);
tic;XbarCZ{k} = cz_prediction(XhatCZ{k-1},w0(:,k));toc
tic;XhatCZ{k} = cz_update(XbarCZ{k},YK{k},v0(:,k));toc;
end
clear functions
if(flagMVT)
% Mean Value Theorem
fprintf('\n MVT: %d\n',k);
XbarMVT{k} = MVT_prediction(XhatMVT{k-1},w0(:,k));toc
XhatMVT{k} = MVT_update(XbarMVT{k},YK{k},v0(:,k));toc;
end
clear functions
if(flagREGO)
% Rego Prediction Update
fprintf('\n REGO: %d\n',k);
tic;XhatREGO{k-1} = conZonotope(mptPolytope(XhatREGO{k-1}));
XbarREGO{k} = Rego_prediction(func,XhatREGO{k-1},w0(:,k),WM,3);toc
tic;XbarREGO{k} = conZonotope(mptPolytope(XbarREGO{k}));
XhatREGO{k} = Rego_update(Outfunc,XbarREGO{k},YK{k},v0(:,k),number_of_states);toc;
end
clear functions
if(flagREGOMVT)
% Rego Prediction + MVT Update
fprintf('\n REGO+MVT: %d\n',k);
% XhatREGOMVT{k-1} = conZonotope(XhatREGOMVT{k-1});
tic;XbarREGOMVT{k} = Rego_prediction(func,XhatREGOMVT{k-1},w0(:,k),WM,3);toc
tic;XbarREGOMVT{k} = conZonotope(mptPolytope(XbarREGOMVT{k}));
XhatREGOMVT{k} = MVT_update(XbarREGOMVT{k},YK{k},v0(:,k));toc;
XhatREGOMVT{k} = conZonotope(XhatREGOMVT{k});
end
clear functions
if(flagCombined)
% Combined
fprintf('\n Combined: %d\n',k);
tic;
XhatCombined{k-1} = conZonotope(mptPolytope(XhatCombined{k-1}));
if(flagZB);a{1} = zb_prediction(XhatCombined{k-1},w0(:,k));end
if(flagCZ);a{2} = cz_prediction(XhatCombined{k-1},w0(:,k));end
if(flagMVT);a{3} = MVT_prediction(XhatCombined{k-1},w0(:,k));end
if(flagREGO)
a{4} = Rego_prediction(func,XhatCombined{k-1},w0(:,k),WM,3);
a{4} = conZonotope(mptPolytope(a{4}));
end
% a{3} = zonoBundle(mvx_prediction(XhatCombined{k-1},w0(:,k)));
% a{4} = zonoBundle(Rego_prediction(func,XhatCombined{k-1},w0(:,k),WM,3));
a = a(~cellfun('isempty',a));
XbarCombined{k} = mptPolytope(a{1});
for m = 2:length(a)
XbarCombined{k} = removeRedundancies(and(mptPolytope(a{m}),XbarCombined{k}));
end
XbarCombined{k} = conZonotope(XbarCombined{k});
toc;
tic;
if(flagZB);b{1} = zb_update(XbarCombined{k},YK{k},v0(:,k));end
if(flagCZ);b{2} = cz_update(XbarCombined{k},YK{k},v0(:,k));end
if(flagMVT);b{3} = MVT_update(XbarCombined{k},YK{k},v0(:,k));end
if(flagREGO)
XbarCombined{k} = conZonotope(mptPolytope(XbarCombined{k}));
b{4} = Rego_update(Outfunc,XbarCombined{k},YK{k},v0(:,k),number_of_states);
b{4} = conZonotope(mptPolytope(b{4}));
end
b = b(~cellfun('isempty',b));
XhatCombined{k} = mptPolytope(b{1});
for m = 2:length(b)
XhatCombined{k} = removeRedundancies(and(mptPolytope(b{m}),XhatCombined{k}));
end
XhatCombined{k} = conZonotope(XhatCombined{k});
toc;
end
clear functions
% Plots
if(flagZB);plot(XhatZB{k},[1,2],'r');end
if(flagCZ);plot(XhatCZ{k},[1,2],'g');end
if(flagMVT);plot(XhatMVT{k},[1,2],'b');end
if(flagREGO);plot(XhatREGO{k},[1,2],'color',[0.9290, 0.6940, 0.1250]);end
if(flagREGOMVT);plot(XhatREGOMVT{k},[1,2],'c');end
if(flagCombined);plot(XhatCombined{k},[1,2],'m');end
scatter(xs{k}(1,:),xs{k}(2,:),10,'ko');
drawnow;
end
xlabel('Sx');ylabel('Sy');title('Unicycle Robot Trajectory');
%%
lgdE = [flagZB;flagCZ;flagMVT;flagREGO;flagREGOMVT;flagCombined];
lgdM = {'Zonotope Bundle Method',...
'Constrained Zonotope Method',...
'Mean Value Theorem',...
'REGO',...
'REGO Prediction + MVT Update',...
'Combination'};
for x = 1:length(lgdE)
if(lgdE(x) == 1)
Legend{x} = lgdM{x};
end
end
Legend = Legend(~cellfun('isempty',Legend));
legend(Legend,'Location','best');