-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAnsPWQElp.m
280 lines (225 loc) · 6.76 KB
/
AnsPWQElp.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
function [pwasys,pwactrl] = AnsPWQElp(pwasys, option)
% Copyright: Mohsen Zamani Fekri, Concordia University April 2011
%
%
[NR NS] = size(pwasys.Abar); % Number of Systems, Number of Regions
n = size(pwasys.Abar{1},1)-1; % Number of state variables
m = size(pwasys.Bbar{1},2); % Number of inputs
alpha=option.alpha;
istar = [];
A = pwasys.A;
a = pwasys.a;
B = pwasys.B;
F = pwasys.F;
f = pwasys.f;
K=pwasys.K;
k=pwasys.k;
xcl=pwasys.xcl;
pwatype=pwasys.type;
if strcmp(pwatype, 'lower-envelope')
col_index=[1];
elseif strcmp(pwatype, 'pwadi')
col_index=[1 2];
elseif strcmp(pwatype, 'null')
col_index=[];
end
L=length(col_index);
%% Regions equations with ellipsiodal approximations
% R=norm(E*z +e) < 1
if ~isfield(pwasys, 'EpA') | ~isfield(pwasys, 'Epb')
pwasys=MinElp(pwasys);
end
for i=1:NR
E{i}=pwasys.EpA{i};
e{i}=pwasys.Epb{i};
end
%% extracting system data in seperated form from pwasys cell
for i=1:NR,
xcl_is_inside_Ri = all(pwasys.E{i}*xcl+pwasys.e{i}>=0-1e-7);
if xcl_is_inside_Ri,
istar = union(istar,i); % Center region(s)
end
end
region_excluded_istar=setdiff([1:NR], istar);
%% shifting the equations with respect to the equilibrium point
% shifting a{i,j}
for i=1:NR,
for j=col_index,
a{i,j} = a{i,j}+ A{i,j}*xcl;
end
end
% shifting the intersection equations R_i \cap R_j= F_{ij}*s+f_{ij}
% F{i,j} remains the same. Only f{i,j} changes
for i=1:NR
for j=1:NR
if ~isempty(f{i,j})
f{i,j} = f{i,j}-xcl;
end
end
end
% shifting the ellipsidal regions equations. only e changes, E remains the
% same.
for i=1:NR
e{i}=e{i}+ E{i}*xcl;
sys_E{i}=pwasys.E{i};
sys_e{i}=pwasys.E{i}*xcl+pwasys.e{i};
end
%% building a shifted "pwasys" model.
for i=1:NR
shifted_model.Ebar{i}=pwasys.Ebar{i};
shifted_model.Ebar{i}(1:end-1, 1:end)=[sys_E{i} sys_e{i}];
for j=col_index
shifted_model.Abar{i,j}=pwasys.Abar{i,j};
shifted_model.Abar{i,j}(1:n, n+1)=a{i,j};
end
end
for i=1:NR
for j=1:NR
shifted_model.Fbar{i,j}=[];
if ~isempty(f{i,j})
shifted_model.Fbar{i,j}=pwasys.Fbar{i,j};
shifted_model.Fbar{i,j}(1:n, n)=f{i,j};
end
end
end
shifted_model.Bbar=pwasys.Bbar;
shifted_model.type=pwatype;
for i=1:NR
k{i}=K{i}*xcl+k{i}; % shifting the controller from x to z
end
%% YALMIP initialzation
yalmip('clear');
constraints=set([]);
disp('Defining Matrix P_i for Lyapunov function')
%% defining varibales P_i for a piecewise-quadratic lyapunov
%% function
NR=size(pwasys.Abar,1);
Num_S=size(pwasys.Abar{1,1},1);
% converting MXN=0 equations to a unique AY=0 equation
[reshaping,lyap_info]=LyapContGeneral(shifted_model);
istar=[];
for i_P=1:NR,
xcl_is_inside_Ri = all(pwasys.Ebar{i_P}*[xcl;1]>=0-1e-7);
if xcl_is_inside_Ri,
istar = union(istar,i_P); % Center region(s)
end
end
clear p_var P_vec P_mtrx
unrptd_var_length=0;
for x=1:Num_S
unrptd_var_length=unrptd_var_length+x;
end
p_temp={};
p_var=sdpvar(1, NR*unrptd_var_length);
tot_index_P=1:size(lyap_info.S, 2); % indices of total variables
indp_index_P=lyap_info.d; % if B is fixed lower and upper boundry have the same result. we have chosen lower boundry for our alculation
depn_index_P=setdiff(tot_index_P, indp_index_P); % dependent variable
for m_P=indp_index_P
p_var(m_P)=0;
end
for m_P=depn_index_P
p_var(m_P)=sdpvar(1);
p_temp{size(p_temp,2)+1}=p_var(m_P);
if norm(lyap_info.S(:,m_P))~=0
[base_index_P, non_impt, base_coef]=find(lyap_info.w(:,m_P));
for k_P=1:length(base_index_P)
b_indx_indp=base_index_P(k_P); % index_P of the independent vector
b_indx_P=lyap_info.d(b_indx_indp);
val_indx_P=base_coef(k_P); % coefficient by which the independent
p_var(b_indx_P)=p_var(b_indx_P)-val_indx_P*(p_var(m_P)); % linear
end
end
end
for i_P=1:NR
P_vec{i_P}=p_var((i_P-1)*unrptd_var_length+1:i_P*unrptd_var_length);
end
for i_P=1:NR
P_mtrx{i_P}=sdpvar(Num_S);
for j_P=1:unrptd_var_length
r_P=reshaping(j_P ,2);
q_P=reshaping(j_P ,3);
P_mtrx{i_P}(r_P,q_P)=P_vec{i_P}(reshaping(j_P,1));
P_mtrx{i_P}(q_P,r_P)=P_vec{i_P}(reshaping(j_P,1));
end
end
%% end of defining variables
for i=1:NR
P{i}=P_mtrx{i}(1:end-1, 1:end-1);
q{i}=P_mtrx{i}(1:end-1, end);
r{i}=P_mtrx{i}(end,end);
end
for i=region_excluded_istar
miu{i}=sdpvar(1);
bita{i}=sdpvar(1);
end
%% Central Region equations: Y, Z and DV
%inequality for negative definitness vdot < alpha * v
for i=istar
for j=col_index
P_11 = P{i}*(A{i,j}+B{i,j}*K{i})+(A{i,j}+B{i,j}*K{i})'*P{i}+alpha*P{i};
DV{i, j}=[P_11];
end
end
for i=istar
PV{i}=[P{i} zeros(n,1); zeros(1, n) r{i}];
end
%% Regions exculded istar: DV
for i=region_excluded_istar
for j=col_index
P_11 = P{i}*(A{i,j}+B{i,j}*K{i})+(A{i,j}+B{i,j}*K{i})'*P{i}+alpha*P{i}...
+miu{i}*E{i}'*E{i};
P_12 = P{i}*(a{i,j}+B{i,j}*k{i})+miu{i}*E{i}'*e{i};
P_21 = P_12';
P_22 = -miu{i}*(1-e{i}'*e{i});
q_11=zeros(n);
q_12=(A{i,j}+B{i,j}*K{i})'*q{i};
q_21=q_12';
q_22=2*q{i}'*(a{i,j}+B{i,j}*k{i});
DV{i, j}=[P_11 P_12; P_21 P_22]+[q_11 q_12; q_21 q_22];
end
end
for i=region_excluded_istar
PV_11 = P{i}-bita{i}*E{i}'*E{i};
PV_12 = -bita{i}*E{i}'*e{i};
PV_21 = PV_12';
PV_22 = bita{i}*(1-e{i}'*e{i});
qv_11=zeros(n);
qv_12=q{i};
qv_21=qv_12';
qv_22=r{i};
PV{i}=[PV_11 PV_12; PV_21 PV_22]+[qv_11 qv_12; qv_21 qv_22];
end
constraints=set([]);
for i=1:NR
constraints=constraints+set(PV{i}>0,['PV' num2str(i) '>0']);
for j=col_index
constraints=constraints+set(DV{i,j}<0,['DV' num2str(i) '-' num2str(j) '<0']);
end
end
for i=region_excluded_istar
constraints=constraints+set(miu{i}<0,['miu' num2str(i) '<0']);
constraints=constraints+set(bita{i}<0,['bita' num2str(i) '<0']);
end
%
%% solution
u=solvesdp(constraints);
[u1 u2]=checkset(constraints);
pwactrl.problem=u.problem;
pwactrl.u1=u1;
pwactrl.u2=u2;
pwactrl.constraints=constraints;
pwactrl.xcl = xcl;
% pwactrl.istar = istar;
for i=1:NR
P{i}=double(P{i});
q{i}=double(q{i});
r{i}=double(r{i});
pwactrl.Pbar{i} = [P{i} q{i}; q{i}' r{i}];
end
for i=region_excluded_istar
miu{i}=double(miu{i});
bita{i}=double(bita{i});
pwactrl.miu{i} = miu{i};
pwactrl.bita{i} = bita{i};
end
pwactrl.Ebar = pwasys.Ebar;