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

后端 未结 2 1015
既然无缘
既然无缘 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: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.

提交回复
热议问题