【问题标题】:Finding fitting range using gnuplot使用 gnuplot 查找拟合范围
【发布时间】:2021-02-02 05:29:52
【问题描述】:

我是 Gnuplot 新手,我有一个非线性数据集,我只想将数据拟合在线性范围内。我通常使用以下命令进行拟合并指定拟合范围,然后通过手动更改拟合范围来重做拟合过程,直到获得最佳拟合范围:

fit [0.2:0.6]f(x) "data.txt" u 2:3:6 yerror via m1,m2 

plot "<(sed -n '15,500p' data.txt)" u 2:3:6 w yerr title 'Window A',[0:.6] f(x) notitle lc rgb 'black'

是否可以在某个数据范围内迭代运行拟合以获得 Gnuplot 中拟合的最佳数据范围?

数据通常是这样的: data

【问题讨论】:

  • 欢迎来到 StackOverflow!是的,这可能是可能的,并且可能取决于数据。所以,请提供一些示例数据。 stackoverflow.com/help/minimal-reproducible-example
  • 嗨@theozh,抱歉我没有放数据。我编辑了我的问题并添加了我使用的典型数据。
  • 您对您的数据还有什么了解?例如,这个线性范围通常总是在开始吗?也能在最后吗?与整个数据范围相比,线性范围通常有多大...?任何提供有关在何处以及如何粗略找到拟合范围的更多信息的任何内容都可能使该过程更容易和更可靠。
  • 感谢@theozh 的评论,在这个数据中,数据末尾的线性并不重要。我只想在数据开头使用线性范围,因为它是高效值范围。 x 轴(第 2 列)表示低效率值,因此我想找到可以包含在线性拟合中的最低效率值。以手动方式,我从第 15 个数据开始拟合范围并将范围扩展到某个点(第 85 个数据)并找到最适合我的拟合范围。我想知道如何使 Gnuplot 迭代地适合每个第 n 个数据范围..

标签: gnuplot data-fitting


【解决方案1】:

您的数据(我将文件命名为 'mas_data.txt')如下所示(请始终在您的问题中显示/提供相关数据)。

数据:(如何放大绘图)

### plotting data with zoom-in
reset session
FILE = 'mas_data.txt'

colX = 2
colY = 3
set key top left

set multiplot
    plot FILE u colX:colY w lp pt 7 ps 0.3 lc rgb "red" ti "Data", \

    set title "Zoom in"
    set origin 0.45,0.1
    set size 0.5, 0.6
    set xrange [0:1.0]
    plot FILE u colX:colY w lp pt 7 ps 0.3 lc rgb "red" ti "Data"
    
unset multiplot
### end of code

关于“最佳”拟合范围,您可以尝试以下步骤:

  1. 使用stats 查找数据的绝对y 最小值(请参阅help stats
  2. 将 x 范围从这个最小值限制到最大 x 值
  3. f(x)=a*x+b 进行线性拟合并记住斜率的标准误差值(此处:a_err
  4. 将 x 范围缩小 2 倍
  5. 回到 3. 直到达到迭代次数(这里:N=10
  6. Aerr[i]的最小值,得到对应的x-range

假设如果相对误差 (Aerr[i]) 具有最小值,那么您将拥有从数据最小值开始的线性拟合的“最佳”拟合范围。 但是,我不确定此过程是否适用于您的所有数据集。也许有更聪明的程序。当然,您也可以在不同的步骤中减小 xrange。此过程可以作为进一步调整和优化的起点。

代码:

### finding "best" fitting range
reset session

FILE = 'mas_data.txt'
colX = 2
colY = 3

stats FILE u colX:colY nooutput   # do some statistics
MinY = STATS_min_y          # minimum y-value
MinX = STATS_pos_min_y      # x position of minimum y-value
Xmax = STATS_max_x          # maximum x-value
XRangeMax = Xmax-MinX

f(x,a,b) = a*x + b 
set fit quiet nolog

N = 10
array A[N]
array B[N]
array Aerr[N]
array R[N]

set print $myRange
    do for [i=1:N] {
        XRange = XRangeMax/2**(i-1)
        R[i] = MinX+XRange
        fit [MinX:R[i]] f(x,a,b) FILE u colX:colY via a,b
        A[i] = a
        Aerr[i] = a_err/a*100   # asymptotic standard error in %
        B[i] = b
        print sprintf("% 9.3g % 9.3f   %g",MinX,R[i],Aerr[i])
    }
set print 
print $myRange

set key bottom right
set xrange [0:1.5]

plot FILE    u colX:colY w lp pt 7 ps 0.3 lc rgb "red" ti "Data", \
     for [i=1:N] [MinX:R[i]] f(x,A[i],B[i]) w l lc i title sprintf("%.2f%%",Aerr[i])

stats [*:*] $myRange u 2:3 nooutput
print sprintf('"Best" fitting range %.3f to %.3f', MinX, STATS_pos_min_y)
### end of code

结果:

放大xrange[0:1.0]

0.198    19.773   1.03497
0.198     9.985   1.09066
0.198     5.092   1.42902
0.198     2.645   1.53509
0.198     1.421   1.81259
0.198     0.810   0.659631
0.198     0.504   0.738046
0.198     0.351   0.895321
0.198     0.274   2.72058
0.198     0.236   8.50502


"Best" fitting range 0.198 to 0.810

【讨论】:

  • 太棒了!感谢@theozh 的过程、代码和清晰的解释!通过一些优化,我可以解决问题。再次感谢您,我会记得在下一个问题上提供相关数据和足够的信息。
猜你喜欢
  • 1970-01-01
  • 2021-11-27
  • 2015-09-16
  • 1970-01-01
  • 2020-07-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-09
相关资源
最近更新 更多