I have a data file containing the following data:
x z
0.1 0
0.12 0.1
0.12 0.14
0.13 0.15
0.15 0.15
... ...
I would like to calculate the curve length
sum_{n=0->end of file}(sqrt((x_n+1-x_n)^2+(z_n+1-z_n)^2))
I guess I have to use a recursive function.But don't know how to use it with a data file. How could I do that in gnuplot script?
You certainly could define a recursive function, but it is not necessary.
You simply sum up all the path lengths between two consecutive points.
For the first line, i.e. if the pseudocolumn 0 (basically, line index zero-based, check help pseudocolumns
) is equal to 0
, initialize your variables x1
and y1
and L
. For each line assign x0=x1
and x1=$1
. With this, x0
holds the previous x-value and x1
the current x-value. Same for y0=y1
and y1=$2
. Calculate the length via sqrt((x1-x0)**2 + (y1-y0)**2)
and sum all pieces up in L
.
The function sumUpSegments(colX,colY)
takes the x- and the y-column (here: 1 and 2) as argument.
Furthermore, the construct '+' u (x1):(y1) ... every ::0::0
is one way of many ways to plot a single data point.
Script:
### determine path length
reset session
$Data <<EOD
0.1 0
0.12 0.1
0.12 0.14
0.13 0.15
0.15 0.15
0.17 0.10
0.14 0.04
0.13 0.08
0.13 0.11
0.15 0.12
0.16 0.10
0.14 0.07
EOD
set key noautotitle
sumUpSegments(colX,colY) = ($0==0 ? (L=0, x1=column(colX), y1=column(colY)):0, \
x0=x1, y0=y1, x1=column(colX), y1=column(colY), \
L=L+sqrt((x1-x0)**2 + (y1-y0)**2), x1)
plot $Data u (sumUpSegments(1,2)):2 w l lc "red", \
'+' u (x1):(y1):(sprintf("L=%.3g",L)) every ::0::0 w labels offset 0,-0.5
### end of script
Result: