Saturday, 20 June 2009

Plotting an inequality in 2D

In one of the comments, someone asked how an inequality can be plotted in gnuplot. Well, there are two solutions (at least) for this problem. One of them is discussed in the following page, if you need only the curve separating the region of the x-y plane that fulfils the inequality from that that doesn't. But the question was how to shade these regions. We will look at the inequality
x3 - 2xy + y3>0

The way to do this is to define a function that has a constant value, if the inequality holds true, and has an indefinite value, if this is not the case. The value of 1/0 is quietly ignored in gnuplot, thus we can define our function as follows
f(x,y)=(x*x*x - 2*x*y + y*y*y>0)?1:1/0

where we used the ternary operator, which always has the form

i.e., returns 'B', if 'A' is true, and 'C' otherwise. (Incidentally, the value 'B' can be a ternary operator in itself, and in this way, multiple conditions can be imposed.) In the function above, f(x,y) = 1, if x3 - 2xy + y3>0, and f(x,y) = 1/0 (undefined), if x3 - 2xy + y3<0. Once we defined our function, we can plot it in the usual way. The complete script and the figure is given below.
f(x,y)=(x*x*x - 2*x*y + y*y*y>0)?1:1/0
unset colorbox
set isosample 300, 300
set xlabel 'x'
set ylabel 'y'
set sample 300
set pm3d map
splot [-2:2] [-2:2] f(x,y) t 'x^3 - 2xy + y^3>0'