<div dir="ltr"><div dir="ltr"><div>Hi,<br></div><div>with the if-loop-approach you probably thought about this:<br></div><div><pre style="font-family:Monospaced;font-size:12px;font-style:normal"><span style="color:rgb(0,0,0)">x</span><span style="color:rgb(92,92,92)">=</span><span style="color:rgb(74,85,219)">(</span><span style="color:rgb(188,143,143)">0</span><span style="color:rgb(255,170,0)">:</span><span style="color:rgb(188,143,143)">1</span><span style="color:rgb(255,170,0)">:</span><span style="color:rgb(188,143,143)">72</span><span style="color:rgb(74,85,219)">)</span>
<span style="color:rgb(160,32,240)">for</span> <span style="color:rgb(0,0,0)">i</span><span style="color:rgb(92,92,92)">=</span><span style="color:rgb(188,143,143)">1</span><span style="color:rgb(255,170,0)">:</span><span style="color:rgb(50,185,185)">length</span><span style="color:rgb(74,85,219)">(</span><span style="color:rgb(0,0,0)">x</span><span style="color:rgb(74,85,219)">)</span>
<span style="color:rgb(160,32,240)">if</span> <span style="color:rgb(74,85,219)">(</span><span style="color:rgb(74,85,219)">(</span><span style="color:rgb(0,0,0)">x</span><span style="color:rgb(74,85,219)">(</span><span style="color:rgb(0,0,0)">i</span><span style="color:rgb(74,85,219)">)</span><span style="color:rgb(92,92,92)"><</span><span style="color:rgb(188,143,143)">18</span><span style="color:rgb(74,85,219)">)</span> <span style="color:rgb(92,92,92)">==</span> <span style="color:rgb(218,112,214)">%T</span><span style="color:rgb(74,85,219)">) // elementwise comparison</span>
<span style="color:rgb(160,32,240)">then</span> <span style="color:rgb(0,0,0)">y</span><span style="color:rgb(74,85,219)">(</span><span style="color:rgb(0,0,0)">i</span><span style="color:rgb(74,85,219)">)</span> <span style="color:rgb(92,92,92)">=</span> <span style="color:rgb(50,185,185)">sin</span><span style="color:rgb(74,85,219)">(</span><span style="color:rgb(0,0,0)">x</span><span style="color:rgb(74,85,219)">(</span><span style="color:rgb(0,0,0)">i</span><span style="color:rgb(74,85,219)">)</span><span style="color:rgb(92,92,92)">*</span><span style="color:rgb(218,112,214)">%pi</span><span style="color:rgb(92,92,92)">/</span><span style="color:rgb(188,143,143)">24</span><span style="color:rgb(74,85,219)">)</span>
<span style="color:rgb(160,32,240)">else</span>
<span style="color:rgb(0,0,0)">y</span><span style="color:rgb(74,85,219)">(</span><span style="color:rgb(0,0,0)">i</span><span style="color:rgb(74,85,219)">)</span><span style="color:rgb(92,92,92)">=</span> <span style="color:rgb(188,143,143)">0.1</span><span style="color:rgb(92,92,92)">*</span><span style="color:rgb(0,0,0)">x</span><span style="color:rgb(74,85,219)">(</span><span style="color:rgb(0,0,0)">i</span><span style="color:rgb(74,85,219)">)</span>
<span style="color:rgb(160,32,240)">end</span>
<span style="color:rgb(160,32,240)">end</span>
<span style="color:rgb(50,185,185)">plot2d</span><span style="color:rgb(74,85,219)">(</span><span style="color:rgb(0,0,0)">x</span><span style="color:rgb(0,0,0)">,</span><span style="color:rgb(0,0,0)">y</span><span style="color:rgb(0,0,0)">,</span><span style="color:rgb(188,143,143)">2</span><span style="color:rgb(74,85,219)">)</span></pre>However it's a if-loop within a for loop...<br>Best regards,<br>Philipp</div></div></div><br><div class="gmail_quote"><div class="gmail_attr" dir="ltr">Am So., 9. Feb. 2020 um 10:34 Uhr schrieb Antoine Monmayrant <<a href="mailto:amonmayr@laas.fr">amonmayr@laas.fr</a>>:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;padding-left:1ex;border-left-color:rgb(204,204,204);border-left-width:1px;border-left-style:solid">Hello,<br>
<br>
I think that (x>18) is not doing what you think it does.<br>
See below two options to define y piecewise.<br>
Hope it helps,<br>
<br>
Antoine<br>
<br>
----------------------<br>
<br>
//your code<br>
clf<br>
x=(0:1:72)<br>
// here you x<18 is not what your think (it's a boolean vector)<br>
if x<18<br>
then y = sin(x*%pi/24)<br>
else<br>
y= 0.1*x<br>
end<br>
plot2d(x,y,2)<br>
<br>
// two options<br>
<br>
// using the fact that x>18 is converted into<br>
// 1 when the condition is true<br>
// 0 when the condition is false<br>
y2 = sin(x*%pi/24).*(x>18) + 0.1*x.*(x<=18);<br>
<br>
plot(x,y2,"ro");<br>
<br>
// using a range of indices & find<br>
y3=0.1*x;<br>
inds=find(x>18);<br>
y3(inds)=sin(x(inds)*%pi/24);<br>
<br>
plot(x,y3,"g");<br>
<br>
----------------------<br>
<br>
<br>
<br>
<br>
Le Dimanche, Février 09, 2020 08:56 CET, Lloyd Judd <<a href="mailto:ltjudd@bigpond.com" target="_blank">ltjudd@bigpond.com</a>> a écrit: <br>
<br>
> Hello, <br>
> I am trying make a plot, the function of which changes depending on the <br>
> value of x,. What I am really trying to do is to simulate solar panel <br>
> out put where it roughly follows a sine function during daylight but is <br>
> basically zero from sunset to sunrise, and then later overlay that with <br>
> the demand curve. But I am still experimenting with the code at this <br>
> stage.<br>
> I tried all different ways; using the "function" command and the "plot <br>
> 2d" command . It only ever seems to plot the last argument in the "if <br>
> then else" statement.<br>
> For example with this code;<br>
> clf<br>
> x=(0:1:72)<br>
> if x<18<br>
> then y = sin(x*%pi/24)<br>
> else<br>
> y= 0.1*x<br>
> end<br>
> plot2d(x,y,2)it only plots the y=0.1x partConversely if I put the <br>
> sine function under the "else" statement, it only plots the sin <br>
> function.clfx=(0:1:72)<br>
> if x<18 then<br>
> y=0.01*x<br>
> else<br>
> y=sin(x*%pi/24)<br>
> end<br>
> plot2d(x,y,2)What is the best way to plot a function that changes <br>
> depending on the range of x?Thanks and regardsLloyd Judd<br>
><br>
<br>
_______________________________________________<br>
users mailing list<br>
<a href="mailto:users@lists.scilab.org" target="_blank">users@lists.scilab.org</a><br>
<a href="http://lists.scilab.org/mailman/listinfo/users" target="_blank" rel="noreferrer">http://lists.scilab.org/mailman/listinfo/users</a><br>
</blockquote></div>