[Scilab-users] ?==?utf-8?q? plotting a function that changes with x

Antoine Monmayrant amonmayr at laas.fr
Sun Feb 9 10:33:29 CET 2020


Hello,

I think that (x>18) is not doing what you think it does.
See below two options to define  y piecewise.
Hope it helps,

Antoine

----------------------

//your code
clf
  x=(0:1:72)
// here you x<18 is not what your think (it's a boolean vector)
if x<18
then y = sin(x*%pi/24)
else
     y= 0.1*x
end
plot2d(x,y,2)

// two options

// using the fact that x>18 is converted into
// 1 when the condition is true
// 0 when the condition is false
y2 =  sin(x*%pi/24).*(x>18) + 0.1*x.*(x<=18);

plot(x,y2,"ro");

// using a range of indices & find
y3=0.1*x;
inds=find(x>18);
y3(inds)=sin(x(inds)*%pi/24);

plot(x,y3,"g");

----------------------

 
 
 
Le Dimanche, Février 09, 2020 08:56 CET, Lloyd Judd <ltjudd at bigpond.com> a écrit: 
 
> Hello, 
> I am trying make a plot, the function of which changes depending on the 
> value of x,. What I am really trying to do is to simulate solar panel 
> out put where it roughly follows a sine  function during daylight but is 
> basically zero from sunset to sunrise, and then later overlay that with 
> the demand curve. But I am still experimenting with the code at this 
> stage.
>  I tried all different ways;  using the "function" command and the "plot 
> 2d" command . It only ever seems  to plot the last argument in the "if 
> then else" statement.
> For example with this code;
> clf
>    x=(0:1:72)
>   if x<18
>       then y = sin(x*%pi/24)
>   else
>       y= 0.1*x
>    end
>   plot2d(x,y,2)it only plots  the y=0.1x partConversely if I put the 
> sine function under the "else" statement, it only plots the  sin 
> function.clfx=(0:1:72)
> if x<18 then
>      y=0.01*x
> else
>      y=sin(x*%pi/24)
> end
>   plot2d(x,y,2)What is the best way to plot a function that changes 
> depending on the range of x?Thanks and regardsLloyd Judd
>




More information about the users mailing list