📄 leastsqu.h
字号:
/*NRY 1/90 - The following procedures are interfaces to curve fitting
algorithms which use the Least Square Method. Given a set of data
points and an uncertainty factor for each point, the equation of
a line or curve is returned depending on which function is used.
This line or curve is chosen such that the square of the distance
from any datapoint to a point on the curve is minimized. More detailed
descriptions follow after definitions.
*/
/*POLYFIT NOT TESTED YET*/
#pragma LeastSquare
#include <GaussJ.h>
typedef
struct { /* see UNIT Putpoint */
int noNums;
double X[MAX+1], Y[MAX+1];
double sigma[MAX+1];
} DataPoints;
typedef struct {
double A, B; /* A = y-int. B = slope */
double sigA, sigB; /* A & B's uncertainty factors */
double chi2, Q; /* chi-square and goodness-of-fit */
} LineReturn;
void LineFit(DataPoints &ptRec, double weighting, LineReturn &retVal);
void PolyFit(DataPoints &ptRec, PolyReturn &retVal);
/*
LineFit
-------
This procedure returns a linear solution using the method stated above.
LineFit accepts an argument of the type DataPoints which
includes: noNums - the number of data points to be analyzed.
X[], Y[] - the actual coordinates of each point.
sigma[] - the uncertainty factor of each point.
LineFit also accepts a weighting term which is a flag (zero or nonzero)
indicating whether or not to use the values in the sigma array for
weights. If weighting = 0.0 the all sigmas are uniform and assumed
to be 1. In other words sigma[i] = 1 for all points X[i],Y[i].
LineFit also accepts an argument of the type LineReturn, this is what
the resultant linear equation is returned in. LineReturn
includes: A - the Y-intercept of the line.
B - the slope of the line.
sigA,sigB - these are the uncertainty factors for A and B.
chi2 - chi squared or the "goodness-of-fit" term.
Q - the probability of the "goodness-of-fit".
If Q > .1 the goodness-of-fit is believable;;
If Q > .001 the goodness-of-fit may be acceptable if errors are
nonnormal or have been moderately underestimated.
If Q < .001 the model and/or estimation can be called into question.
Note: This function has not been fully implemented to handle a nonzero
weighting factor. There is a HALT statement in the function GammaQ.
PolyFit
-------
This function returns a nonlinear solution using the least square
method. PolyFit takes an argument of the type DataPoints
just as above. It also takes an argument of the type PolyReturn which
is defined in the module GaussJ. PolyReturn includes:
coeff[] - this holds the coefficients of the polynomial equation.
noTerms - the number of coefficients.
listA[] - this array keeps track of the coefficients to be fitted.
minFit - the number of elements to be fitted from listA[].
covarMatrix - the covariance matrix.
subI,subJ - the size of the covariance matrix.
chi2 - chi squared, the goodness-of-fit.
This procedure returns it's information in the type
PolyReturn. Several elements of PolyReturn must be filled in before
the call though. An initial guess of the coefficients must be made.
The noTerms must be set. The coefficients which are to be fitted
must be entered in listA, the others are held constant. MinFit is set
to the number of terms to be fitted. The rest is filled in by PolyFit.
Note: A more detailed description of the process and equations can be found
in Numerical Recipes by Press, Flannery, Teukolsky, and Vetterling.
-----------------
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -