# [Scilab-users] plotting a function that changes with x Classic List Threaded 4 messages Open this post in threaded view
|

## [Scilab-users] plotting a function that changes with x 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 part``Conversely if I put the sine function under the "else" statement, it only plots the sin function.``clf``x=(0:1:72)if x<18 then y=0.01*xelse 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 [hidden email] http://lists.scilab.org/mailman/listinfo/users
Open this post in threaded view
|

## Re: ?==?utf-8?q? plotting a function that changes with x 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 <[hidden email]> 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 [hidden email] http://lists.scilab.org/mailman/listinfo/users 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) end end plot2d(x,y,2)```However it's a if-loop within a for loop...Best regards,PhilippAm So., 9. Feb. 2020 um 10:34 Uhr schrieb Antoine Monmayrant <[hidden email]>: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 <[hidden email]> 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 [hidden email] http://lists.scilab.org/mailman/listinfo/users _______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users Merci Phillip,Antoine,Now I have something very close to what I am looking forThanks for your helpregardsLloyd------ Original Message ------From: "P M" <[hidden email]>To: "Users mailing list for Scilab" <[hidden email]>Sent: Monday, 10 Feb, 2020 At 9:46 AMSubject: Re: [Scilab-users] ?==?utf-8?q? plotting a function that changes with xHi,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,PhilippAm So., 9. Feb. 2020 um 10:34 Uhr schrieb Antoine Monmayrant <[hidden email]>: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 <[hidden email]> 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 [hidden email] http://lists.scilab.org/mailman/listinfo/users _______________________________________________users mailing list[hidden email]http://lists.scilab.org/mailman/listinfo/users_______________________________________________ users mailing list [hidden email] http://lists.scilab.org/mailman/listinfo/users Capture truncated sine curve.JPG (162K) Download Attachment