问题描述
我一直在尝试使用 gnuplot 计算导数,使用另一个 discussion 中的脚本,即使使用相同的数据文件。但是我不断收到此错误:
gnuplot> d(y) = ($0 == 0) ? (y1 = y,1/0) : (y2 = y1,y1 = y,y1-y2)
^
"prova.g",line 7: ')' expected
我不知道在这里做什么。有什么帮助吗?
解决方法
这是我收藏中的数值导数示例。需要 gnuplot >=5.0
代码:
### numerical derivatives
reset session
# create some data
MyFunction = "sin(x)/x"
set table $Data
set samples 150
plot [-10:10] '+' u 1:(@MyFunction) w table
unset table
DerivX(colX) = (dx=column(colX)-x0,x0=column(colX),column(colX)-dx/2)
DerivY(colY) = (dy=column(colY)-y0,y0=column(colY),dy/dx)
set table $Deriv1
plot x0=y0=NaN $Data u (DerivX(1)):(DerivY(2)) w table
unset table
set table $Deriv2
plot x0=y0=NaN $Deriv1 u (DerivX(1)):(DerivY(2)) w table
unset table
set table $Deriv3
plot x0=y0=NaN $Deriv2 u (DerivX(1)):(DerivY(2)) w table
unset table
plot $Data u 1:2 w l lc rgb "red" ti MyFunction,\
$Deriv1 u 1:2 w l lc rgb "web-green" ti "1st Derivative",\
$Deriv2 u 1:2 w l lc rgb "blue" ti "2nd Derivative",\
$Deriv3 u 1:2 w l lc rgb "magenta" ti "3rd Derivative"
### end of code
结果:
附加:(gnuplot 4.2.6 版本)
gnuplot 4.2.6 没有数据块和串行评估,但这里有一个没有这些功能的繁琐解决方法。
-
为了说明,我创建了一些数据文件
Data.dat
(您已经有了输入文件) -
将数据文件绘制到另一个文件中
temp1.dat
跳过第一行数据 -
使用系统命令
temp2.dat
将文件逐行合并到另一个文件paste
中(无论是在系统上已经安装的 Linux 上,还是在您必须安装的 Windows 上,例如 {{1} } 来自GnuWin)。 -
现在您可以分别计算第 1 列和第 4 列以及第 2 列和第 5 列的两个连续数据点之间的
CoreUtils
和dx
。 -
小缺点:由于文件长度不同,最后一行应该被跳过。这可能可以以某种方式消除,但我目前不知道如何。
这就是 dy
的样子:
temp2.dat
代码:
#Curve 0 of 1,150 points #Curve 0 of 1,150 points
#x y type #x y type
-10 -0.0544021 i -9.86577 -0.0432646 i
-9.86577 -0.0432646 i -9.73154 -0.0310307 i
-9.73154 -0.0310307 i -9.59732 -0.0178886 i
...
结果:(截图 gnuplot 4.2.6)