How can I plot the derivative of a graph in gnuplot?

后端 未结 2 1016
既然无缘
既然无缘 2020-12-29 08:48

I have a set of measurements of a variable over time. I have these measurements in a file called \"results\" with this format:

# time sample
0      5
12             


        
相关标签:
2条回答
  • 2020-12-29 09:24

    You can do it by defining a function to take the derivative:

    #!/usr/bin/env gnuplot
    
    set term pngcairo
    set output 'test.png'
    
    # derivative functions.  Return 1/0 for first point, otherwise delta y or (delta y)/(delta x)
    d(y) = ($0 == 0) ? (y1 = y, 1/0) : (y2 = y1, y1 = y, y1-y2)
    d2(x,y) = ($0 == 0) ? (x1 = x, y1 = y, 1/0) : (x2 = x1, x1 = x, y2 = y1, y1 = y, (y1-y2)/(x1-x2))
    
    set key bottom left Left reverse
    
    # offset for derivatives (half the x spacing)
    dx = 0.25
    
    plot 'data.dat' title 'data', \
         '' u ($1-dx):(d($2)) title '1-variable derivative', \
         '' u ($1-dx):(d2($1,$2)) title '2-variable derivative', \
         '' u ($1-dx):(d2($1,$2)) smooth csplines title '2-variable derivative (smoothed)'
    

    d2(x,y) (which is probably what you are looking for) just computes rise over run (delta y over delta x) at all but the first data point, and d(y) computes delta y in the same way. Given this data file

    0.0 1
    0.5 2
    1.0 3
    1.5 4
    2.0 5
    2.5 3
    3.0 1
    

    The result is

    enter image description here

    0 讨论(0)
  • 2020-12-29 09:33

    An alternative (more generic) syntax to plot the derivative is given here by Viktor T. Toth

    x0=NaN
    y0=NaN
    plot 'test.dat' using (dx=$1-x0,x0=$1,$1-dx/2):(dy=$2-y0,y0=$2,dy/dx) w l t 'dy/dx'
    

    Explanation: The datafile modifier (after using) within the brackets is to be interpreted as the computed coordinates of the point (x):(y), computed row by row from the datafile. For each row, the column values ($1, $2, ...) are modified by allowed arithmetic operations. The value of the bracket is the last expression in a list of comma-separated expressions. The first two are evaluated first and stored in variables, that are used later and for the next row. A pseudo code for the above syntax is:

      x0 = NaN // Initialise to 'Not a number' for plot to ignore the first row
      y0 = NaN
      foreach row in 'test.dat' with col1 as $1, and col2 as $2:
        dx = $1-x0
        x0 = $1
        x = $1 - dx/2 // Derivative at the midpoint of the interval
        dy = $2-y0
        y0 = $2
        y = dy/dx
        plot x:y  // Put the point on the graph
    

    Extra: This explanation can also be used to interpret @andryas solution to the derivative function d2(x,y). The only difference being the usage of $0. $0 in gnuplot is the 'zeroth' column of the datafile, essentially the row number (as in a spreadsheet, after ignoring the comment lines in the datafile). $0==0? checks if it is the first row and assigns a 1/0 (NaN) so the plot command ignores and does not plot it. The code, however, is correct only if the interval length is fixed (in the above case 0.5). On the other hand, Viktor's code computes the interval for every row.

    0 讨论(0)
提交回复
热议问题