Getting Started Documentation Glish Learn More Programming Contact Us
Version 1.9 Build 1556
News FAQ
Search Home


next up previous contents index
Next: polyfitter.fit - Function Up: mathematics - Module Previous: gauss1dfitter.setcriteria - Function


polyfitter - Tool



Package utility
Module mathematics


Postscript file available

Least-squares fit polynomials to data

include "polyfitter.g"

Constructors
Functions
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



Description

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, $ \chi^{2}_{}$ 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.



Example

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 $ \sigma$, so the software assumes the errors are identical for each data point and $ \sigma$ = 1. The variables have the following meaning:

ok
The return value is True if the fit succeeds, False otherwise.
coeff
A vector containing the estimated (fit) coefficients. Coefficient zero (i.e., the constant term) is first, the highest order coefficient is last.
coefferrs
The estimated errors in the fit coefficients.
chisq
The $ \chi^{2}_{}$ for this fit.
x,y
The data - x and y must be vectors having the same number of elements.
order
What order polynomial do we want to fit? We specify it this way because we have ``skipped over'' the defaulted sigma argument. You can spell out all arguments this way if you like.

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))

ok
The return value is True if the fit succeeds, False otherwise. (Output)
y2
The polynomial evaluated at x. (Output)
x
Where to evaluate the polynomial. (Input)
coeff
A vector containing the coefficients to use. Coefficient zero (i.e., the constant term) is first, the highest order coefficient is last. (Input)

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.



See Also
polyfitterdemo polyfittertest




next up previous contents index
Next: polyfitter.fit - Function Up: mathematics - Module Previous: gauss1dfitter.setcriteria - Function   Contents   Index
Please send questions or comments about AIPS++ to aips2-request@nrao.edu.
Copyright © 1995-2000 Associated Universities Inc., Washington, D.C.

Return to AIPS++ Home Page
2006-10-15