使用 gnuplot 查找拟合范围

Finding fitting range using gnuplot

我是 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

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

数据:(如何放大绘图)

### 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