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 43
234 342
etc...
I can easily plot this in gnuplot with:
plot "results"
Is there any way to plot the derivative of these measurements with regard to time (i.e. dsample/dt) directly from gnuplot, or do I have to calculate the derivative separately and plot that directly in gnuplot?
You can do it by defining a function to take the derivative:
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
The result is
An alternative (more generic) syntax to plot the derivative is given here by Viktor T. Toth
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:
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.