]> Creatis software - CreaPhase.git/blob - octave_packages/control-2.3.52/@lti/prescale.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / control-2.3.52 / @lti / prescale.m
1 ## Copyright (C) 2011   Lukas F. Reichlin
2 ##
3 ## This file is part of LTI Syncope.
4 ##
5 ## LTI Syncope is free software: you can redistribute it and/or modify
6 ## it under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation, either version 3 of the License, or
8 ## (at your option) any later version.
9 ##
10 ## LTI Syncope is distributed in the hope that it will be useful,
11 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 ## GNU General Public License for more details.
14 ##
15 ## You should have received a copy of the GNU General Public License
16 ## along with LTI Syncope.  If not, see <http://www.gnu.org/licenses/>.
17
18 ## -*- texinfo -*-
19 ## @deftypefn {Function File} {[@var{scaledsys}, @var{info}] =} prescale (@var{sys})
20 ## Prescale state-space model.
21 ## Frequency response commands perform automatic scaling unless model property
22 ## @var{scaled} is set to @var{true}.
23 ##
24 ## @strong{Inputs}
25 ## @table @var
26 ## @item sys
27 ## LTI model.
28 ## @end table
29 ##
30 ## @strong{Outputs}
31 ## @table @var
32 ## @item scaledsys
33 ## Scaled state-space model.
34 ## @item info
35 ## Structure containing additional information.
36 ## @item info.SL
37 ## Left scaling factors.  @code{Tl = diag (info.SL)}.
38 ## @item info.SR
39 ## Right scaling factors.  @code{Tr = diag (info.SR)}.
40 ## @end table
41 ##
42 ## @strong{Equations}
43 ## @example
44 ## @group
45 ## Es = Tl * E * Tr
46 ## As = Tl * A * Tr
47 ## Bs = Tl * B
48 ## Cs =      C * Tr
49 ## Ds =      D
50 ## @end group
51 ## @end example
52 ##
53 ## For proper state-space models, @var{Tl} and @var{Tr} are inverse of each other.
54 ##
55 ## @strong{Algorithm}@*
56 ## Uses SLICOT TB01ID and TG01AD by courtesy of
57 ## @uref{http://www.slicot.org, NICONET e.V.}.
58 ## @end deftypefn
59
60 ## Author: Lukas Reichlin <lukas.reichlin@gmail.com>
61 ## Created: June 2011
62 ## Version: 0.2
63
64 function [retsys, varargout] = prescale (sys)
65
66   if (nargin != 1)
67     print_usage ();
68   endif
69
70   if (! isa (sys, "ss"))
71     warning ("prescale: system not in state-space form");
72     sys = ss (sys);
73   endif
74
75   [retsys, lscale, rscale] = __prescale__ (sys);
76   
77   if (nargout > 1)
78     varargout{1} = struct ("SL", lscale, "SR", rscale);
79   endif
80
81 endfunction
82
83
84 ## Scaling of state-space models, direct access to TB01ID
85 %!shared Ao, Bo, Co, SCALEo, MAXREDo, Ae, Be, Ce, SCALEe, MAXREDe
86 %! A = [          0.0  1.0000e+000          0.0          0.0          0.0
87 %!       -1.5800e+006 -1.2570e+003          0.0          0.0          0.0
88 %!        3.5410e+014          0.0 -1.4340e+003          0.0 -5.3300e+011
89 %!                0.0          0.0          0.0          0.0  1.0000e+000
90 %!                0.0          0.0          0.0 -1.8630e+004 -1.4820e+000 ];
91 %!
92 %! B = [          0.0          0.0
93 %!        1.1030e+002          0.0
94 %!                0.0          0.0
95 %!                0.0          0.0
96 %!                0.0  8.3330e-003 ];
97 %!
98 %! C = [  1.0000e+000          0.0          0.0          0.0          0.0
99 %!                0.0          0.0  1.0000e+000          0.0          0.0
100 %!                0.0          0.0          0.0  1.0000e+000          0.0
101 %!        6.6640e-001          0.0 -6.2000e-013          0.0          0.0
102 %!                0.0          0.0 -1.0000e-003  1.8960e+006  1.5080e+002 ];
103 %!
104 %! MAXRED = 0.0;
105 %!
106 %! [Ao, Bo, Co, MAXREDo, SCALEo] = sltb01id (A, B, C, MAXRED);
107 %!
108 %! Ae = [    0.0000000D+00  0.1000000D+05  0.0000000D+00  0.0000000D+00  0.0000000D+00
109 %!          -0.1580000D+03 -0.1257000D+04  0.0000000D+00  0.0000000D+00  0.0000000D+00
110 %!           0.3541000D+05  0.0000000D+00 -0.1434000D+04  0.0000000D+00 -0.5330000D+03
111 %!           0.0000000D+00  0.0000000D+00  0.0000000D+00  0.0000000D+00  0.1000000D+03
112 %!           0.0000000D+00  0.0000000D+00  0.0000000D+00 -0.1863000D+03 -0.1482000D+01 ];
113 %!
114 %! Be = [    0.0000000D+00  0.0000000D+00
115 %!           0.1103000D+04  0.0000000D+00
116 %!           0.0000000D+00  0.0000000D+00
117 %!           0.0000000D+00  0.0000000D+00
118 %!           0.0000000D+00  0.8333000D+02 ];
119 %!
120 %! Ce = [    0.1000000D-04  0.0000000D+00  0.0000000D+00  0.0000000D+00  0.0000000D+00
121 %!           0.0000000D+00  0.0000000D+00  0.1000000D+06  0.0000000D+00  0.0000000D+00
122 %!           0.0000000D+00  0.0000000D+00  0.0000000D+00  0.1000000D-05  0.0000000D+00
123 %!           0.6664000D-05  0.0000000D+00 -0.6200000D-07  0.0000000D+00  0.0000000D+00
124 %!           0.0000000D+00  0.0000000D+00 -0.1000000D+03  0.1896000D+01  0.1508000D-01 ];
125 %!
126 %! SCALEe = [0.1000000D-04  0.1000000D+00  0.1000000D+06  0.1000000D-05  0.1000000D-03 ];
127 %!
128 %! MAXREDe = 0.3488E+10;
129 %!
130 %!assert (Ao, Ae, 1e-4);
131 %!assert (Bo, Be, 1e-4);
132 %!assert (Co, Ce, 1e-4);
133 %!assert (MAXREDo, MAXREDe, 1e6);
134 %!assert (SCALEo, SCALEe.', 1e-4);
135
136
137 ## Scaling of descriptor state-space models, direct access to TG01AD
138 %!shared Ao, Eo, Bo, Co, LSCALEo, RSCALEo, Ae, Ee, Be, Ce, LSCALEe, RSCALEe
139 %! A = [   -1         0         0    0.003
140 %!          0         0    0.1000    0.02
141 %!        100        10         0    0.4
142 %!          0         0         0    0.0];
143 %!
144 %! E = [    1       0.2         0    0.0
145 %!          0         1         0    0.01
146 %!        300        90         6    0.3
147 %!          0         0        20    0.0];
148 %!
149 %! B = [   10         0
150 %!          0         0
151 %!          0      1000
152 %!      10000     10000];
153 %!
154 %! C = [ -0.1      0.0    0.001    0.0
155 %!        0.0      0.01  -0.001    0.0001];
156 %!
157 %! TRESH = 0.0;
158 %!
159 %! [Ao, Eo, Bo, Co, LSCALEo, RSCALEo] = sltg01ad (A, E, B, C, TRESH);
160 %!
161 %! Ae = [ -1.0000    0.0000    0.0000    0.3000
162 %!         0.0000    0.0000    1.0000    2.0000
163 %!         1.0000    0.1000    0.0000    0.4000
164 %!         0.0000    0.0000    0.0000    0.0000];
165 %!
166 %! Ee = [  1.0000    0.2000    0.0000    0.0000
167 %!         0.0000    1.0000    0.0000    1.0000
168 %!         3.0000    0.9000    0.6000    0.3000
169 %!         0.0000    0.0000    0.2000    0.0000 ];
170 %!
171 %! Be = [100.0000    0.0000
172 %!         0.0000    0.0000
173 %!         0.0000  100.0000
174 %!       100.0000  100.0000 ];
175 %!
176 %! Ce = [ -0.0100    0.0000    0.0010    0.0000
177 %!         0.0000    0.0010   -0.0010    0.0010];
178 %!
179 %! LSCALEe = [ 10.0000   10.0000    0.1000    0.0100 ];
180 %!
181 %! RSCALEe = [  0.1000    0.1000    1.0000   10.0000 ];
182 %!
183 %!assert (Ao, Ae, 1e-4);
184 %!assert (Eo, Ee, 1e-4);
185 %!assert (Bo, Be, 1e-4);
186 %!assert (Co, Ce, 1e-4);
187 %!assert (LSCALEo, LSCALEe.', 1e-4);
188 %!assert (RSCALEo, RSCALEe.', 1e-4);
189
190
191 ## Scaling of state-space models, user function
192 %!shared Ao, Bo, Co, INFOo, Ae, Be, Ce, SCALEe
193 %! A = [          0.0  1.0000e+000          0.0          0.0          0.0
194 %!       -1.5800e+006 -1.2570e+003          0.0          0.0          0.0
195 %!        3.5410e+014          0.0 -1.4340e+003          0.0 -5.3300e+011
196 %!                0.0          0.0          0.0          0.0  1.0000e+000
197 %!                0.0          0.0          0.0 -1.8630e+004 -1.4820e+000 ];
198 %!
199 %! B = [          0.0          0.0
200 %!        1.1030e+002          0.0
201 %!                0.0          0.0
202 %!                0.0          0.0
203 %!                0.0  8.3330e-003 ];
204 %!
205 %! C = [  1.0000e+000          0.0          0.0          0.0          0.0
206 %!                0.0          0.0  1.0000e+000          0.0          0.0
207 %!                0.0          0.0          0.0  1.0000e+000          0.0
208 %!        6.6640e-001          0.0 -6.2000e-013          0.0          0.0
209 %!                0.0          0.0 -1.0000e-003  1.8960e+006  1.5080e+002 ];
210 %!
211 %! SYS = ss (A, B, C);
212 %!
213 %! [SYSo, INFOo] = prescale (SYS);
214 %!
215 %! [Ao, Bo, Co] = ssdata (SYSo);
216 %!
217 %! Ae = [    0.0000000D+00  0.1000000D+05  0.0000000D+00  0.0000000D+00  0.0000000D+00
218 %!          -0.1580000D+03 -0.1257000D+04  0.0000000D+00  0.0000000D+00  0.0000000D+00
219 %!           0.3541000D+05  0.0000000D+00 -0.1434000D+04  0.0000000D+00 -0.5330000D+03
220 %!           0.0000000D+00  0.0000000D+00  0.0000000D+00  0.0000000D+00  0.1000000D+03
221 %!           0.0000000D+00  0.0000000D+00  0.0000000D+00 -0.1863000D+03 -0.1482000D+01 ];
222 %!
223 %! Be = [    0.0000000D+00  0.0000000D+00
224 %!           0.1103000D+04  0.0000000D+00
225 %!           0.0000000D+00  0.0000000D+00
226 %!           0.0000000D+00  0.0000000D+00
227 %!           0.0000000D+00  0.8333000D+02 ];
228 %!
229 %! Ce = [    0.1000000D-04  0.0000000D+00  0.0000000D+00  0.0000000D+00  0.0000000D+00
230 %!           0.0000000D+00  0.0000000D+00  0.1000000D+06  0.0000000D+00  0.0000000D+00
231 %!           0.0000000D+00  0.0000000D+00  0.0000000D+00  0.1000000D-05  0.0000000D+00
232 %!           0.6664000D-05  0.0000000D+00 -0.6200000D-07  0.0000000D+00  0.0000000D+00
233 %!           0.0000000D+00  0.0000000D+00 -0.1000000D+03  0.1896000D+01  0.1508000D-01 ];
234 %!
235 %! SCALEe = [0.1000000D-04  0.1000000D+00  0.1000000D+06  0.1000000D-05  0.1000000D-03 ];
236 %!
237 %!assert (Ao, Ae, 1e-4);
238 %!assert (Bo, Be, 1e-4);
239 %!assert (Co, Ce, 1e-4);
240 %!assert (INFOo.SL.^-1, SCALEe.', 1e-4);
241 %!assert (INFOo.SR, SCALEe.', 1e-4);
242
243
244 ## Scaling of descriptor state-space models, user function
245 %!shared Ao, Eo, Bo, Co, INFOo, Ae, Ee, Be, Ce, LSCALEe, RSCALEe
246 %! A = [   -1         0         0    0.003
247 %!          0         0    0.1000    0.02
248 %!        100        10         0    0.4
249 %!          0         0         0    0.0];
250 %!
251 %! E = [    1       0.2         0    0.0
252 %!          0         1         0    0.01
253 %!        300        90         6    0.3
254 %!          0         0        20    0.0];
255 %!
256 %! B = [   10         0
257 %!          0         0
258 %!          0      1000
259 %!      10000     10000];
260 %!
261 %! C = [ -0.1      0.0    0.001    0.0
262 %!        0.0      0.01  -0.001    0.0001];
263 %!
264 %! SYS = dss (A, B, C, [], E);
265 %!
266 %! [SYSo, INFOo] = prescale (SYS);
267 %!
268 %! [Ao, Bo, Co, ~, Eo] = dssdata (SYSo); 
269 %!
270 %! Ae = [ -1.0000    0.0000    0.0000    0.3000
271 %!         0.0000    0.0000    1.0000    2.0000
272 %!         1.0000    0.1000    0.0000    0.4000
273 %!         0.0000    0.0000    0.0000    0.0000];
274 %!
275 %! Ee = [  1.0000    0.2000    0.0000    0.0000
276 %!         0.0000    1.0000    0.0000    1.0000
277 %!         3.0000    0.9000    0.6000    0.3000
278 %!         0.0000    0.0000    0.2000    0.0000 ];
279 %!
280 %! Be = [100.0000    0.0000
281 %!         0.0000    0.0000
282 %!         0.0000  100.0000
283 %!       100.0000  100.0000 ];
284 %!
285 %! Ce = [ -0.0100    0.0000    0.0010    0.0000
286 %!         0.0000    0.0010   -0.0010    0.0010];
287 %!
288 %! LSCALEe = [ 10.0000   10.0000    0.1000    0.0100 ];
289 %!
290 %! RSCALEe = [  0.1000    0.1000    1.0000   10.0000 ];
291 %!
292 %!assert (Ao, Ae, 1e-4);
293 %!assert (Eo, Ee, 1e-4);
294 %!assert (Bo, Be, 1e-4);
295 %!assert (Co, Ce, 1e-4);
296 %!assert (INFOo.SL, LSCALEe.', 1e-4);
297 %!assert (INFOo.SR, RSCALEe.', 1e-4);