PsiLAB Help Manual Page

Section: Nonlinear Least Square Fits (MinPACK)

Name: Fit

Nonlinear Least Square Fit module.
This module provides several function for nonlinear least square method fitting of special or user contributed functions to one or two dimensional datasets using the MINPACK library.
Generic fit 1
[ sigma:float *
param:(float,'b,'c) Matrix.t *
paramsig:(float,'b,'c) Matrix.t] =
Fit.generic1
x:(float,'b,'c) Matrix.t
y:(float,'b,'c) Matrix.t
param0:(float, 'b, 'c) Matrix.t
func:(float * (float, 'a, 'b) Matrix.t -> float)
?tol:float
?plot:bool
?verbose:bool

This functions fits the dataset y(x), x and y are vectors of same size, to the one dimensional user contributed function func :
[ y:float ] = func
x:float *
pa:(float,'b,'c) Matrix.t

Note: the arguments are combined in a tuple !
The user is responsible to give the fit function a rough approximation of the initial paramter set with the vector param0 . The fitted parameters are returned in the float vector param and the standard deviation for each paramter in the vector paramsig :
param.{i} -> i-th parameter of func (user defined)

Additionaly, the deviation of the last fit iteration is returned in sigma :
sigma = sqrt ( (Sum [ y(x) - f(x,param) ] 2 ) / (n-param num )

The optional tol argument controls the descision for a successfull fit. The default value is 1.0E-6 . The plot argument set to true enables a final plot of the fitted function and the dataset.
Generic fit 2
[ sigma:float *
param:(float,'b,'c) Matrix.t *
paramsig:(float,'b,'c) Matrix.t ] =
Fit.generic2
x:(float,'b,'c) Matrix.t
y:(float,'b,'c) Matrix.t
z:(float,'b,'c) Matrix.t
param0:(float, 'b, 'c) Matrix.t
func:(float,'b,'c) Matrix.t
?tol:float
?plot:bool
?verbose:bool

This functions fits the dataset z(x,y), x and y are index vectors and z is a 2 dimensional matrix consistent with the index vectors, to the two dimensional user contributed function func :
[ z ] = func
x:float *
y:float *
pa:(float,'b,'c) Matrix.t

Note: the arguments are combined in a tuple !
The user is responsible to give the fit function a rough approximation of the initial paramter set with the vector param0 . The fitted parameters are returned in the float vector param and the standard deviations for each parameter in the vector paramsig :
param.{i} -> i-th parameter (user defined)

Additionaly, the deviation of the last fit iteration is returned in sigma :
sigma = sqrt ( (Sum [ z(y,x) - F(x,y,param) ] 2 ) / (n-param num )

The optional tol argument controls the descision for a successfull fit. The default value is 1.0E-6 . The plot argument set to true enables a final plot of the fitted function and the dataset.
Gaussfit 1
[ sigma:float *
param:(float,'b,'c) Matrix.t *
paramsig:(float,'b,'c) Matrix.t ] =
Fit.gauss1
x:(float,'b,'c) Matrix.t
y:(float,'b,'c) Matrix.t
?tol:float
?plot:bool
?verbose:bool

This functions fits the dataset y(x), x and y are vectors of same size, to the one dimensional generic gauss function
F(x) = y 0 + y m * exp(-1/(2*sig 2 ) *(x-x 0 ) 2 )

The fitted parameters are returned in the float vector param and the standard deviation for each paramter in the vector paramsig :
param.{1} -> y 0
param.{2} -> y m
param.{3} -> sig
param.{4} -> x 0

Additionaly, the deviation of the last fit iteration is returned in sigma :
sigma = sqrt ( (Sum [ y(x) - f(x,param) ] 2 ) / (n-param num )

The optional tol argument controls the descision for a successfull fit. The default value is 1.0E-6 . The plot argument set to true enables a final plot of the fitted function and the dataset.
Gaussfit 2
[ sigma:float *
param:(float,'b,'c) Matrix.t *
paramsig:(float,'b,'c) Matrix.t ] =
Fit.gauss2
x:(float,'b,'c) Matrix.t
y:(float,'b,'c) Matrix.t
z:(float,'b,'c) Matrix.t
?tol:float
?plot:bool
?verbose:bool

This functions fits the dataset z(x,y), x and y are index vectors and z is a 2 dimensional matrix consistent with the index vectors, to the two dimensional generic gauss function
F(x,y) = z 0 + z m *exp[ -(
(1/(2*sig x 2 )*(x-x 0 ) 2 ) +
(1/(2*sig y 2 )*(y-y 0 ) 2 ) ]

The fitted parameters are returned in the float vector param and the standard deviation for each paramter in the vector paramsig :
param.{1} -> z 0
param.{2} -> z m
param.{3} -> sig x
param.{4} -> x 0
param.{5} -> sig y
param.{6} -> y 0

Additionaly, the deviation of the last fit iteration is returned in sigma :
sigma = sqrt ( (Sum [ z(y,x) - F(x,y,param) ] 2 ) / (n-param num )

The optional tol argument controls the descision for a successfull fit. The default value is 1.0E-6 . The plot argument set to true enables a final plot of the fitted function and the dataset.
Gaussfit 2L
[ sigma:float *
param:(float,'b,'c) Matrix.t *
paramsig:(float,'b,'c) Matrix.t ] =
Fit.gauss2l
x:(float,'b,'c) Matrix.t
y:(float,'b,'c) Matrix.t
z:(float,'b,'c) Matrix.t
?tol:float
?plot:bool
?verbose:bool

This functions fits the dataset z(x,y), x and y are index vectors and z is a 2 dimensional matrix consistent with the index vectors, to the two dimensional generic gauss and a linear function (polynomial of degree 1 )
F(x,y) = z 0 + a*x + b*y + z m
* exp[ -(
(1/(2*sig x 2 )*(x-x 0 ) 2 ) +
(1/(2*sig y 2 )*(y-y 0 ) 2 ) ]

The fitted parameters are returned in the float vector param and the standard deviation for each paramter in the vector paramsig :
param.{1} -> z 0
param.{2} -> z m
param.{3} -> sig x
param.{4} -> x 0
param.{5} -> sig y
param.{6} -> y 0
param.{7} -> a
param.{8} -> b

Additionaly, the deviation of the last fit iteration is returned in sigma :
sigma = sqrt ( (Sum [ z(y,x) - F(x,y,param) ] 2 ) / (n-param num )

The optional tol argument controls the descision for a successfull fit. The default value is 1.0E-6 . The plot argument set to true enables a final plot of the fitted function and the dataset.
Examples:
[] let x = fmatrix ~fill:[1.0;0.1;1000.0] () ;; 
[] let pa = fmatrix ~const:[55.0;130.0;40.0;450.0] () ;; 
[] let yd = (fun x -> (gaussf x pa)*(1.0+Random.float 0.2)) ||<< x ;; 
[] let gf (x,p) = 
	 gaussf x p 
	 ;; 

(* perform a generic fit with our function definition *) 
[] let (sigma,err,pfa) = Fit.generic1 x yd pa gf ~plot:true () ;; 

(* use the built in gauss funtcion *) 
[] let (sigma,pf,pfsig) = Fit.gauss1 x yd ~plot:true () ;; 

[] let x = fmatrix ~fill:[1.0;2.0;100.0] () ;; 
[] let y = fmatrix ~fill:[1.0;4.0;200.0] () ;; 
[] let pz = fmatrix ~const:[50.0;70.0;10.0;40.0;23.0;70.0] () ;; 
[] let gz = fun (y,x) -> (gaussf2 x y pz)*(1.0+(Random.float 0.1)) ;; 
[] let z = gz ||<<< (y,x) ;; 
[] let gf (x,y,p) = 
	 gaussf2 x y p 
	 ;; 

(* perform a generic fit with our function definition *) 
[] let (sigma,pf,pfsig) = Fit.generic2 x y gz pz gf ~plot:true () ;; 

(* use the builtin gauss function fit *) 
[] let (sigma,pf,pfsig) = Fit.gauss2 x y gz ~plot:true() ;; 

Printed by PsiLAB