I came across an unexpected Gnuplot behaviour (Version 5.2 patchlevel 6) I could not explain, so far. This is the data (though any similar data would do):
x y
0 1.1
1 1.0
2 1.3
3 1.4
4 0.9
The line
p a=0 'data.txt' u 1:(a=a+$2,a) w lp
works perfectly: plots the progressive summation of column 2 and stores the partial sum in “a” (this is a simple, almost naive, way of integrating column 2).
pr a
returns 5.7, which is the expected value. But let us change the code a bit.
- Using an expression at left side of the colon.
The line
p a=0 'data.txt' u (a=a+$2,$1):2 w lp
simply plots columns 1 and 2 and it works, but
pr a
Returns NaN. What is wrong in this case, since the summation process is nearly the same?
- Enclosing the left side of the colon in parenthesis.
This one
p a=0 'data.txt' u ($1):(a=a+$2,a) w lp
returns “all points y value undefined!”. Why, since the syntax seems right?
- More complicated expressions
The origin of this problem was this line:
p a=0 'data.txt' u ($1>1 && $1<4? $1:1/0):(a=$2+a,a) w lp
which returns “all points y value undefined!” (I was trying to integrate column 2 in the xrange from 1 to 4). Nonetheless, the following works nicely (it is almost the same as before, but plots the column 2 instead "a")
p a=0 'data.txt' u ($1>1 && $1<4? $1:1/0):(a=$2+a,$2) w lp
However, “a” returns NaN again.
Finally, the following is the way to make it work perfectly, though it simply moves the ternary operator to the right side of the colon.
p a=0 'data.txt' u 1:($1>1 && $1<4? a=$2+a: 1/0,a) w lp
I am pretty sure all of this has a single cause, which I hope to be my poor understanding of the syntax, instead of a bug.
Any help would be appreciated.
Note: I have always used the abbreviated form of the commands. So I hope the code is clear enough.