next up previous contents
Next: 8.2 Fitting 2d data Up: 8. Least square fitting Previous: 8. Least square fitting

   
8.1 Fitting 1d data sets

Starting point for a LS refinement is a loaded data set ( xi, yi), the selection of a suitable theory function y(xi,p) to describe the data and a set of starting values for the fit parameters p. The program KUPLOT offers the following three predefined theory functions and additionally allows the use of a user defined function (see section 8.3).


   
y(xi,p) = $\displaystyle \sum_{n=0}^{N} p_{n} x_{i}^{n}$ (8.1)
y(xi,p) = $\displaystyle p_{1} + p_{2}x_{i} +
\sum_{n=1}^{N} p_{1,n}\cdot\exp\left\{
\frac {-(x_{i}-p_{2,n})^{2}}{\sigma_{n}^{2}}\right\}$ (8.2)
y(xi,p) = $\displaystyle p_{1} + p_{2}x_{i} +
\sum_{n=1}^{N} p_{1,n}\cdot\left\{\frac{\sigma_{n}^{2}}
{(\sigma_{n}^{2}+4(x_{i}-p_{1,n}))^{2}}\right\}$ (8.3)
  with $\displaystyle \sigma_{n} = \left\{ \begin{array}{ll}
p_{3,n} \cdot p_{4,n} & \m...
...} < p_{2,n}$ }\\
\frac{p_{3,n}}{p_{4,n}} & \mbox {else} \\  \end{array}\right.$  

Let us have a closer look at these theory functions. The first one shown in equation 8.1 is a simple polynom of the order of N which can be defined by the user. The fit parameters pn are the corresponding coefficients. Note that KUPLOT actually numbers the fit parameters starting with one, i.e. parameter one corresponds to the term x0.

The next function (equation 8.2) is a sum of N Gaussians and a linear background defined by the first two parameters p1 and p2. The number of Gaussians to be used is defined by the user. Each Gaussian is represented by a set of four fit parameters: p1,n is its peak height and p2,n is its peak position. The half width of the Gaussian (as well as the Lorentzian) is defined by a half width parameter p3,n and an asymmetry parameter p4,n. Obviously the symmetric case is given by p4,n = 1.0. The Lorentzian theory function (equation 8.3) is defined by a similar set of parameters. Currently both function types can not be used in combination except as user defined theory function as described in section 8.3.


  
Figure 8.1: Fit results for 1D example
\includegraphics[scale=0.5, angle=270.0]{fit.1.eps}

Now we will see the fitting segment of KUPLOT in action in the following example. The data set is the profile we have used in examples before in Figure 4.1. Assuming the corresponding data are loaded as set one, the next step is to enter the LS sublevel of the program KUPLOT. This is done by the command 'fit' followed by the data set number that should be used as observed data (line 1). The macro is show below and as before the line number are not part of the macro file itself.

     1  fit 1
     2  #
     3  mfen 7
     4  func gaus,1
     5  par
     6  #
     7  run
     8  save

The command in line 3 sets the 'window' size used to determine maxima within the plot which are used as defaults for the starting values. The procedure is equivalent to the command 'smax' explained in section 7.4. For our example the setting to 7 points will determine the main maximum correctly. Next (line 4) the theory function is specified to be one Gaussian. Note that KUPLOT determines the default starting values for the fitting parameters pat this stage and the command 'mfen' has to be entered before to have an effect. Furthermore all current values of the fit parameters, e.g. from a previous run, are replaced by the new default values. The command 'par' (line 5) lists the current parameter settings and after checking them on the screen we are ready to go. The fit is started via the command 'run' in line 7 of our example command file. The fit progress is displayed on the screen until the fits converges to a minimum or the maximum number of cycles is reached. This maximum cycle number can be altered using the 'cyc' command in the fitting sublevel. In our example it took 9 iterations to reach the minimum and the resulting parameters are listed in table 8.1 on the left. Finally the results are saved (line 8) as two data files containing the calculated and difference data set and a text file summarizing the results.


 
Table 8.1: Results of example 1D fit
Parameter One Gaussian Two Gaussians
    Gaussian 1 Gaussian 2
Background p1 506.(16) 501.(10)
Background p2 -107.(18) -106.(11)
Peak height 421.(9) 315.(10) 134.(10)
Peak position -0.9270(4) -0.9277(3) -0.9250*
Half width 0.0374(9) 0.0278(9) 0.0640*
R-value 2.1% 1.2%

The resulting plot of the observed and calculated data as well as the difference between them is shown in Figure 8.1 again on the left. Although there a reasonable agreement is achieved, there are clearly visible differences especially at the tails of the Gaussian. Note that we have fitted a symmetric Gaussian which is the default, i.e. the parameter p4,n is set to 1.0 and not refined. To refine this parameter use the command 'par 6,1'. The first number is the number of the fit parameter (since we have two background parameters it is 4+2=6) and the next number can be 1 for refining or 0 for keeping the corresponding parameter fixed. An optional third parameter allows one to alter the value of the fit parameter.

To improve the fit we will try to fit two Gaussians located at the same position to the data set. This will allow to model the sides of the peak in a better way. The corresponding macro is shown below. Lines 1-4 are the same as before except that we select two Gaussians rather than one. Again we assume the profile is loaded as data set one.

     1  fit 1
     2  #
     3  mfen 7
     4  func gaus,2
     5  #
     6  par 7,1,0.0
     7  par 8,1,p[4]
     8  par 9,1,p[5]
     9  par10,0,p[6]
    10  par
    11  #
    12  run
    13  save

Since KUPLOT will only find one maximum a warning will be displayed and we have to set the starting values for our second Gaussian manually (lines 6-9). We set the peak height to 0.0 and take the other parameters from the settings for the first Gaussian using the variables p[i]. Just to be sure we list the current settings again (line 10) and the fit is started in line 12 by the 'run' command. Finally the results are saved as before (line 13). The resulting parameters are listed in Table 8.1. A measure for the quality of a fit is the R-vale which is defined as:


 \begin{displaymath}R=\sqrt{\frac{\sum\limits_{i=1}^{N} w_{i} (y_{i} - y(x_{i},p))^{2}}
{\sum\limits_{i=1}^{N} w_{i} y_{i}^{2}}}
\end{displaymath} (8.4)

Here the sum goes over all N data points ( xi,yi) and y(xi,p)is the theory function. KUPLOT offers a variety of weights wito be selected to the fit. The default we have used in our example is $w_{i} = \frac{1}{y_{i}}$. Check the online help for the command 'wic' to obtain more information about supported weighting schemes. Inspection of the results for this second fit shown in Table 8.1 and Figure 8.1 clearly show that the second fit describes the data much better. However the parameters marked with * in Table 8.1 could not be refined and remained at their starting values. Possible steps to avoid that problem are to refine the parameters one by one or change the setting of 'URF' which controlled the 'speed' of the fit. A small value, e.g. 0.1, will converge quickly to the minimum but in case of a more complex problem, the fit might fail. A larger value of 'URF' on the other hand will change the parameters in each iteration by a smaller amount and the fit might work. However care has to be taken to not end up in a local minimum. The value of 'URF' (which stands for something very German) is altered via the command 'urf'. The authors recommend playing around with the various settings of the fit sublevel to get familiar with the LS fitting process.


next up previous contents
Next: 8.2 Fitting 2d data Up: 8. Least square fitting Previous: 8. Least square fitting
Thomas Proffen {Billinge}
1999-06-26