]> Creatis software - CreaPhase.git/blob - octave_packages/control-2.3.52/__conred_sb16ad__.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / control-2.3.52 / __conred_sb16ad__.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{Kr}, @var{info}] =} __conred_sb16ad__ (@var{method}, @dots{})
20 ## Backend for btaconred and spaconred.
21 ## @end deftypefn
22
23 ## Author: Lukas Reichlin <lukas.reichlin@gmail.com>
24 ## Created: December 2011
25 ## Version: 0.1
26
27 function [Kr, info] = __conred_sb16ad__ (method, varargin)
28
29   if (nargin < 3)
30     print_usage ();
31   endif
32   
33   if (method != "bta" && method != "spa")
34     error ("modred: invalid method");
35   endif
36
37   G = varargin{1};
38   K = varargin{2};
39   varargin = varargin(3:end);
40   
41   if (! isa (G, "lti"))
42     error ("%sconred: first argument must be an LTI system", method);
43   endif
44
45   if (! isa (K, "lti"))
46     error ("%sconred: second argument must be an LTI system", method);
47   endif
48
49   if (nargin > 3)                                  # *conred (G, K, ...)
50     if (is_real_scalar (varargin{1}))              # *conred (G, K, nr)
51       varargin = horzcat (varargin(2:end), {"order"}, varargin(1));
52     endif
53     if (isstruct (varargin{1}))                    # *conred (G, K, opt, ...), *conred (G, K, nr, opt, ...)
54       varargin = horzcat (__opt2cell__ (varargin{1}), varargin(2:end));
55     endif
56     ## order placed at the end such that nr from *conred (G, K, nr, ...)
57     ## and *conred (G, K, nr, opt, ...) overrides possible nr's from
58     ## key/value-pairs and inside opt struct (later keys override former keys,
59     ## nr > key/value > opt)
60   endif
61
62   nkv = numel (varargin);                          # number of keys and values
63
64   if (rem (nkv, 2))
65     error ("%sconred: keys and values must come in pairs", method);
66   endif
67
68   [a, b, c, d, tsam, scaled] = ssdata (G);
69   [ac, bc, cc, dc, tsamc, scaledc] = ssdata (K);
70   [p, m] = size (G);
71   [pc, mc] = size (K);
72   dt = isdt (G);
73
74   if (p != mc || m != pc)
75     error ("%sconred: dimensions of controller (%dx%d) and plant (%dx%d) don't match", \
76            method, pc, mc, p, c);
77   endif
78
79
80   ## default arguments
81   alpha = __modred_default_alpha__ (dt);
82   tol1 = 0.0;
83   tol2 = 0.0;
84   jobc = jobo = 0;
85   bf = true;                                # balancing-free
86   weight = 3;
87   equil = scaled && scaledc;
88   ordsel = 1;
89   ncr = 0;
90   negfb = false;                            # positive feedback controller
91
92
93   ## handle keys and values
94   for k = 1 : 2 : nkv
95     key = lower (varargin{k});
96     val = varargin{k+1};
97     switch (key)
98       case "weight"
99         switch (lower (val(1)))
100           case "n"                          # none
101             weight = 0;
102           case {"l", "o"}                   # left, output
103             weight = 1;
104           case {"r", "i"}                   # right, input
105             weight = 2;
106           case {"b", "p"}                   # both, performance
107             weight = 3;
108           otherwise
109             error ("%sconred: '%s' is an invalid value for key weight", method, val);
110         endswitch
111
112       case {"order", "ncr", "nr"}
113         [ncr, ordsel] = __modred_check_order__ (val, rows (ac));
114
115       case "tol1"
116         tol1 = __modred_check_tol__ (val, "tol1");
117
118       case "tol2"
119         tol2 = __modred_check_tol__ (val, "tol2");
120
121       case "alpha"
122         alpha = __modred_check_alpha__ (val, dt);
123
124       case "method"
125         switch (tolower (val))
126           case "sr"
127             bf = false;
128           case "bfsr"
129             bf = true;
130           otherwise
131             error ("modred: '%s' is an invalid approach", val);
132         endswitch
133
134       case {"jobc", "gram-ctrb"}
135         jobc = __modred_check_gram__ (val, "gram-ctrb");
136
137       case {"jobo", "gram-obsv"}
138         jobo = __modred_check_gram__ (val, "gram-obsv");
139       
140       case {"equil", "equilibrate", "equilibration", "scale", "scaling"}
141         scaled = __modred_check_equil__ (val);
142
143       case "feedback"
144         negfb = __conred_check_feedback_sign__ (val);
145
146       otherwise
147         warning ("%sconred: invalid property name '%s' ignored", method, key);
148     endswitch
149   endfor
150
151   
152   ## handle model reduction approach
153   if (method == "bta" && ! bf)              # 'B':  use the square-root Balance & Truncate method
154     jobmr = 0;
155   elseif (method == "bta" && bf)            # 'F':  use the balancing-free square-root Balance & Truncate method
156     jobmr = 1;
157   elseif (method == "spa" && ! bf)          # 'S':  use the square-root Singular Perturbation Approximation method
158     jobmr = 2;
159   elseif (method == "spa" && bf)            # 'P':  use the balancing-free square-root Singular Perturbation Approximation method
160     jobmr = 3;
161   else
162     error ("%smodred: invalid jobmr option"); # this should never happen
163   endif
164
165   ## handle negative feedback controllers
166   if (negfb)
167     [ac, bc, cc, dc] = ssdata (-K);
168   endif
169
170   
171   ## perform model order reduction
172   [acr, bcr, ccr, dcr, ncr, hsvc, ncs] = slsb16ad (a, b, c, d, dt, equil, ncr, ordsel, alpha, jobmr, \
173                                                    ac, bc, cc, dc, \
174                                                    weight, jobc, jobo, tol1, tol2);
175
176   ## assemble reduced order controller
177   Kr = ss (acr, bcr, ccr, dcr, tsamc);
178
179   ## handle negative feedback controllers
180   if (negfb)
181     Kr = -Kr;
182   endif
183
184   ## assemble info struct  
185   info = struct ("ncr", ncr, "ncs", ncs, "hsvc", hsvc);
186
187 endfunction
188
189
190
191