使用gnuplot查找拟合范围

使用gnuplot查找拟合范围,gnuplot,data-fitting,Gnuplot,Data Fitting,我是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)

我是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'
fit[0.2:0.6]f(x)“data.txt”u2:3:6通过m1、m2计算的年误差
绘图“您的数据(我将文件命名为
'mas\u data.txt'
)如下所示(请始终在您的问题中显示/提供相关数据)

数据:(如何放大打印)

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

  • 使用
    stats
    (请参阅
    help stats
    )查找数据的绝对y最小值
  • 将x范围从该最小值限制到最大x值
  • 使用
    f(x)=a*x+b进行线性拟合,并记住斜率的标准误差值(此处:
    a_err
  • 将x范围减少2倍
  • 返回到3。直到达到迭代次数(此处:
    N=10
  • 找到
    Aerr[i]
    的最小值,并获得相应的x范围
  • 假设相对误差(
    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
    
    结果:

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


    欢迎来到StackOverflow!是的,可能是这样,也可能取决于数据。因此,请提供一些示例数据。Hi@theozh,对不起,我没有输入数据。我编辑了我的问题,并添加了我使用的典型数据。关于数据,你还知道什么?例如,这个线性范围通常总是在开头吗?是否也可以最后?与完整数据范围相比,线性范围通常有多大…?任何提供更多关于大致查找拟合范围的位置和方式的信息的方法都可能使该过程更容易、更可靠。感谢@theozh的评论,在此数据中,数据末尾的线性并不重要。我想使用li仅在数据开始处接近范围,因为它是高效值范围。x轴(第2列)表示低效值,因此我希望找到可包含在线性拟合中的最低效率值。以手动方式,我从第15个数据开始拟合范围,并将范围扩展到某个点(第85个数据)然后找到最适合我的拟合范围。我想知道如何使Gnuplot迭代地适合每N个数据范围..太棒了!感谢@theozh提供的过程、代码和清晰的解释!通过一些优化,我可以解决这个问题。再次感谢你,我会记得提供相关数据和足够的信息n下一个问题。
    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