YARM: Yet Another Relaxation Matrix program
A program for performing nuclear spin relaxation calculations

Sections: Main | Using YARM | Subroutines | Theory | Samples | Resources
Subsections: Introduction | Relaxation | CSA | Structure | Data Fitting | Hydrodynamics | Interface | Utilities | System
fitFunction | fitMultiFunction


&fitFunction

Summary:
      Fits the free parameters in an arbitrary function to experimental data using nonlinear least squares.

Usage:
&fitFunction( $dir_name, \%data, $eq, @estimates );

Description:
      This subroutine uses the Levenburg-Marquardt algorithm to perform least squares minimization on a supplied function. Adapted from Joel Tolmans "lmfit" perl scripts, and his odrfit driver to the ODRPACK libraries.

      Fits the data found in %data to the "FORTRAN-type" equation supplied as $eq beginning with the estimates found in the list @estimates. The equation must be passed as a scaler, enclosed in single quotes, using the following form:
$eq = 'f(1,I) = b(1)*exp(-b(2)*x(I))';
which would fit a 2 parameter single exponential fit.

      A directory called $dir_name is created, and contains subdirectories named after all the atoms found in %data hash. Each of these subdirectories contains XMGR files (use: xmgr -batch odrfit.xmgr.batch) of the fit for that nucleus and the actual input and output file from the odrpack fit.

      In the directory "test" will be a fit_summary.txt file which summaries the results of the fit. See the Netlib software repository http://www.netlib.org/ for more details, especially the section on ODRPACK: http://www.netlib.org/odrpack/

      ODRPACK was created by
Paul T. Boggs, Richard H. Byrd, Janet E. Rogers and Robert B. Schnabel
Center for Computing and Applied Mathematics
National Institute of Standards and Technology (NIST)
Gaithersburg, MD 20899

Example:
      Fitting some experimentally measured R1 relaxation data to 2 parameter single exponential fit, the data is a list of 3 values per data point, X1, Y1, dY1, X2, Y2, dY2, etc...

Of course, the data can be read in from a file using the &readData subroutine.

$atom = "A 1 ARG N";
$data{$atom} = [
0.00, -68.01300, 2.0,
0.25, -52.13200, 2.0,
0.50, -39.70800, 2.0,
0.75, -29.16000, 2.0,
1.00, -22.54300, 2.0,
1.25, -16.90200, 2.0,
1.50, -12.70400, 2.0,
1.75,  -9.60580, 2.0,
2.00,  -7.24160, 2.0,
2.25,  -5.47010, 2.0,
];

$debug=0;
$eq = 'f(1,I) = b(1)*exp(-b(2)*x(I))';
@est = ( 1, 1 );
%fit_data = &fitFunction( \%data, "test", $eq, @est );

($b1, $b1err, $b2, $b2err) = @{ $fit_data{$atom} };
print "b1=$b1($b1err) b2=$b2($b2err)\n";

Which gives as a result:
b1=-6.83877238E+01(2.7622E-01) b2=1.11536372E+00(7.6625E-03)
      Additionally, the directory yarm_work/fits/A_1_ARG_N/ contains files with the the full statistical analysis of the fit (odrfit.out), the original input file (odrfit.in), the original input data and the fitted curve (odrfit.xmgr) and an XMGR batch file to fascilitate visualizing the results by typing "xmgr -batch odrfit.xmgr.batch".


&fitMultiFunction

Summary:
      Fits the free parameters in an arbitrary function to experimental data using nonlinear least squares. Usage:
&fitMultiFunction( \%data, $dir_name, \%fit_scheme );

Description:
      Works by calling fitFunction multiple times, once for each entry in %fit_scheme. In this manner, you can automate many fitting schemes. Example:

$eq = 'f(1,I) = b(1) - (b(1)-b(2))*exp(- b(3)*x(I))';
%r1_scheme = (
	"3param"   => [ "all", $eq, -0.3,	31,   1.6 ],
	"2param"   => [ "all", $eq, "0f",	 1, 	1 ],
);
%R1_raw  = &readData( $R1_raw_data );
&fitMultiFunction( \%R1_raw, "Fit_R1", \%r1_scheme );
      This example will perform a 3 and 2 parameter fit on some R1 data, placing the results in the subdirectory "Fir_R1", underwhich the subdirectories "3param" and "2param" will contain the results.