VF_polyfit VD_polyfit VE_polyfit
VF_polyfitwW VD_polyfitwW VE_polyfitwW
   
VF_polyfitEven VD_polyfitEven VE_polyfitEven
VF_polyfitEvenwW VD_polyfitEvenwW VE_polyfitEvenwW
   
VF_polyfitOdd VD_polyfitOdd VE_polyfitOdd
VF_polyfitOddwW VD_polyfitOddwW VE_polyfitOddwW
Functionfitting of one X-Y data set to a polynomial
Syntax C/C++#include <MFstd.h>
int VF_polyfit( fVector A, unsigned deg, fVector X, fVector Y, ui sizex );
int VF_polyfitwW( fVector A, fMatrix Covar, unsigned deg, fVector X, fVector Y, fVector InvVar, ui sizex );

identical syntax for VF_polyfitEven, VF_polyfitEvenwW, VF_polyfitOdd, VF_polyfitOddwW
C++ MatObj#include <OptiVec.h>
int vector<T>::polyfit( const vector<T>& X, const vector<T>& Y );
int vector<T>::polyfitwW( matrix<T> Covar, const vector<T>& X, const vector<T>& Y, const vector<T>& InvVar );
int vector<T>::polyfitwW( matrix<T>* Covar, const vector<T>& X, const vector<T>& Y, const vector<T>& InvVar );

identical syntax for polyfitEven, polyfitEvenwW, polyfitOdd, polyfitOddwW
Pascal/Delphiuses MFstd;
function VF_polyfit( A:fVector; deg:UInt; X, Y:fVector; sizex:UIntSize ): IntBool;
function VF_polyfitwW( A:fVector; Covar:fMatrix; deg:UInt, X, Y, InvVar:fVector; sizex:UIntSize ): IntBool;

identical syntax for VF_polyfitEven, VF_polyfitEvenwW, VF_polyfitOdd, VF_polyfitOddwW
DescriptionThe input data are used to determine the coefficients ai of a polynomial of degree deg.
VF_polyfit, VF_polyfitwW:
Pi = a0 + a1Xi + a2Xi2 ... anXin
A has deg+1 elements.

VF_polyfitEven, VF_polyfitEvenwW:
The polynomial has only even terms,
Pi = a0 + a2Xi2 ... a2nXi2n
A has deg/2+1 elements, as the zero coefficients are not stored.

VF_polyfitOdd, VF_polyfitOddwW:
The polynomial has only odd terms,
Pi = a1Xi + a3Xi3 ... a2n+1Xi2n+1
A has (deg+1)/2 elements, as the zero coefficients are not stored.

The coefficients are determined so as to minimize the deviation between the "theoretical" Pi values, calculated by the polynomial, and the actual Yi values. To be more precise, the figure-of-merit chi-square,
c2 = sum( 1/si2 * (Pi - Yi)2 );
is minimized. In the weighted variants, VF_polyfitwW etc., 1/si2 has to be provided as the vector InvVar. In the non-weighted variant, all si are assumed as 1.0.

Arguments:
Avector of size deg+1; returns the coefficients
Covarmatrix of dimensions [deg+1, deg+1]; returns the covariances of the coefficients.
If the covariances are not needed, one may call the function with Covar=NULL / nil.
degthe degree of the fitting polynomial
X, Y, InvVarvectors of size sizex, holding the input data

If possible, the X axis should include the zero point. Fitting far off the zero-point may lead to poor matching of the polynomial and your data, given the typical form of a polynomial (which has only deg-1 "turns"). In such a case, you should calculate the polynomial not for the original X, but for a shifted X' axis. If, e.g., you have to find a polynomial matching data for x ranging from 9 to 10, you would best shift your X axis by -9.5, in order to have x'=0 in the middle of the range. Shifting by -9.0 for x'=0 at the beginning of the range would also be fine.

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 alsoVF_linregress,   VF_linfit,   VF_chi2,   chapter 13

MatrixLib Table of Contents  OptiVec home