libSUFR
a LIBrary of Some Useful Fortran Routines
All Classes Namespaces Files Functions Variables Pages
sufr_fitting Module Reference

Procedures to fit functions to data. More...

Functions/Subroutines

subroutine, public linear_fit_yerr (ndat, xdat, ydat, yerr, ncoef, fcoef, freecoef, ncov, covmat, chisq, myfunc)
 Fit the model coefficients using a chi^2 method.
 
subroutine, public nonlin_fit_yerr (ny, ndat, xdat, ydat, yerr, ncoef, fcoef, freecoef, nmat, covmat, curvmat, chisq, iterstat, myfunc)
 Levenberg-Marquardt method to reduce the value of chi-squared of a fit of a set of data points with errors in Y to a non-linear function.
 
subroutine, public nonlin_fit_example_myfunc (ny, xdat, ncoef, fcoef, ydat, dyda)
 Dummy example function myFunc for nonlin_fit_yerr(): return the value and partial derivatives.
 
pure subroutine, public basefunc_polynomial (xval, ncoef, basefunc)
 User-provided base function to fit a polynomial to.
 

Detailed Description

Procedures to fit functions to data.

Function/Subroutine Documentation

◆ basefunc_polynomial()

pure subroutine, public sufr_fitting::basefunc_polynomial ( real(double), intent(in) xval,
integer, intent(in) ncoef,
real(double), dimension(ncoef), intent(out) basefunc )

User-provided base function to fit a polynomial to.

Parameters
xValX value to evaluate yVal = basefunc_polynimial(xVal) for
nCoefNumber of coefficients for the base function
baseFuncBase function i for coefficent i. For a polynomial, BF(i) = x**(i-1) (output)

Definition at line 585 of file fitting.f90.

References sufr_kinds::double.

◆ linear_fit_yerr()

subroutine, public sufr_fitting::linear_fit_yerr ( integer, intent(in) ndat,
real(double), dimension(ndat), intent(in) xdat,
real(double), dimension(ndat), intent(in) ydat,
real(double), dimension(ndat), intent(in) yerr,
integer, intent(in) ncoef,
real(double), dimension(ncoef), intent(inout) fcoef,
integer, dimension(ncoef), intent(in) freecoef,
integer, intent(in) ncov,
real(double), dimension(ncov,ncov), intent(out) covmat,
real(double), intent(out) chisq,
external myfunc )

Fit the model coefficients using a chi^2 method.

Do a chi^2-minimisation fit of nDat data points xDat and yDat, the latter with errors yErr, to function func. The fit uses the nCoef coefficients stored in fCoef() that should be varied if the corresponding entry in freeCoef=1, fixed if =0.

The fitting function must be provided as the subroutine myFunc which returns a value y = myFunc(x, basefunc, nCoef), where y is a linear combination of x, i.e. y = SUM(i=1,nCoef) fCoef(i) * basefunc(i)(x). For example, for a polynomial, basefunc(i) = x**(i-1).

The routine returns the fit coefficients in fCoef(), the variance-covariance matrix in covMat sith size nCov, and the chi^2 value in chiSq.

Parameters
nDatNumber of data points
xDatX values of the data (nDat)
yDatY values of the data (nDat)
yErrErrors (standard deviations) of the y values (nDat)
nCoefNumber of coefficients of the fitting function
fCoefCoefficients of the fitting function (nCoef) (output)
freeCoefFix coefficient coef(i) if freeCoef(i)=0, otherwise let if vary freely and fit it (nCoef)
nCovSize of both dimensions of covMat()
covMatCovariance matrix (nCov,nCov) (output)
chiSqChi^2 - chi squared (output)
myFuncExternal subroutine that describes the model value of Y for given value X
Note
  • Needs sort_var_covar_matrix() and solve_linear_equations_Gauss_Jordan()
See also
Numerical Recipes in Fortran 77, Sect.15.4

Definition at line 69 of file fitting.f90.

References sufr_kinds::double, sufr_system::quit_program_error(), and sufr_system::warn().

Here is the call graph for this function:

◆ nonlin_fit_example_myfunc()

subroutine, public sufr_fitting::nonlin_fit_example_myfunc ( integer, intent(in) ny,
real(double), intent(in) xdat,
integer, intent(in) ncoef,
real(double), dimension(ncoef), intent(in) fcoef,
real(double), dimension(ny), intent(out) ydat,
real(double), dimension(ny,ncoef), intent(out) dyda )

Dummy example function myFunc for nonlin_fit_yerr(): return the value and partial derivatives.

Parameters
nYNumber of Y values for each X value (normally 1, but e.g. 2 for a sky position with two coordinates)
xDatInput X value for the current data point
nCoefNumber of coefficients
fCoefVector of coefficients that describe the function
yDatY values for the current data point (output)
dydaPartial derivatives for yDat: 1: dy/dfCoef(1), ..., n: dy/dfCoef(n) (output)
Note
Write a subroutine with the same interface to use with nonlin_fit_yerr()

Definition at line 406 of file fitting.f90.

References sufr_kinds::double, sufr_system::quit_program_error(), sufr_system::swapdbl(), and sufr_system::warn().

Here is the call graph for this function:

◆ nonlin_fit_yerr()

subroutine, public sufr_fitting::nonlin_fit_yerr ( integer, intent(in) ny,
integer, intent(in) ndat,
real(double), dimension(ndat), intent(in) xdat,
real(double), dimension(ny,ndat), intent(in) ydat,
real(double), dimension(ndat), intent(in) yerr,
integer, intent(in) ncoef,
real(double), dimension(ncoef), intent(inout) fcoef,
integer, dimension(ncoef), intent(in) freecoef,
integer, intent(in) nmat,
real(double), dimension(nmat,nmat), intent(out) covmat,
real(double), dimension(nmat,nmat), intent(out) curvmat,
real(double), intent(out) chisq,
real(double), intent(inout) iterstat,
external myfunc )

Levenberg-Marquardt method to reduce the value of chi-squared of a fit of a set of data points with errors in Y to a non-linear function.

You will need to call this routine repeatedly, with different values for iterStat, until chiSq no longer decreases (significantly)

Parameters
nYNumber of Y values for each X value (normally 1, but e.g. 2 for a sky position with two coordinates)
nDatNumber of data points in X and Y
xDatX data points to fit
yDatY data points to fit
yErrErrors (standard deviations) for yDat
nCoefNumber of coefficients used to describe the non-linear function myFunc
fCoefCoefficients for the non-linear function myFunc, updated after each call
freeCoefDetermines which coefficients for the non-linear function myFunc should be fitted for. Set freeCoef(i) = 0 in order to keep fCoef(i) fixed
nMatSize of the matrices; > nCoef
covMatVariance-covariance matrix - returned on last call with iterStat. Fixed parameters return zero (co)variances
curvMatHessian/curvature matrix - double partial derivative of chi squared to two coefficients fCoef
chiSqChi squared
iterStatIteration status; Set to <0 for initialisation in the first call, and let it vary in subsequent calls, until the fit converges. After that, set to 0 to return the variance-covariance and curvature matrices on the final call. IterStat decreases 10x if chiSq becomes smaller, and increases 10x otherwise.
myFuncExternal subroutine that describes the model value of Y and partial derivatives dY/dXi for given value X and function coefficients fCoef
See also
Numerical Recipes in Fortran, 15.5: Modelling of Data / Non-linear models
Note
Uses sort_var_covar_matrix(), solve_linear_equations_Gauss_Jordan() and nonlin_fit_eval()

Definition at line 191 of file fitting.f90.

References sufr_numerics::deq0(), and sufr_kinds::double.

Here is the call graph for this function: