VF_linfit VD_linfit VE_linfit
VF_linfitwW VD_linfitwW VE_linfitwW
FunctionData-fitting to models y=f(x) linear in the parameters
Syntax C/C++#include <MFstd.h>
int VF_linfit( fVector A, iVector AStatus, unsigned npars, fVector X, fVector Y, ui sizex,
void funcs(fVector BasFuncs, float x, unsigned nfuncs));

int VF_linfitwW( fVector A, fMatrix Covar, iVector AStatus, unsigned npars, fVector X, fVector Y, fVector InvVar, ui sizex,
void funcs(fVector BasFuncs, float x, unsigned nfuncs));

C++ MatObj#include <OptiVec.h>
int vector<T>::linfit( const vector<int>& AStatus, const vector<T>& X, const vector<T>& Y,
void funcs(fVector BasFuncs, float x, unsigned nfuncs));

int vector<T>::linfitwW( matrix<T> Covar, const vector<int>& AStatus, const vector<T>& X, const vector<T>& Y, const vector<T>& InvVar,
void funcs(fVector BasFuncs, float x, unsigned nfuncs));

int vector<T>::linfitwW( matrix<T>* Covar, const vector<int>& AStatus, const vector<T>& X, const vector<T>& Y, const vector<T>& InvVar,
void funcs(fVector BasFuncs, float x, unsigned nfuncs));

Pascal/Delphiuses MFstd;
function VF_linfit( A:fVector; AStatus:iVector; npars:UInt; X, Y:fVector; sizex:UIntSize; funcs:Pointer ): IntBool;

function VF_linfitwW( A:fVector; Covar:fMatrix; AStatus:iVector; npars:UInt; X, Y, InvVar:fVector; sizex:UIntSize; funcs:Pointer ): IntBool;

DescriptionThe input data X, Y (and InvVar) are used to evaluate the parameters ai of a general linear function,
y = a0f0(x) + a1f1(x) + a2f2(x)...
The parameters ai are returned in the vector A.

Arguments:
Avector of size npars; returns the coefficients
Covarmatrix of dimensions [npars, npars]; returns the covariances of the coefficients
AStatusvector of size npars; decides which parameters are treated as free or as fixed
nparstotal number of parameters
X, Y, InvVarvectors of size sizex, holding the input data
funcsuser-defined model function
 
Your model function may actually contain more parameters than you wish to treat as adjustable. This is why you have to provide the vector AStatus, which contains the necessary information about which parameters are to be held fixed at their input values (AStatus[i] = 0) and which are free (AStatus[i] = 1). Any fixed parameters must be initialized in A prior to calling VF_linfit. npars denotes the total number of parameters in A (not only the free parameters!).

The model function funcs must calculate the individual fi(x) for any argument x and store the fi(x) in a vector BasFuncs of size npars. In C/C++, it has to be defined as
void MyFunc( fVector BasFuncs, float x, unsigned nfuncs)
{
  BasFuncs[0] = f0( x );
  BasFuncs[1] = f1( x);
  . . .
}

and shall be passed to VF_linfit by calling
VF_linfit( A, AStatus, npars, X, Y, sizex, MyFunc );
In Pascal/Delphi, the model function has to be defined as
procedure MyFunc( BasFuncs:fVector; x:Single; nfuncs:UInt );
begin
  VF_Pelement( BasFuncs, 0 )^ := f0( x );
  VF_Pelement( BasFuncs, 1 )^ := f1( x );
  . . .
end;

and shall be passed to VF_linfit by calling
VF_linfit( A, AStatus, npars, X, Y, sizex, @MyFunc );
Note the address-of operator in front of "MyFunc.". In Turbo Pascal, the model function must be compiled with the Force-Far-Calls option {$F+}.

The functions f0( x ) etc. must not contain the parameters ai.
In the weighted variant, VF_linfitwW, the vector InvVar has to contain the inverse of the variances of the individual X-Y data points, and the matrix Covar will be filled with the covariances of the parameters ai on output: MCovari,j = covariance( ai, aj ).

Internally, VF_linfit employs a Singular Value Decomposition algorithm to obtain a solution even for (near-)singular linear systems. Thereby, coefficients ai whose significance is lower than a threshold, Thresh, are set to 0 instead of infinity. This threshold can be modified by calling VF_setLinfitNeglect. The current threshold can be retrieved by VF_getLinfitNeglect.

In the rare case of failure, this function returns 1 (TRUE) and sets all A[i] = 0.

Return valueFALSE (0), if no error occurred, otherwise TRUE (non-zero).
See alsoMF_linfit,   VF_nonlinfit,   chapter 13,  FITDEMO*.*

MatrixLib Table of Contents  OptiVec home