[Scilab-users] plotting a function that changes with x

classic Classic list List threaded Threaded
4 messages Options
Lloyd Judd Lloyd Judd
Reply | Threaded
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*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 regards
Lloyd Judd

_______________________________________________
users mailing list
[hidden email]
http://lists.scilab.org/mailman/listinfo/users
Antoine Monmayrant-2 Antoine Monmayrant-2
Reply | Threaded
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
der_Phil der_Phil
Reply | Threaded
Open this post in threaded view
|

Re: ?==?utf-8?q? plotting a function that changes with x

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,
Philipp

Am 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
Lloyd Judd Lloyd Judd
Reply | Threaded
Open this post in threaded view
|

Re: ?==?utf-8?q? plotting a function that changes with x



Merci Phillip,Antoine,

Now I have something very close to what I am looking for

Thanks for your help

regards

Lloyd



------ Original Message ------
From: "P M" <[hidden email]>
To: "Users mailing list for Scilab" <[hidden email]>
Sent: Monday, 10 Feb, 2020 At 9:46 AM
Subject: Re: [Scilab-users] ?==?utf-8?q? plotting a function that changes with x

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,
Philipp

Am 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