gnuplot / misc (2E)
not so FAQ |
Miscellaneous Stuff (No.2)Least-Squares Fitting with non-linear functions (I) Gnuplot3.7 has a provision of a least-squares fitting. It searches The following sample data were prepared by calculating this function -10.000000 0.019802 -8.947370 0.024674 -7.894740 0.031582 -6.842110 0.041828 -5.789470 0.057941 -4.736840 0.085333 -3.684210 0.137236 -2.631580 0.252359 -1.578950 0.572561 -0.526316 1.566160 0.526316 1.566160 1.578950 0.572561 2.631580 0.252359 3.684210 0.137236 4.736840 0.085333 5.789470 0.057941 6.842110 0.041828 7.894740 0.031582 8.947370 0.024674 10.000000 0.019802 Those numerical data are stored into the data-file At the data fitting, one can use using, index, every gnuplot> f(x)=a/(1+b*x*x) gnuplot> fit f(x) "exp.dat" via a,b Iteration 0 WSSR : 1.43879 delta(WSSR)/WSSR : 0 delta(WSSR) : 0 limit for stopping : 1e-05 lambda : 0.201184 ....... Final set of parameters Asymptotic Standard Error ======================= ========================== a = 2 +/- 5.236e-07 (2.618e-05%) b = 0.999998 +/- 7.397e-07 (7.397e-05%) correlation matrix of the fit parameters: a b a 1.000 b 0.837 1.000 During the data fitting, you can see changes in the parameters on your The final results are stored in the variables, a and gnuplot> plot f(x) with lines, "exp.dat" using 1:2 w points ![]() Least-Squares Fitting with non-linear functions (II) The next example is more realistic one. We have experimental data like below. # X Y Z 6.295 8.4 0.3 6.795 23.9 0.7 7.826 104.0 3.0 8.830 255.0 8.0 9.841 421.0 13.0 10.860 566.0 18.0 11.864 690.0 22.0 Each data point has an error (Z), then the weight of the point is 1/Z*Z. At first, plot the experimental data only to see their tendency. gnuplot> set log y gnuplot> plot "exp.dat" using 1:2:3 w yerrorbars ![]() Next, choose initial values of those variables, a, gnuplot> a=6 gnuplot> b=1 gnuplot> c=10 gnuplot> f(x)=c*(x-a)**b gnuplot> fit f(x) "exp.dat" using 1:2:3 via a,b,c The calculation converges after 17 times iteration, the final After 17 iterations the fit converged. final sum of squares of residuals : 90.8196 rel. change during last iteration : -9.71916e-09 degrees of freedom (ndf) : 4 rms of residuals (stdfit) = sqrt(WSSR/ndf) : 4.76497 variance of residuals (reduced chisquare) = WSSR/ndf : 22.7049 Final set of parameters Asymptotic Standard Error ======================= ========================== a = 5.76731 +/- 0.176 (3.051%) b = 1.89369 +/- 0.2127 (11.23%) c = 25.7956 +/- 9.807 (38.02%) correlation matrix of the fit parameters: a b c a 1.000 b -0.944 1.000 c 0.975 -0.975 1.000 Those results were compared with the experimental data. gnuplot> plot f(x),"exp.dat" using 1:2:3 w yerrorbars ![]() |