談?wù)刧nuplot(二十四):擬合||| gnuplot 除了繪圖功能之外,最簡(jiǎn)單實(shí)用的功能就是擬合了。gnuplot 可以進(jìn)行單變量甚至多變量的線性和非線性擬合。雖然可能不像專(zhuān)門(mén)的數(shù)學(xué)軟件那么強(qiáng)大,但是足以對(duì)付日常需要了。我們拿上一篇文章里的數(shù)據(jù)來(lái)舉例子。
首先,要定義一個(gè)待擬合的函數(shù): gnuplot> f(x)=50*(1+erf(a*(x-b))) 這里使用了誤差函數(shù) erf(x),有兩個(gè)待定的參數(shù):a, b。下面我們生成一個(gè)文件“fit.par”,里面包含的是參數(shù) a 和 b 的初值: a = 1 b = 12 初值的選擇要盡可能貼近結(jié)果,否則可能導(dǎo)致誤差甚至無(wú)法收斂。下面我們進(jìn)行擬合: gnuplot> fit [8:16] f(x) 'probability.dat' using 1:2 via 'fit.par' gnuplot 里面關(guān)于擬合的命令是 fit,后面的自變量取值范圍不是必需的。f(x) 函數(shù)已經(jīng)在上面定義過(guò)了,數(shù)據(jù)文件“probability.dat”也已經(jīng)在上一篇博文中交代過(guò)了。via 后面跟的是參數(shù)變量列表文件。執(zhí)行 fit 命令之后,gnuplot 會(huì)輸出一堆結(jié)果。我們忽略那些中間運(yùn)算,只把最后結(jié)果貼在下面: After 5 iterations the fit converged. final sum of squares of residuals : 41.9399 rel. change during last iteration : -4.27973e-07 degrees of freedom (FIT_NDF) : 8 rms of residuals (FIT_STDFIT) = sqrt(WSSR/ndf) : 2.28965 variance of residuals (reduced chisquare) = WSSR/ndf : 5.24249 Final set of parameters Asymptotic Standard Error ======================= ========================== a = 1.15661 +/- 0.06331 (5.474%) b = 11.9027 +/- 0.02383 (0.2002%) correlation matrix of the fit parameters: a b a 1.000 b 0.014 2.000 這段文字說(shuō)明,經(jīng)過(guò) 5 次迭代,gnuplot 得到了收斂的結(jié)果。中間部分是參數(shù) a 和 b 的最終取值以及漸近標(biāo)準(zhǔn)差(asymptotic standard error)。漸近標(biāo)準(zhǔn)差的計(jì)算是基于線性擬合的,對(duì)于非線性擬合,漸近標(biāo)準(zhǔn)差一般都比真的標(biāo)準(zhǔn)差小,所以這個(gè)數(shù)字只能用于定性分析。而最后給出的相關(guān)矩陣(correlation matrix)可以幫助我們確認(rèn)漸近標(biāo)準(zhǔn)差的可靠度,非對(duì)角元素絕對(duì)值越小,漸近標(biāo)準(zhǔn)差越接近真實(shí)標(biāo)準(zhǔn)差。 好了,現(xiàn)在我們可以把數(shù)據(jù)和擬合曲線畫(huà)在同一張圖上了: gnuplot> set xrange [8:16] gnuplot> set yrange [-5:105] gnuplot> unset key gnuplot> set xlabel "Laser Pulse Energy (μJ)" gnuplot> set ylabel "Bubble Formation Probability (%)" gnuplot> plot "probability.dat" using 1:2:3:4 with xerrorbars, f(x) lw 2 lc rgb "orange" |
|
來(lái)自: 大老淵 > 《學(xué)習(xí)》