Getting Started | Documentation | Glish | Learn More | Programming | Contact Us |
Version 1.9 Build 1556 |
|
Package | utility | |
Module | mathematics |
include "polyfitter.g"
eval | Evaluate a polynomial (usually the result of a fit) |
fit | Fit a polynomial of a specified order to data |
multifit | Fit several sets of data to polynomials |
polyfitter is a class that does a linear least-squares fit of a polynomial of a specified order to data. An optional estimate of the errors in the data may be provided. Besides the coefficients, and the estimated errors in the coefficient are output. A function to evaluate the fit polynomial is also provided.
At present, only fits to real data without constraints are supported.
The fits are carried out with double precision.
The computation is actually performed in a C++ executable, not in Glish. Details about the algorithms are available in the documentation of the underlying C++ fitting classes.
polyfitterdemo() and polyfittertest() functions are available.
First we need to get access to the polynomial fitting declarations and create a fitting tool:
include "polyfitter.g" fitter := polyfitter()
Now lets manufacture some data from the polynomial: y = 3x2 + 5
x := 1:10 # 1,2,3,4,5,6,7,8,9,10 y := 3*x*x + 5 # 8, ..., 305
Now, lets perform the fit:
ok := fitter.fit(coeff, coefferrs, chisq, x, y, order=2)
All the parameters up to (but not including) x are output parameters, the others are all input errors. We did not provide an error estimate , so the software assumes the errors are identical for each data point and = 1. The variables have the following meaning:
One can also fit several independent y vectors with a single call using the multifit function.
The other function in this class is one to evaluate a polynomial. Usually it will be the polynomial or polynomialsyou have just fit, but it could be any polynomial you specify. For example, to find out the largest deviation of the fit values from the data values we could do the following:
ok := fitter.eval(y2, x, coeff) print max(abs(y2 - y))
The coeff parameter in the eval function could be the result of a call to fit or multifit. If coeff is a vector, it will return a single polynomial evaluated at x. If coeff is a matrix (two dimensions), it will return N polynomials evaluated at x, where N is the number of elements along the second dimension of coeff.