Search code examples
gnuplot

Summation - Gnuplot expression


I am trying to use the summation expression in Gnuplot but it is not working properly. I have the following data structure with many number of rows:

t x1 y1 z1 x2 y2 z2 x3 y3 z3 ... x98 y98 z98

I would like to plot the following equation:

u = (sqrt(sum(x)**2 + sum(y)**2 + sum(z)**2))/98

98 is the number of points (x,y,z).

What I have until now is how to plot the average of columns x1, x2, x3.. as following:

plot 'data file' u 1:((sum[i=0:ColCount-1] column(i*ColStep+ColStart))/ColCount) w lines ls 4 notitle

Where ColCount = 98, ColStep = 3 and ColStart=2.

But I have been trying to plot the equation, but it is not working. I would really appreciate any help.


Solution

  • What the following script does: It takes the square root of the sum of (x1+x2+x3)**2 and (y1+y2+y3)**2 and (z1+z2+z3)**2. This you can adapt to your column numbers. But I'm still not sure whether this is what you want. Please clarify.

    Code:

    ### summing up columns
    reset session
    
    $Data <<EOD
    #t   x1   y1   z1   x2   y2   z2   x3   y3   z3
     1 1.11 1.21 1.31 2.11 2.21 2.31 3.11 3.21 3.31
     2 1.12 1.22 1.32 2.12 2.22 2.32 3.12 3.22 3.32
     3 1.13 1.23 1.33 2.13 2.23 2.33 3.13 3.23 3.33
     4 1.14 1.24 1.34 2.14 2.24 2.34 3.14 3.24 3.34
     5 1.15 1.25 1.35 2.15 2.25 2.35 3.15 3.25 3.35
     6 1.16 1.26 1.36 2.16 2.26 2.36 3.16 3.26 3.36
     7 1.17 1.27 1.37 2.17 2.27 2.37 3.17 3.27 3.37
     8 1.18 1.28 1.38 2.18 2.28 2.38 3.18 3.28 3.38
     9 1.19 1.29 1.39 2.19 2.29 2.39 3.19 3.29 3.39
    EOD
    
    ColStep = 3
    ColCount = 3
    
    mySum(ColStart) = sum[i=0:ColCount-1] column(i*ColStep+ColStart)
    
    plot $Data u 1:(sqrt(mySum(2)**2 + mySum(3)**2 + mySum(4)**2)) w lp pt 7 notitle
    ### end of code
    

    Result:

    enter image description here