2.1.11.2 FitpolyFitpoly-GlobalFunction
Description
Fit a polynomial equation to XY vectors and return the coefficients and statistical results.
Fit a polynomial equation to a curve and return the coefficients and statistical results.
Fit a polynomial equation to a curve or curve segment.
Fit a polynomial equation to a curve or curve segment and return the parameter coefficients.
Syntax
bool fitpoly( vector & vX, vector & vY, int nOrder, vector & pCoeff = NULL, vector & pError = NULL, RegStats * psRegStats = NULL )
bool fitpoly( Curve & cv, int nOrder, vector & pCoeff = NULL, vector & pError = NULL, RegStats * psRegStats = NULL, int iBeg = 0, int iEnd = -1, double * pxmin = NULL, double * pxmax = NULL )
BOOL fitpoly( Curve & crv, int ipolyorder, double * coeff, int inumer = 0, int idenom = 0 )
int fitpoly( int nOrder, double * pX, double * pY, int nSize, vector & vCoeff, vector & vError = NULL, RegStats * psRegStats = NULL, bool * pbXNorm = NULL, LROptions* pstLROptions = NULL )
Parameters
- vX
- [input] vector containing data points of the independent variables
- vY
- [input] vector containing data points of the dependent variables
- nOrder
- [input] degree of polynomial equation
- pCoeff
- [output] vector containing parameter coefficients
- pError
- [output] vector containing parameter errors
- psRegStats
- [output] struct containing statistical results, such as Chi^2 etc.
- cv
- [input] curve to fit
- nOrder
- [input] degree of polynomial equation
- pCoeff
- [output] vector containing parameter coefficients
- pError
- [output] vector containing parameter errors
- psRegStats
- [output] struct containing statistical results, such as Chi^2 etc.
- iBeg
- [input] beginning index of data points to fit. Index values are 0 based. The default value 0 refers to the lower bound.
- iEnd
- [input] ending index of data points to fit. Index values are 0 based. The default value -1 refers to the upper bound.
- pxmin
- [output] pointer to min x in range
- pxmax
- [output] pointer to max x in range
- crv
- [input] curve to fit
- ipolyorder
- [input] degree of polynomial equation
- coeff
- [output] array containing parameter coefficients
- inumer
- [input] Nth segment of M equal curve segments (default 0 fits entire range as one segment)
- idenom
- [input] number M of equal curve segments (default 0 fits entire range as one segment)
- nOrder
- [input] Degree of polynomial equation, nOrder >= 1
- pX
- [input] Vector containing data points of the independent variables, size of pX = nSize
- pY
- [input] Vector containing data points of dependent variable, size of pY = nSize
- nSize
- [input] The number of observations, n. n>=2
- vCoeff
- [output] Vector containing parameter coefficients
- vError
- [output] Vector containing parameter errors
- psRegStats
- [output] Struct containing statistical results, such as Chi^2 etc
- pbXNorm
- [output] true for performing normalization on the source data in the case of source data have large absolute value but relative small range. false for other case.
- pstLROptions
- [input] Input pointer to struct for linear regression options.
Return
Returns TRUE on success and FALSE on failure.
Returns TRUE on success and FALSE on failure.
Returns TRUE on success and FALSE on failure.
Returns STATS_NO_ERROR or positive on successful exit and a negative STATS error code on failure.
LARGE_X_NORMALIZED(1): the independent data have large value and small range, so are normalized first.
Error Codes:
STATS_INPUT_NULL_POINTER(-156): pX and pY must not be empty.
STATS_ERROR_TOO_FEW_DATA_PTS(-165): nSize must not be less than 2
STATS_ERROR_SETTING(-151): nOrder must not be less than 1
STATS_ERROR_SVD_FAIL(-179): The singular value decomposition is failed
STATS_ERROR_RANK_DEFICIENT(-180): The pX is not full rank.
STATS_ERROR_BAD_WEIGHT(-108): Some data of the weight are less than zero
Examples
EX1
void fitpoly_ex1()
{
vector vX = {1, 2, 3, 4, 5};
vector vY = {5.5, 43.1, 128, 290.7, 498.4};
int nOrder = 3;
vector vCoeff;
vCoeff.SetSize(nOrder+1);
fitpoly(vX, vY, nOrder, vCoeff);
for (int i = 0; i <= nOrder; i++)
printf("%g\n", vCoeff[i]);
}
EX2
void fitpoly_ex2(Curve& crv)
{
int nOrder = 3;
vector vCoeff;
vCoeff.SetSize(nOrder+1);
vector vError(nOrder+1);
fitpoly(crv, nOrder, vCoeff, vError);
for (int i = 0; i <= nOrder; i++)
printf("%d: Coeff = %g, Error = %g\n", i+1, vCoeff[i], vError[i]);
}
void run_fitpoly_ex2()
{
Worksheet wks = Project.ActiveLayer();
if( wks && wks.GetNumCols() >= 2 )
{
Curve crv(wks, 0, 1);
fitpoly_ex2( crv );
}
}
EX3
void fitpoly_ex3(Curve& crv)
{
int nOrder = 3;
vector vCoeffs(nOrder+1);
if( fitpoly(crv, nOrder, vCoeffs))
{
for (int i = 0; i <= nOrder; i++)
printf("%g\n", vCoeffs[i]);
}
}
void run_fitpoly_ex3()
{
Worksheet wks = Project.ActiveLayer();
if( wks && wks.GetNumCols() >= 2 )
{
Curve crv(wks, 0, 1);
fitpoly_ex3( crv );
}
}
EX4
void fitpoly_ex4()
{
vector vX = {1, 2, 3, 4, 5};
vector vY = {5.5, 43.1, 128, 290.7, 498.4};
int nOrder = 3;
int nSize = vX.GetSize();
vector vCoeff;
vCoeff.SetSize(nOrder+1);
fitpoly(nOrder, vX, vY, nSize, vCoeff);
for (int i = 0; i <= nOrder; i++)
printf("%g\n", vCoeff[i]);
}
Remark
The function used for fitting function parameter initialization.
Fit a polynomial equation to a curve or curve segment and return the parameter coefficients. If specified, only the Nth segment of M equal curve segments is fit.
See Also
fitpoly_range, fit_polyline
Header to Include
origin.h
Reference
|