Search code examples
functionplotinterpolationscilabpiecewise

Scilab plotting functions with if


I have a problen in scilab How can I plot functions containing if and < like

function y = alpha(t)
   if (t < 227.8) then 
       y = 0.75;
   elseif (t < 300) then
       y = 2.8 - 0.009 .* t;
   else
       y = 0.1;
   end   
endfunction

and

function [r]=minus_alpha(t)
    r = 1 - alpha(t)
endfunction

When I use

x = linspace(0,300)
plot(x, alpha(x))

I got the error message

WARNING: Transposing row vector X to get compatible dimensions
plot2d: falsche Größe für Eingangsargument: inkompatible Größen.
Error 999 : in plot2d called by plot

Sorry for german mix. Thank you.


Solution

  • If you check the output of your alpha(x), you will see that it is just a scalar (not a vector). I guess you wanted something like this, so it's necessary to iterate through t to compute each value of y based on the value of t:

    clc;
    clear;
    function y = alpha(t)
        for i=1:size(t,"*") 
            if t(i) < 227.8 then 
                y(i) = 0.75;
            elseif t(i) < 300 then
                y(i) = 2.8 - 0.009 * t(i);
            else
                y(i) = 0.1;
            end  
        end 
    endfunction
    
    x = linspace(0,300);
    plot2d(x,alpha(x));
    

    If you find the answer useful, please do not forget to accept it, so others will see that your problem is solved.