summaryrefslogtreecommitdiff
path: root/stepest.sci
blob: 6310971af087be624be2348bc8ebea98d878e1f7 (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
function varargout = stepest(varargin)
    [lhs,rhs] = argn(0)
    //checking the number of inputs
    if rhs > 2  then
        error(msprintf(gettext("%s: Unexpected number of input arguments "),"stepest"))
    end
    modelData = varargin(1)
    if typeof(modelData) <> "idpoly" then
        error(msprintf(gettext("%s: Plant model must be ""idpoly"" type. "),"stepest"))
    end
    //adding noise
    if rhs == 2 then
        noiseFlag = varargin(2)
        if typeof(noiseFlag) <> 'boolean' then
            error(msprintf(gettext("%s: Last input data must be ""boolean"".type "),"stepest"))
        end
    else
        noiseFlag = %F
    end
    z = poly(0,'z')
    aPoly = poly(modelData.a(length(modelData.a):-1:1),'z','coeff')
    bPoly = poly(modelData.b,'z','coeff')
    fPoly = poly(modelData.f(length(modelData.f):-1:1),'z','coeff')
    afPoly = aPoly*fPoly
    
    bCoeff = modelData.b
    extra = 1
    if ~bCoeff(1,1) then
        afCoeff = coeff(afPoly)
        bLength = length(bCoeff);afLength = length(afCoeff)
        if bLength == afLength then
            extra = 1
        else
            extra = z^-(bLength-afLength)
        end
    end
    
    bPoly = poly(modelData.b(length(modelData.b):-1:1),'z','coeff')
    sys = syslin('d',bPoly,afPoly)*extra
    if size(find(gsort(abs(roots(sys.den)))>1),'*') then
        tempRoot = clean(roots(sys.den))
        [sorted index] = gsort(abs(real(tempRoot)))
        tempRoot = tempRoot(index)
        n = 26/log10(abs(real(tempRoot(1))))
         
    else
        finalValue = horner(sys,1)*1
        n = 10
        tempValue = sum(ldiv(sys.num,sys.den,n))
        if finalValue >= 0 then
            while finalValue*0.99 >= tempValue || n >=1000
                n = n+10
                tempValue = sum(ldiv(sys.num,sys.den,n))
            end
        elseif finalValue < 0 then
            while finalValue*0.99 <tempValue || n >=1000
                n = n+10
                tempValue = sum(ldiv(sys.num,sys.den,n))
            end  
        end
    end
    uData = ones(1,n)
    yData = flts(uData,sys)
    timeData = (0:(n-1))*modelData.Ts
    if noiseFlag then
        varargout(1) = yData'
    else
        plot2d2(timeData,yData,2)
        varargout(1) = []
    end
endfunction