Hi,
with the if-loop-approach you probably thought about this:

x=(0:1:72)for i=1:length(x)
    if ((x(i)<18) == %T)                   // elementwise comparison
        then y(i) = sin(x(i)*%pi/24)
    else
        y(i)= 0.1*x(i)
    endendplot2d(x,y,2)

However it's a if-loop within a for loop...
Best regards,
Philipp

Am So., 9. Feb. 2020 um 10:34 Uhr schrieb Antoine Monmayrant <
amonm...@laas.fr>:

> 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 <ltj...@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
> >
>
> _______________________________________________
> users mailing list
> users@lists.scilab.org
> http://lists.scilab.org/mailman/listinfo/users
>
_______________________________________________
users mailing list
users@lists.scilab.org
http://lists.scilab.org/mailman/listinfo/users

Reply via email to