summaryrefslogtreecommitdiff
path: root/modules/cacsd/macros/gamitg.sci
blob: 4dc19bf9290277b9daed24763bfc721f8045dcf5 (plain)
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395

// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) INRIA - P. Gahinet
//
// This file must be used under the terms of the CeCILL.
// This source file is licensed as described in the file COPYING, which
// you should have received as part of this distribution.  The terms
// are also available at
// http://www.cecill.info/licences/Licence_CeCILL_V2.1-en.txt

function [gopt]=gamitg(g,r,PREC,options)
    //[gopt]=gamitg(G,r,PREC,options);
    //
    //  On input:
    //  ---------
    //     *  G contains the parameters of plant realization (syslin list)
    //           b = ( b1 , b2 ) ,     c = ( c1 ) ,    d = ( d11  d12)
    //                        ( c2 )          ( d21  d22)
    //     *  r(1) and r(2) are the dimensions of d22 (rows x columns)
    //     *  PREC is the desired relative accuracy on the norm
    //     *  available options are
    //        - 't': traces each bisection step, i.e., displays the lower
    //         and upper bounds and the current test point.
    //
    //  On output:
    //  ---------
    //     *  gopt:  optimal Hinf gain
    //
    //!
    //  History:
    //  -------
    //      author: P. Gahinet, INRIA
    //      last modification:
    //****************************************************************************
    // Copyright INRIA

    if and(typeof(g)<>["rational","state-space"]) then
        error(msprintf(gettext("%s: Wrong type for input argument #%d: Linear state space or a transfer function expected.\n"),"gamitg",1))
    end
    if g.dt<>"c" then
        error(msprintf(gettext("%s: Wrong value for input argument #%d: Continuous time system expected.\n"),"gamitg",1))
    end
    //user interface. The default values are:
    //     PREC=1.0e-3;      options='nul';
    //************************************************
    [lhs,rhs]=argn(0);
    select rhs,
    case 1 then
        error(msprintf(gettext("%s: Wrong number of input arguments: At least %d expected.\n"),"gamitg",2))
    case 2 then
        PREC=1.0e-3; options="nul";
    case 3 then
        if type(PREC)==10 then
            iopt=3
            options=PREC; PREC=1.0e-3;
        else
            options="nul";
        end,
    case 4 then
        iopt=4
    end
    if typeof(r)<>"constant"|~isreal(r) then
        error(msprintf(gettext("%s: Wrong type for argument #%d: Real vector expected.\n"),"gamitg",2))
    end
    if size(r,"*")<>2 then
        error(msprintf(gettext("%s: Wrong size for input argument #%d: %d expected.\n"),"gamitg",2,2))
    end
    r=int(r);
    if or(r<=0) then
        error(msprintf(gettext("%s: Wrong values for input argument #%d: Elements must be positive.\n"),"gamitg",2))
    end

    if type(options)<>10|and(options<>["t","nul"]) then
        error(msprintf(gettext("%s: Wrong value for input argument #%d: Must be in the set {%s}.\n"),"gamitg",iopt,"""t"",""nul"""))
    end
    if type(PREC)<>1|size(PREC,"*")<>1|~isreal(PREC)|PREC<=0 then
        error(msprintf(gettext("%s: Wrong value for input argument #%d: Must be a positive scalar.\n"),"gamitg",3))
    end

    //constants
    //*********
    INIT_LOW=1.0e-3; INIT_UPP=1.0e6;  INFTY=10e10;
    RELACCU=1.0e-10; //relative accuracy on generalized eigenvalue computations
    gopt=-1;

    //parameter recovery
    [a,b1,b2,c1,c2,d11,d12,d21,d22]=smga(g,r);

    //dimensions
    [na,na]=size(a); twona=2*na;
    [p1,m2]=size(d12),
    [p2,m1]=size(d21),

    //CHECK HYPOTHESES
    //****************

    if m2 > p1 then
        warning(msprintf(gettext("%s: Dimensions of %s are inadequate.\n"),"gamitg","D12"));
    end

    if p2 > m1 then
        warning(msprintf(gettext("%s: Dimensions of %s are inadequate.\n"),"gamitg","D21"));
    end

    [u12,s12,v12]=svd(d12);   s12=s12(1:m2,:);
    [u21,s21,v21]=svd(d21);   s21=s21(:,1:p2);
    u12=u12(:,1:m2);   //d12 = u12 s12 v12' with s12 square diagonal
    v21=v21(:,1:p2);   //d21 = u21 s21 v21'

    //rank condition on D12 and D21
    //-----------------------------
    if s12(m2,m2)/s12(1,1) <= 100*%eps then
        warning(msprintf(gettext("%s: %s is not full rank at the machine precision.\n"),"gamitg","D12"));
    end

    if s21(p2,p2)/s21(1,1) <= 100*%eps then
        warning(msprintf(gettext("%s: %s is not full rank at the machine precision.\n"),"gamitg","D21"));
    end

    //(A,B2,C2) stabilizable + detectable
    //-------------------------------------
    noa=max(abs(a));  nob2=max(abs(b2)); noc2=max(abs(c2));

    ns=st_ility(syslin("c",a,b2,c2),1.0e-10*max(noa,nob2));
    if ns<na then
        warning(msprintf(gettext("%s: %s is nearly unstabilizable.\n"),"gamitg","(A,B2)"));
    end

    nd=dt_ility(syslin("c",a,b2,c2),1.0e-10*max(noa,noc2));
    if nd>0 then
        warning(msprintf(gettext("%s: %s is nearly unstabilizable.\n"),"gamitg","(C2,A)"));
    end

    //Imaginary axis zeros: test the Hamiltonian spectra at gamma=infinity
    //-----------------------------------------------------------------

    ah=a-b2*v12/s12*u12'*c1;  bh=b2*v12; ch=u12'*c1;
    hg=[ah,-bh/(s12**2)*bh';ch'*ch-c1'*c1,-ah'];
    spech=spec(hg);
    if min(abs(real(spech))) < 1.0e-9*max(abs(hg)) then
        warning(msprintf(gettext("%s: %s has zero(s) near the imaginary axis.\n"),"gamitg","G12"));
    end


    ah=a-b1*v21/s21*u21'*c2;  ch=u21'*c2; bh=b1*v21;
    jg=[ah',-ch'/(s21**2)*ch;bh*bh'-b1*b1',-ah];
    specj=spec(jg);
    if min(abs(real(specj))) < 1.0e-9*max(abs(jg)) then
        warning(msprintf(gettext("%s: %s has zero(s) near the imaginary axis.\n"),"gamitg","G21"));
    end

    //COMPUTATION OF THE LOWER BOUND SIGMA_D
    //--------------------------------------
    if m2 < p1 then
        sig12=norm((eye(p1,p1)-u12*u12')*d11);
    else
        sig12=0;
    end

    if p2 < m1 then
        sig21=norm(d11*(eye(m1,m1)-v21*v21'));
    else
        sig21=0;
    end
    sigd=max(sig12,sig21);




    //SEARCH WINDOW INITIALIZATION
    //****************************
    // Initialize the search window [lower,upper] where `lower' and `upper'
    // are lower and upper bounds on the Linf norm of G. When no such
    // bounds are available, the window is arbitrarily set to [INIT_LOW,INIT_UPP]
    // and the variables LOW and UPP keep record of these initial values so that
    // the window can be extended if necessary.

    upper=INIT_UPP;     UPP=INIT_UPP;

    if sigd==0 then
        lower=INIT_LOW; LOW=INIT_LOW;
    else
        lower=sigd; LOW=0;
    end



    //HAMILTONIAN SETUP
    //------------------
    sh22=m1+m2;
    h11=[a,0*eye(a);-c1'*c1,-a'];
    h12=[-b1,-b2;c1'*d11,c1'*d12];
    h21=[d11'*c1,b1';d12'*c1,b2'];
    h22=eye(m1,m1); h22(sh22,sh22)=0;

    sj22=p1+p2;
    j11=[a',0*eye(a);-b1*b1',-a];
    j12=[-c1',-c2';b1*d11',b1*d21']
    j21=[d11*b1',c1;d21*b1',c2];
    j22=eye(p1,p1); j22(sj22,sj22)=0;

    d1112s=[d11,d12]'*[d11,d12];
    d1121s=[d11;d21]*[d11;d21]';


    T_EVALH=1; //while 1, Hamiltonian spectrum of H must be tested
    T_EVALJ=1; //while 1, Hamiltonian spectrum of J must be tested
    T_POSX=1;   //while 1, the nonnegativity of X must be tested
    T_POSY=1;   //while 1, the nonnegativity of Y must be tested



    //********************************************************
    //         GAMMA ITERATION STARTS
    //********************************************************

    for iterations=1:30,   //max iterations=30

        ga=sqrt(lower*upper);   //test point gamma = log middle of [lower,upper]
        gs=ga*ga;

        if str_member("t",options) then
            write(%io(2),[lower,ga,upper],"(''min,cur,max = '',3e20.10)");
        end

        // Search window management:
        //--------------------------
        //   If the gamma-iteration runs into one of the initial arbitrary bounds
        //   LOW or UPP, extend the search window to allow for continuation

        if ga<10*LOW then lower=LOW/10; LOW=lower; end
        // expand search window toward gamma<<1
        if ga>UPP/10 then upper=UPP*10; UPP=upper; end
        // expand search window toward gamma>>1




        DONE=0
        //DONE=1 indicates that the current gamma has been classified and
        //the next iteration can start


        //----------------------------------------------------------------------
        //  FIRST TEST : PURE IMAGINARY EIGENVALUES IN HAMILTONIAN SPECTRUM ?
        //----------------------------------------------------------------------

        hg=h11-(h12/(gs*h22-d1112s))*h21;

        if T_EVALH==1 then
            hev=spec(hg);
            if min(abs(real(hev))) <= RELACCU*max(abs(hev)) then
                lower=ga; DONE=1;   //Hg HAS EVAL ON iR -> NEXT LOOP
                if str_member("t",options) then
                    mprintf(gettext("%s: %s has pure imaginary eigenvalue(s).\n"),"gamitg","H");
                end
            end
        end


        if DONE==0 then
            jg=j11-(j12/(gs*j22-d1121s))*j21;

            if T_EVALJ==1 then
                jev=spec(jg);
                if min(abs(real(jev))) <= RELACCU*max(abs(jev)) then
                    lower=ga; DONE=1;   //Jg HAS EVAL ON iR -> NEXT LOOP
                    if str_member("t",options) then
                        mprintf(gettext("%s: %s has pure imaginary eigenvalue(s).\n"),"gamitg","J");
                    end
                end
            end
        end



        //---------------------------------------------------------
        //  SECOND TEST : NONNEGATIVITY OF THE RICCATI SOLUTIONS
        //---------------------------------------------------------


        if DONE==0 then

            //compute orthon. bases of the negative invariant subspace of H
            [uh,d]=schur(hg,"c");
            px=uh(1:na,1:na);   qx=uh(na+1:twona,1:na);

            //nonnegativity test for X (or Y):
            //--------------------------------
            //  * if |f(i)| < RELACCU then discard this eval (this corresponds
            //    to ||X||-> infty and the sign is ill-defined
            //  * if |e(i)| < RELACCU then X is nearly singular in this direction.
            //    We then consider X is actually singular and that the eval can be
            //    discarded
            //  * For the remaining entries, if e(i)/f(i) < - RELACC/100 then X is
            //    diagnosed as indefinite. Otherwise X is nonnegative.

            if T_POSX==1 then
                [e,f]=spec(qx,px);
                i=1;
                while i<=na,
                    if min(abs([e(i),f(i)])) >= RELACCU & real(e(i)/f(i)) <= 0 then
                        lower=ga;  DONE=1;  T_EVALH=0;  i=na+1;
                        if str_member("t",options) then
                            mprintf(gettext("%s: %s is indefinite.\n"),"gamitg","X");
                        end
                    else
                        i=i+1;
                    end
                end
            end
        end



        if DONE==0 then

            //compute orthon. bases of the negative inv. subspace of J
            [uj,d]=schur(jg,"c");
            py=uj(1:na,1:na);   qy=uj(na+1:twona,1:na);

            if T_POSY==1 then
                [e,f]=spec(qy,py);
                i=1;
                while i<=na,
                    if min(abs([e(i),f(i)])) >= RELACCU & real(e(i)/f(i)) <= 0 then
                        lower=ga;  DONE=1;  T_EVALJ=0;  i=na+1;
                        if str_member("t",options) then
                        mprintf(gettext("%s: %s is indefinite.\n"),"gamitg","Y");end;
                    else
                        i=i+1;
                    end
                end
            end
        end


        //--------------------------------------------------------------
        //  THIRD TEST : COMPARE THE SPECTRAL RADIUS OF XY WITH gamma**2
        //--------------------------------------------------------------


        if DONE==0 then

            [e,f]=spec(qy'*qx,py'*px);
            if max(real(e-gs*f)) <= 0 then
                upper=ga;
                if str_member("t",options) then
                write(%io(2),"rho(XY) <= gamma**2"); end
            else
                lower=ga; T_POSX=0; T_POSY=0; T_EVALH=0; T_EVALJ=0;
                if str_member("t",options) then
                write(%io(2),"rho(XY) > gamma**2"); end
            end
        end



        //------------------
        // TERMINATION TESTS
        //------------------

        if lower > INFTY then
            mprintf(gettext("%s: Controllable & observable mode(s) of %s near the imaginary axis.\n"),"gamitg","A");
            gopt=lower;
            return;
        else
            if upper < 1.0e-10 then
                gopt=upper;
                mprintf(gettext("%s: All modes of %s are nearly nonminimal so that %s is almost %d.\n"),"gamitg","A","|| G ||",0);
                return;
            else
                if 1-lower/upper < PREC,
                    gopt=sqrt(lower*upper);
                    return;
                end,
            end,
        end


    end//end while

    gopt=sqrt(lower*upper);

endfunction
function [bool]=str_member(c,s)
    //**********************
    // tests whether the character c occurs in the string s
    // returns %T (true) if it does, %F (false) otherwise.

    for i=1:length(s),
        if part(s,i)==c then bool=%t; return; end
    end
    bool=%f;
endfunction