-
Notifications
You must be signed in to change notification settings - Fork 5
/
PWACrtNon.m
340 lines (330 loc) · 12.9 KB
/
PWACrtNon.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
338
339
340
clear model pwasys
echo on;
clc
%
% *************************************************************************
% CREATING THE MODEL (NONLINEAR)
% *************************************************************************
%
% PWATOOL starts with importing a nonlinear or piecewise-affine model. If
% the model is nonlinear PWATOOL approximates it with a PWA model.
%
% References
% (1) B. Samadi and L. Rodrigues. Extension of local linear controllers to
% global piecewise affine controllers for uncertain non-linear systems.
% International Journal of Systems Science, 39(9):867-879, 2008.
%
pause; %strike any key to continue
%
% Consider a nonlinear model
%
% dx/dt=A x + a + f(x) + B(x) u
%
% which consists of a linear dynamics A, an affine term a, a nonlinear
% dynamics f(x) and input gain B(x).
%
% In order to show how PWATOOL imports the above nonlinear model, we use an
% an example from (L. Rodrigues and S. Boyd, 2005); we model a nonlinear
% resistor and later approximate it by PWADIs.
%
pause; %strike any key to continue
clc
% |-----------------------------------------------------------------------|
% | Creating a m-file for writing the data of the nonlinear model |
% |-----------------------------------------------------------------------|
%
% The nonlinear resistor example from (L. Rodrigues and S. Boyd, 2005) is
% of the order two (n=2) and has one input (m=1).
%
% Therefore, to start creating the nonlinear model we type
%
% pwacreate(2, 1, 'nonlinear_resistor_non.m')
%
pause; %strike any key to continue
%
% or in general
%
% pwacreate(n, m, 'myfilename.m')
%
% where myfilename.m is the m-file which is to store the nonlinear
% model. By doing so, 'myfilename.m' will be created (if does not exist
% before) or rewritten (if existed before).
%
% Now open 'nonlinear_resistor_non.m' which we created previously. We show
% how it has been filled.
%
pause; %strike any key to continue
clc
%
open nonlinear_resistor_non.m
%
%
% nonlinear_resistor_non.m has two sections of which, only, Section 1,
% should be filled by the user. Section 2 should remain as is to let the
% code be executed after completion.
%
pause; %strike any key to continue
clc
% |-----------------------------------------------------------------------|
% | model.A : linear dynamics |
% |-----------------------------------------------------------------------|
%
% model.A is a matrix of size (n x n). For the nonlinear resistor example
% we have
%
model.A=[-30 -20
.05 0];
%
%
pause; %strike any key to continue
clc
% |-----------------------------------------------------------------------|
% | model.aff : affine term |
% |-----------------------------------------------------------------------|
%
% model.aff is column vector of size (n x 1). For the nonlinear resistor
% example we have
%
model.aff=[24
0];
%
%
pause; %strike any key to continue
clc
% |-----------------------------------------------------------------------|
% | model.Bx : input gain |
% |-----------------------------------------------------------------------|
%
% model. Bx is a matrix of size (n x m). It can be a variable of class
% double or a function handle (please hit number 1 in the main menu of
% PWATOOL for more details). For the nonlinear example we have
%
model.Bx=[20
0];
%
%
pause; %strike any key to continue
clc
% |-----------------------------------------------------------------------|
% | model.Domain : domain of the variables |
% |-----------------------------------------------------------------------|
%
% model.Domain is a cell of (1 x n). For the nonlinear resistor example we
% have
%
model.Domain={[-20 20],[-2e4 2e4]};
%
pause; %strike any key to continue
clc
% |-----------------------------------------------------------------------|
% | model.fx : nonlinear dynamics |
% |-----------------------------------------------------------------------|
%
% model.fx denotes the function handle which generates the nonlinearity in
% the model. For instance, for the nonlinear resistor example, we write
%
model.fx=@resistor_nonlinearity;
%
% to mean that 'resistor_nonlinearity.m' file contains the code which for
% each value of X=[x(1); x(2);...;x(n)] produces a column vector of size
% (n x 1).
%
pause; %strike any key to continue
%
% In our example 'resistor_nonlinearity.m' is coded as follows:
%
%
% ---------------------------------------
% | function F = resistor_nonlinearity(X) |
% | x=X(2); |
% | if (-2e4 <= x & x< .2) |
% | F=[0; -50*(1e-3/.2*x)]; |
% | elseif (.2 <= x & x<.6) |
% | F=[0; -50*(-2e-3*x+1.4e-3)]; |
% | elseif (.6 <= x & x <= 2e4) |
% | F=[0; -50*(4e-3*x-2.2e-3)]; |
% | end |
% ---------------------------------------
%
% Note that the output of the function should be a column vector of size
% (n x 1). Hence some elements of vector 'F' above are set to zero since
% they are not set by the nonlinearity dynamics.
%
pause; %strike any key to continue
clc
% |-----------------------------------------------------------------------|
% | model.NonlinearDomain : domain of the nonlinear dynamics |
% |-----------------------------------------------------------------------|
%
% model.NonlinearDomain is a row vector of size (1 x n) which shows which
% variables appear in the nonlinear dynamics. For every variable x(j) which
% appear in the nonlinear dynamics model.fx we put a '1' at the j-th place
% of vector model.NonlinearDomain.
%
% For the nonlinear resistor example write
%
model.NonlinearDomain=[0;1];
%
% to mean that model.fx is a function of only x(2).
%
pause; %strike any key to continue
clc
% |-----------------------------------------------------------------------|
% | model.xcl: equilibrium point |
% |-----------------------------------------------------------------------|
%
% model.xcl is a column vector of size (n x 1) and shows the "desired"
% equilibrium point which we need in analysis or synthesis. For the
% nonlinear resistor example we have
model.xcl=[0.371428571428570
0.642857142857146];
%
%
pause; %strike any key to continue
clc
%
% |-----------------------------------------------------------------------|
% | model.NR: number of regions |
% |-----------------------------------------------------------------------|
%
% model.NR is a vector of size (1 x ND) where ND is the number of the
% variables which appear in model.fx. Elements of model.NR show the number
% of the regions which we like to have at each direction x(j) which appear
% in model.fx.
%
% For the nonlinear resistor example we have
%
model.NR=3;
%
% If model.NR is given as a scalar, all of the variables x(j) which
% appear in model.fx will have the same number of regions in their
% corresponding direction.
%
pause; %strike any key to continue
clc
%
% |-----------------------------------------------------------------------|
% | model.mtd: gridding type |
% |-----------------------------------------------------------------------|
%
% model.mtd is a string which takes a value from
% {'Uniform', 'OptimalUniform', 'Multiresolution'}. It shows the method
% that PWATOOL will use to grid the domain. For the nonlinear resistor
% exaplme we write
%
model.mtd='Multiresolution';
%
%
pause; %strike any key to continue
clc
% |-----------------------------------------------------------------------|
% | model.Rstar: Rstar region |
% |-----------------------------------------------------------------------|
%
% model.Rstar is matrix of size (* x ND) where ND is the number of
% variables which appear in model.fx and * denotes an optional number. For
% the nonlinear resistor example that ND=1 we write
%
model.Rstar=[.2 ; .6];
%
%
% By specifying above Rstar and since the total number of the regions will
% be 3, the regions which we obtain will be R1=[-2e4 .2], R2=[.2 .6]
% and R3=[.6 2e4]
%
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% % MODELING IS COMPLETE %
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
pause; %strike any key to continue
clc
% |-----------------------------------------------------------------------|
% | Checking and saving the data
% |-----------------------------------------------------------------------|
%
% Now, save the changes in the file 'nonlinear_resistor_non.m' and run it
% by pressing F5 or typing 'run nonlinear_resistor_pwa'.
%
% PWATOOL imports the data that user has entered and checks for dimension
% consistency between the model parameters by calling the function
%
% [Err, model]=NonCheckModel(model);
%
pause; %strike any key to continue
[Err, model]=NonCheckModel(model);
%
pause; %strike any key to continue
%
% If there is no error (Err=0), function 'PWAComp' is called to build the
% PWADI approximation.
%
% [pwainc, pwasys, nlsys]=PWAComp(model);
%
%
pause; %strike any key to continue
[pwainc, pwasys, nlsys]=PWAComp(model);
%
pause; %strike any key to continue
clc
% |-----------------------------------------------------------------------|
% | Outputs and results
% |-----------------------------------------------------------------------|
%
% If PWATOOL accepts the data that user has entered, it computes the PWADI
% approximation for the nonlinear system and prints a message for the user.
%
% PWATOOL, generates three outputs.
%
pause; %strike any key to continue
clc
% First, the original nonlinear system is saved in a variable called
% 'nlsys'. The following table shows the fields of 'nlsys' which, in fact,
% user has entered.
%
% |------------------------------------------------------------------------
% |PARAMETER VARIABLE
% |------------------------------------------------------------------------
% |A model.A
% |a model.aff
% |f(x) model.fx
% |B(x) model.Bx
% |xcl model.xcl
% |Domain model.Domain
% |Gridding Type model.mtd
% |Rstar model.Rstar
% |------------------------------------------------------------------------
%
pause; %strike any key to continue
clc
% Second, PWADI approximation is stored in a variable called 'pwainc'. A
% similar PWA approximation (without envelops) is also stored in a variable
% called 'pwasys'. The table below shows the fields in 'pwainc' or 'pwasys'
%
% |------------------------------------------------------------------------
% |PARAMETER STRUCTURE REPRESENTRS Size of Each
% | Matrix / Vector
% |------------------------------------------------------------------------
% |A NR x 2 cell Linear dynamics n x n
% |a NR x 2 cell affine term n x 1
% |B NR x 2 cell Input gain n x m
% |E NR x 1 cell Regions equation * x n
% |e NR x 1 cell Regions equations * x 1
% |F NR x NR cell Boundary equations n x (n-1)
% |f NR x NR cell Boundary equations n x 1
% |Abar NR x 2 cell Linear dynamics (n+1) x (n+1)
% |Bbar NR x 2 cell Input gain (n+1) x m
% |Ebar NR x 1 cell Regions equation * x (n+1)
% |Fbar NR x NR cell Boundary equations (n+1) x (n+1)
% |NR 1 x ** array Number of regions 1 x **
% |------------------------------------------------------------------------
% *: The number of rows in matrix E and vector e depends on the gridding
% type. If the system is slab, then [2, n]=size(E); if the gridding type is
% uniform and the system is not slab, then [n+1, n]=size(E) and
% [n+1, 1]=size(e).
%
% **: NR could be a scalar or a row vector of size 1 x ND where ND is the
% number of variables which appear in f(x).
%
pause; %strike any key to continue
echo off