![]() | |
![]() |
![]() ![]() ![]() ![]() |
![]() |
Gnuplot3.7 has a provision of a least-squares fitting. It searches automatically for parameters of an arbitrary function by fitting it to data in a file. The function can be linear or non-linear, but it should be expressed by gnuplot's expression manner. Here is an example for data fitting -- fit a Lorentzian function f(x)=a/(1+b*x*x) to data. The variables a and b are the parameters. The following sample data were prepared by calculating this function with a=2 and b=1, to show that gnuplot gives the correct answer.
Those numerical data are stored into the data-file "exp.dat". Firstly the fitting function is defined. The parameters to be searched for are arbitrary except for "x" or "t" those gnuplot uses. A data fitting begins by the command fit . The parameter names are given by the via option. At the data fitting, one can use using, index, every options to specify or to skip data or data block in the file. An uncertainty of the data point is treated as a weight of it. When the data point has an error of sigma, the weigh becomes sigma^{-2}. To do the weighted least-squares fitting, the errors are prepared at the third column in your data-file, then you have to tell that those are the uncertainties, like fit f(x) "exp.dat" using 1:2:3 via a,b . When the data error is omitted, all data points have the equivalent weight.
During the data fitting, you can see changes in the parameters on your display. The same stuff you can find in a file "fit.log" which is generated in the current directory. If succeeded, you get the final values of the parameter as well as those covariance matrix. In the above case, the correct parameters a=2 and b=1 were obtained. The final results are stored in the variables, a and b, so that you can compare the fitted function and the data by plotting this function with the fitted parameters.
![]() |
The next example is more realistic one. We have experimental data like below. A function f(x)=c*(x-a)**b is fitted to the data by adjusting the variables a, b, and c.
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.
![]() Next, choose initial values of those variables, a, b, and c. When the initial values are not given at the parameter search, gnuplot assumes that those values are 1. Since this choice is insufficient in some case, you should give better initial values. Our fitting function is f(x)=c*(x-a)**b, then one can guess that a is near 6 because f(x)=0 at x=a, c is in the order of 10 (see log(f(x))), and b is 1 or 2 as f(x) becomes 10 to 100 times when x is increased by 1. Those values are used for the initial.
The calculation converges after 17 times iteration, the final values with those covariance are displayed. The values obtained were a=5.77, b=1.89, c=25.8.
Those results were compared with the experimental data.
![]() |
![]() | |
![]() |
![]() ![]() ![]() ![]() |
![]() |