如何在gnuplot中绘制图的导数? [英] How can I plot the derivative of a graph in gnuplot?
问题描述
我有一组随时间变化的变量测量值.我将这些测量结果保存在一个名为结果"的文件中,格式为:
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
等...
我可以使用以下方法在gnuplot中轻松绘制此图:
I can easily plot this in gnuplot with:
plot "results"
是否有任何方法可以直接从gnuplot中绘制关于时间的测量导数(即dsample/dt),还是必须单独计算导数并在gnuplot中直接绘制那个 ?
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:
#!/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)(可能是您要寻找的)仅计算除第一个数据点以外的所有运行上升量(delta y超过delta x),而d(y)则在同一数据点计算delta y道路.有了这个数据文件
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
结果是
这篇关于如何在gnuplot中绘制图的导数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!