1 ## Copyright (C) 2011 Lukas F. Reichlin
3 ## This file is part of LTI Syncope.
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.
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.
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/>.
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}.
33 ## Scaled state-space model.
35 ## Structure containing additional information.
37 ## Left scaling factors. @code{Tl = diag (info.SL)}.
39 ## Right scaling factors. @code{Tr = diag (info.SR)}.
53 ## For proper state-space models, @var{Tl} and @var{Tr} are inverse of each other.
55 ## @strong{Algorithm}@*
56 ## Uses SLICOT TB01ID and TG01AD by courtesy of
57 ## @uref{http://www.slicot.org, NICONET e.V.}.
60 ## Author: Lukas Reichlin <lukas.reichlin@gmail.com>
64 function [retsys, varargout] = prescale (sys)
70 if (! isa (sys, "ss"))
71 warning ("prescale: system not in state-space form");
75 [retsys, lscale, rscale] = __prescale__ (sys);
78 varargout{1} = struct ("SL", lscale, "SR", rscale);
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 ];
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 ];
106 %! [Ao, Bo, Co, MAXREDo, SCALEo] = sltb01id (A, B, C, MAXRED);
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 ];
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 ];
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 ];
126 %! SCALEe = [0.1000000D-04 0.1000000D+00 0.1000000D+06 0.1000000D-05 0.1000000D-03 ];
128 %! MAXREDe = 0.3488E+10;
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);
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
154 %! C = [ -0.1 0.0 0.001 0.0
155 %! 0.0 0.01 -0.001 0.0001];
159 %! [Ao, Eo, Bo, Co, LSCALEo, RSCALEo] = sltg01ad (A, E, B, C, TRESH);
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];
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 ];
171 %! Be = [100.0000 0.0000
174 %! 100.0000 100.0000 ];
176 %! Ce = [ -0.0100 0.0000 0.0010 0.0000
177 %! 0.0000 0.0010 -0.0010 0.0010];
179 %! LSCALEe = [ 10.0000 10.0000 0.1000 0.0100 ];
181 %! RSCALEe = [ 0.1000 0.1000 1.0000 10.0000 ];
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);
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 ];
203 %! 0.0 8.3330e-003 ];
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 ];
211 %! SYS = ss (A, B, C);
213 %! [SYSo, INFOo] = prescale (SYS);
215 %! [Ao, Bo, Co] = ssdata (SYSo);
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 ];
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 ];
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 ];
235 %! SCALEe = [0.1000000D-04 0.1000000D+00 0.1000000D+06 0.1000000D-05 0.1000000D-03 ];
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);
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
261 %! C = [ -0.1 0.0 0.001 0.0
262 %! 0.0 0.01 -0.001 0.0001];
264 %! SYS = dss (A, B, C, [], E);
266 %! [SYSo, INFOo] = prescale (SYS);
268 %! [Ao, Bo, Co, ~, Eo] = dssdata (SYSo);
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];
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 ];
280 %! Be = [100.0000 0.0000
283 %! 100.0000 100.0000 ];
285 %! Ce = [ -0.0100 0.0000 0.0010 0.0000
286 %! 0.0000 0.0010 -0.0010 0.0010];
288 %! LSCALEe = [ 10.0000 10.0000 0.1000 0.0100 ];
290 %! RSCALEe = [ 0.1000 0.1000 1.0000 10.0000 ];
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);