non-continuous

You are currently browsing articles tagged non-continuous.

In Gnuplot it is easy to define a continuous and differentiable function such as f(x) = x, but what to do if we need a function that fulfill non of these conditions?
For example let us consider a step function. Typically a step function is given by

           / 1   if x > a
step(x) = -|
           \ 0   else

In Gnuplot this can be achieved by using the ternary operator:

step(x) = x>a ? 1 : 0

Which is a simple if-else statement and means step(x)=1 if x>a else step(x)=0.

If we plot this function we get Fig. 1.


step function

Fig. 1 Continuous plot of the step function (code to produce this figure)

As you can see this will result in a continuous plot. If we want a discontinuity in the plot, we have to create two separate functions that are only piecewise defined. This can be achieved by using 1/0 that will result in a undefined value.

f(x) = x<a  ? 1 : 1/0
g(x) = x>=a ? 1 : 1/0

Plotting both functions will result in Fig. 2.


step function

Fig. 2 Discontinuous plot of the step function (code to produce this figure)

The ternary operator can also be used in an iterative way. For example if we want to define a rectangular function that is given by

           / 0     if abs(x) > 0.5
rect(x) = -| 0.5   if abs(x) = 0.5
           \ 1     if abs(x) < 0.5

we can use the following statement in Gnuplot to define it:

rect(x) = abs(x)>0.5 ? 0 : abs(x)<0.5 ? 1 : 0.5

In Fig. 3 you can see a plot of this function. To produce the sharp edges of the rectangular function we use a higher number of sampling points (also in Fig. 1 for the step function). In order to plot a function Gnuplot calculates 100 points of the given function and draw a line through them. This can be set to another value with the set samples <value> command.


rectangular function

Fig. 3 Plot of the above defined rect(x) function (code to produce this figure)

Tags: , ,

If you have measurement data and like to plot them as points combined by lines, you will probably do that with the linespoints plotting style. But for some applications it is required to combine the data points by non-continuous lines to emphasize that the data came from measurements as shown in Fig. 1.


Plotting data

Fig. 1 Plot of the data from plotting_data1.dat with non-coninuous lines between its points (code to produce this figure)

In Gnuplot exists no line style that can do this directly. But with a little trick it is very easy to achieve. Since Gnuplot 4.4. there exists the property pointinterval (see the documentation) in combination with the plotting style linespoints. This property plots not every single point, but only every second for a value of 2 and so on. But if we use the value -1 it tells Gnuplot to insert a little gap between the points and the line. The only problem is that the size of this gap is not scalable. But by using two different line styles this is also achievable.

set style line 1 lc rgb '#0060ad' lt 1 lw 2 pt 0 pi -1 ps 3
set style line 2 lc rgb '#0060ad' lt 1 lw 2 pt 7 ps 1.5

In the first line style we specify a point interval pi of -1 and enlarge the gap by using a point size of 3. With the point size value we can easily define any gap size we need. The other trick is to use point type 0 that is a point of size 0 independent of the given pointsize. In the second line style we specify the real point we want to plot using with points. Finally our plot command looks now like the following.

plot 'plotting_data1.dat' with linespoints ls 1, \
     ''                   with points ls 2

Using the same data as in the first plot of the introduction chapter Plotting data we will get Fig. 1 as a result.

Tags: , ,