【问题标题】:nested do and if statements miss plots嵌套的 do 和 if 语句错过了情节
【发布时间】:2015-12-01 15:02:33
【问题描述】:

我正在使用

G N U P L O T
版本 4.6 补丁级别 4 上次修改时间为 2013 年 10 月 2 日
构建系统:Linux x86_64

我想用它来绘制大致像这样设置的文件中的数据

0.0 a1 b1
0.0 a2 b2
...
0.1 a1 b1*
0.1 a2 b2*
...

对于第一列中的每个唯一值,我想在 a 上绘制 b。为此,我创建了一个包含条件绘图的 do 循环

为 [t=0:34] {
打印 0.2000*t
plot 'twopi5101/profile.dat' u ($1==0.2000*t ? ($3-7.5) : 1/0):8 notitle w l lt 1 lc 1, \
'twopi5101/profile.dat' u ($1==0.2000*t ? ($3-7.5) : 1/0):9 notitle w l lt 1 lc 2
}

不幸的是,这个循环(以及其他文件的类似循环)会一直错过一些情节

0.0
0.2
0.4
0.6
警告:跳过没有有效点的数据文件
警告:跳过没有有效点的数据文件
更多> ;打印 0.2000*t;plot 'twopi5101/profile.dat' u ($1==0.2000*t ? ($3-7.5) : 1/0):8 notitle w l lt 1 lc 1, 'twopi5101/profile.dat ' u ($1==0.2000*t ? ($3-7.5) : 1/0):9 notitle w l lt 1 lc 2;
^
x 范围无效

但是,如果我手动输入 0.6 完全没有问题

gnuplot> plot 'twopi5101/profile.dat' u ($1==0.6 ? ($3-7.5) : 1/0):8 notitle w l lt 1 lc 1, \ 'twopi5101/profile.dat' u ($1==0.6 ? ($3-7.5) : 1/0):9 notitle w l lt 1 lc 2
gnuplot>

对于为什么会发生这种情况似乎没有合乎逻辑的解释,或者甚至是错过分数的模式。
间隔 [0.0:6.0] gnuplot 跳过:
0.6,1.2,1.4,2.4,2.8,3.4,3.8,4.6,4.8,5.6,5.8
并且每次我运行循环时它都会始终如一地运行,即使我只在该间隔的一部分上运行它(例如,从 0.6 运行到 2.0 将再次熟练 0.6、1.2 和 1.4)。

对于更大的间隔/更多的情节,我在许多其他情况下遇到了相同的行为。我不知道是什么甚至会导致这样的事情,或者我的循环格式是否有一些错误来解释它。
(我使用的终端是 'wxt' 或 postscript 增强型)

【问题讨论】:

    标签: gnuplot


    【解决方案1】:

    这是因为在相等性上测试浮点值通常不是一个好主意。让我们考虑一下:

    gnuplot> print 0.6==0.6
    1
    gnuplot> print 0.6==3*0.2
    0
    

    这是因为 0.2 这样的数字没有准确表示。

    我建议首先将数据中的第一列转换为整数值,例如,

    floor(($1 + 0.05)*10)
    

    这里假设有问题的列只包含0.1 的倍数。存在0.05 因子以确保可能的不准确输入(例如0.10000010.0999999)被转换为1

    这个转换后的值可以用于plot 命令中的过滤,例如,

    plot 'twopi5101/profile.dat' u (floor(($1+0.05)*10)==2*t?($3-7.5):1/0):8
    

    或者,可以将条件$1==0.2000*t 替换为abs($1 - 0.2000*t)<5E-2 之类的东西

    【讨论】:

      猜你喜欢
      • 2018-08-12
      • 2015-02-06
      • 1970-01-01
      • 2018-10-26
      • 2021-01-10
      • 1970-01-01
      • 2016-08-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多