fooplot icon indicating copy to clipboard operation
fooplot copied to clipboard

How to draw a function with x>0 (piecewise function)?

Open q2apro opened this issue 9 years ago • 3 comments

I would like to draw a graph beginning from x=0 and then into the positive: http://fooplot.com/plot/bqb5rfvg9n

Function's equation is: ((21/20*x+1)^(1/2))*(x>0)

Problem is, even though I define (x>0) the drawing can be seen for negative x, starting by x = -1:

save

How comes?


Drawing f(x)=x with the limit to x>0 draws a red line onto the x-axis for negative x: http://fooplot.com/plot/s0x03w0mn9

In the FAQ I found:

How do I draw a piecewise function? You can enter a piecewise function using the comparison operators <, >, <=, >=, and == which return 1 if the comparison is true and 0 if the comparison is false. For example, (x<0) returns 1 if the statement (x<0) is true and 0 otherwise. To plot the piecewise function y={x, x<0; x^2, x>0} you could enter (x<0)*x+(x>0)*x^2.

q2apro avatar Apr 09 '15 13:04 q2apro

Update: I have tried adding (x<0)*0 but still the same problem.

(x<0)*0+((21/20*x+1)^(1/2))*(x>0)

Interestingly, when I remove the +1 from the equations above, the x=-1 is not drawn!

q2apro avatar Apr 09 '15 13:04 q2apro

This is a very good point. The reason is because x>0 returns 0 or 1, so the 0 is plotted. This makes these comparison operators useful for piecewise functions defined everywhere but less useful for piecewise functions that have undefined regions. There isn't a good way in the current framework to explicitly say "undefined for x = a ... b". I think this would be a great addition although some careful thought would be needed into how to structure this kind of input.

For the time being you could do something like this as a workaround, but of course it's only a hack-ish workaround to get the plot you need for now, not a long-term solution. ((21/20_x+1)^(1/2))_(x>0) + 0/(1-(x<0))

dheera avatar Apr 09 '15 13:04 dheera

Ah, I cannot use the hack "officially" otherwise some users will wonder what it means ;-) or worse, get used to it.

Maybe you could define the drawing area. For example, I implemented a dynamic zoom with fooplot here: http://www.echteinfach.tv/formeln/analysis/allplot/#ex4 - you just need to define the xmin,xmax,ymin,ymax as it can be found in your source code. I am using and parsing [[ ]] for this: [[-5|2|-4|4]]

For this issue here, we could define (just an idea): (x<0)*0+((21/20*x+1)^(1/2)) ~x:0|10~ and parse everything in between ~.

Or using this syntax: [~ and ~] so we get: [~0|10~], and this would precisely mean: draw from x = 0 to x = 10.

Of course, you need to think about a solid syntax that does not interfere with the maths formulas (!)

q2apro avatar Apr 09 '15 14:04 q2apro