summaryrefslogtreecommitdiff
path: root/modules/cacsd/macros/h2norm.sci
blob: 6a0483f7979d5843f09d97f39f2e1ff9a67ce693 (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
// Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
// Copyright (C) INRIA -
//
// 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 [nh]=h2norm(g,tol)
    //
    //                 /+00
    //     2           |          *
    //  |g| =1/(2*%pi).|trace[g(jw).g(jw)]dw
    //     2           |
    //                 /-00
    if argn(2)<1 then
        error(msprintf(gettext("%s: Wrong number of input argument(s): At least %d expected.\n"),..
        "h2norm",1))
    end

    if type(g)==1,if norm(g)==0,nh=0,return,end,end,
    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"),"h2norm",1))
    end
    if g.dt<>"c" then
        error(msprintf(gettext("%s: Wrong type for argument #%d: In continuous time expected.\n"),"h2norm",1))
    end

    [lhs,rhs]=argn(0),
    if rhs==1 then
        tol=1000*%eps,
    else
        if type(tol)<>1|size(tol,"*")<>1 then
            error(msprintf(gettext("%s: Wrong type for input argument: Scalar expected.\n"),"h2norm",2))
        end
        if ~isreal(tol)|tol<=0 then
            error(msprintf(gettext( "%s: Input argument #%d must be strictly positive.\n"),"h2norm",2))
        end
    end;
    select typeof(g)
    case "state-space" then
        if norm(g.D)>0 then
            error(msprintf(gettext("%s: Wrong value for input argument #%d: Proper system expected.\n"),"h2norm",1)),
        end;
        sp=spec(g.A),
        if max(real(sp))>=-tol then
            error(msprintf(gettext("%s: Wrong value for input argument #%d: Stable system expected.\n"),"h2norm",1)),
        end,
        w=obs_gram(g.A,g.C,"c"),
        nh=abs(sqrt(sum(diag(g.B'*w*g.B)))),return,
    case "rational" then
        num=g.num;
        den=g.den;
        s=poly(0,varn(den)),
        [t1,t2]=size(num),
        for i=1:t1,
            for j=1:t2,
                n=num(i,j),d=den(i,j),
                if coeff(n)==0 then
                    nh(i,j)=0,
                else
                    if degree(n)>=degree(d) then
                        error(msprintf(gettext("%s: Wrong value for input argument #%d: Proper system expected.\n"),"h2norm",1)),
                    end
                    pol=roots(d),
                    if max(real(pol))>-tol then
                        error(msprintf(gettext("%s: Wrong value for input argument #%d: Stable system expected.\n"),"h2norm",1)),
                    end,
                    nt=horner(n,-s),dt=horner(d,-s),
                    nh(i,j)=residu(n*nt,d,dt),
                end,
            end
        end
        nh=sqrt(sum(nh)),return,
    end
endfunction