Nat*_*man 13 gnuplot numerical-methods
我有一组随时间变化的变量.我将这些测量结果放在一个名为"results"的文件中,格式如下:
# time sample
0 5
12 43
234 342
Run Code Online (Sandbox Code Playgroud)
等等...
我可以在gnuplot中轻松地绘制这个:
plot "results"
Run Code Online (Sandbox Code Playgroud)
有什么方法来绘制这些测量的导数与直接从gnuplot的关于时间(即dsample/DT),或者我要单独计算的导数和情节是直接在gnuplot的?
and*_*ras 15
您可以通过定义一个函数来实现它:
#!/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)'
Run Code Online (Sandbox Code Playgroud)
d2(x,y)(这可能是你正在寻找的)只计算除了第一个数据点之外的所有运行(delta y over delta x),而d(y)以相同的方式计算delta y.鉴于此数据文件
0.0 1
0.5 2
1.0 3
1.5 4
2.0 5
2.5 3
3.0 1
Run Code Online (Sandbox Code Playgroud)
结果是

小智 6
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'
Run Code Online (Sandbox Code Playgroud)
说明:括号内的数据文件修饰符(使用后)将被解释为点 (x):(y) 的计算坐标,从数据文件中逐行计算。对于每一行,列值 ($1, $2, ...) 由允许的算术运算修改。括号的值是逗号分隔表达式列表中的最后一个表达式。前两个首先评估并存储在变量中,稍后使用并用于下一行。上述语法的伪代码是:
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
Run Code Online (Sandbox Code Playgroud)
额外:这个解释也可以用来解释@andryas 对导函数 d2(x,y) 的解。唯一的区别是使用了 $0。gnuplot 中的 $0 是数据文件的“第零”列,本质上是行号(就像在电子表格中一样,在忽略数据文件中的注释行之后)。$0==0?检查它是否是第一行并分配一个 1/0 (NaN) 以便 plot 命令忽略并且不绘制它。但是,仅当间隔长度固定(在上述情况下为 0.5)时,代码才是正确的。另一方面,Viktor 的代码计算每一行的间隔。