Gnuplot Fit data with gnuplot Fitting data with errors


There can be up to 12 independent variables, there is always 1 dependent variable, and any number of parameters can be fitted. Optionally, error estimates can be input for weighting the data points. (T. Williams, C. Kelley - gnuplot 5.0, An Interactive Plotting Program)

If you have a data set and want to fit if the command is very simple and natural:

fit f(x) "data_set.dat" using 1:2 via par1, par2, par3

where instead f(x) could be also f(x, y). In the case you also have data error estimates just add the {y | xy | z}errors ({ | } represent the possible choices) in the modifier option (see Syntax). For example

fit f(x) "data_set.dat" using 1:2:3 yerrors via par1, par2, par3

where the {y | xy | z}errors option require respectively 1 (y), 2 (xy), 1 (z) column that specify the value of the error estimate.

Exponential fitting with xyerrors of a file

Data error estimates are used to calculate the relative weight of each data point when determining the weighted sum of squared residuals, WSSR or chisquare. They can affect the parameter estimates, since they determine how much influence the deviation of each data point from the fitted function has on the final values. Some of the fit output information, including the parameter error estimates, is more meaningful if accurate data error estimates have been provided.. (Ibidem)

We'll take a sample data set measured.dat, made up by 4 columns: the x-axis coordinates (Temperature (K)), the y-axis coordinates (Pressure (kPa)), the x-error estimates (T_err (K)) and the y-error estimates (P_err (kPa)).

#### 'measured.dat' ####
### Dependence of boiling water from Temperature and Pressure
##Temperature (K) - Pressure (kPa) - T_err (K) - P_err (kPa)

368.5     73.332        0.66    1.5
364.2     62.668        0.66    1.0
359.2     52.004        0.66    0.8
354.5     44.006        0.66    0.7
348.7     34.675        0.66    1.2
343.7     28.010        0.66    1.6
338.7     22.678        0.66    1.2
334.2     17.346        0.66    1.5
329.0     14.680        0.66    1.6
324.0     10.681        0.66    1.2
319.1     8.015         0.66    0.8
314.6     6.682         0.66    1.0
308.7     5.349         0.66    1.5

Now, just compose the prototype of the function that from the theory should approximate our datas. In this case:

Z = 0.001
f(x) = W * exp(x * Z)

where we have initialised the parameter Z because otherwise evaluating the exponential function exp(x * Z) results in huge values, which leads to (floating point) Infinity and NaN in the Marquardt-Levenberg fitting algorithm, usually you would not need to initialise the variables - have a look here, if you want to know more about Marquardt-Levenberg.

It is time to fit the data!

fit f(x) "measured.dat" u 1:2:3:4 xyerrors via W, Z

The result will look like

After 360 iterations the fit converged.
final sum of squares of residuals : 10.4163
rel. change during last iteration : -5.83931e-07

degrees of freedom    (FIT_NDF)                        : 11
rms of residuals      (FIT_STDFIT) = sqrt(WSSR/ndf)    : 0.973105
variance of residuals (reduced chisquare) = WSSR/ndf   : 0.946933
p-value of the Chisq distribution (FIT_P)              : 0.493377

Final set of parameters            Asymptotic Standard Error
=======================            ==========================
W               = 1.13381e-05      +/- 4.249e-06    (37.47%)
Z               = 0.0426853        +/- 0.001047     (2.453%)

correlation matrix of the fit parameters:
                W      Z      
W               1.000 
Z              -0.999  1.000 

Where now W and Z are filled with the desired parameters and errors estimates on those one.

The code below produce the following graph.

set term pos col
set out ''

set grid
set key center
set xlabel 'T (K)'
set ylabel 'P (kPa)'

Z = 0.001
f(x) = W * exp(x * Z)
fit f(x) "measured.dat" u 1:2:3:4 xyerrors via W, Z

p [305:] 'measured.dat' u 1:2:3:4 ps 1.3 pt 2 t 'Data' w xyerrorbars,\
f(x) t 'Fit'

Plot with fit of measured.dat Using the command with xyerrorbars will display errors estimates on the x and on the y. set grid will place a dashed grid on the major tics. Exponential fit

In the case error estimates are not available or unimportant it is possible also to fit data without the {y | xy | z}errors fitting option:

fit f(x) "measured.dat" u 1:2 via W, Z

In this case the xyerrorbars had also to be avoided.