Search code examples
scilab

How to use Octave/Matlab Inline function in Scilab


How to use this Octave/Matlab inline function in Scilab?

u = inline('t>=0') where t=0:0.001:1.
y = (u(t-0.2)-u(t-0.3)) 

I tried it in Scilab as

deff('[u]=f(t)','u=(t>=0)')

But I am getting an error as "invalid index" in determining 'y'.


Solution

  • Unlike in Matlab, invoking (t>=0) gives you a boolean vector, its entries are True or False. Since you want 1s and 0s, you need bool2s to convert from boolean to integers:

    deff('[u]=f(t)','u=bool2s(t>=0)')
    

    After that you can invoke the function like any other:

    t = 0:0.001:1
    y = u(t-0.2)-u(t-0.3)
    plot(t,y)
    

    plot

    Personally, I never see the need for inline functions in Scilab. If I were writing the above, I would declare the function normally:

    function y = u(t)
        y = bool2s(t>=0)
    endfunction
    

    Unlike Matlab, Scilab allows you to have such functions appear wherever you want them in the script.