Search code examples
scilabpid-controller

How to simulate a disturbance in scilab program (not xcos)


I need to simulate a disturbance in a control system using scilab, that is, the csim function is used to simulate the response of a system by using a step, impulse, ramp or any other input, but, I need to input a disturbance for example in t = 0.5s to see the system behavior.

That drags another problem to me because I don't know how to make csim or syslin to acknowledge two different inputs, or its as simple as defining two systems, one with the referent input and other with the disturbance entrance and sum both?.


Solution

  • Suppose you have the following linear time invariant system (A,B,C)

    x'=A*x+B1*v+B2*d
    y=C*x
    

    with B=[B1,B2], where v is the control/input and d the disturbance. If you want e.g. simulate a step response and a disturbance you have to define your own overall input [v;d] and decide when to apply the disturbance. Here is an example:

    function ud = step(t)
        ud = [1;0];
    endfunction
    
    function ud = input(t)
        ud = zeros(t);
        ud(1,:) = 1;
        s = abs(t-0.5);
        // 0.2 is the half-width of disturbance
        ud(2,:) = -0.1*(1-s/0.2).*(s<0.2)
    endfunction
    
    A = [-2 1;
          1 -2];
    B1 = [1;0];
    B2 = [0;4];
    C = [0 1];
    
    sl = syslin('c',A,[B1 B2],C);
    t = linspace(0,5,1000);
    
    x = csim(step,t,sl)
    xd = csim(input,t,sl)
    
    clf
    plot(t,x,t,xd,t,input(t)(2,:))
    legend('step','step and disturbance','disturbance',2)
    

    I made here two csim calls, one for the usual step response and the second one for the perturbed step response. However, I warn you about the ode solver used by csim: discontinuous inputs can be easily missed, that's why I applied here a hat-shaped disturbance. The code of the perturbed input is designed to allow vector time input, in order to easily plot the perturbation. enter image description here