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


next up previous contents index
Next: functionfitter - Constructor Up: fitting - Module Previous: fitting - Module


functionfitter - Tool



Package utility
Module fitting


Postscript file available

Tool to do simple fitting of numeric data

include "functionfitter.g"

Constructors
functionfitter Construct a functionfitter tool
Functions
done Destroy this tool
filter Filter a data array
fit Fit the data
getchisq Recover the chi squared of the fit
getdata Recover the internal data arrays
geterror Recover the error of the fitted parameters
getfunctionstate Recover the state of the function
getmodel Evaluate the model
getresidual Evaluate the residual
getsolution Recover the solution vector
medianclip Median clip a data array
plot Plot data and fit
setcoordsys Set a new Coordinate System
setdata Set the data to fit
setdatafromtable Set the data to fit from a table
setfunction Set the function you wish to fit
setparameters Set the parameters for your function
type Return tool type


Summary

The functionfitter tool offers fitting of functions to (non-complex) numeric data. It can

You load this tool by including the Glish script 'functionfitter.g'. This will create for you a defaultfunctionfitter toolwhich you can use. You can refer to to it by its short-hand name dff. You can of course make your own tool as well which you should do if you are using more than one at a time (because each one contains state).



Example
- include 'functionfitter.g'
NORMAL: defaultfunctionfitter (dff) ready for use      # Default tool created for you
- dff.type()
functionfitter                                         # Fun type function
-
- ff1 := functionfitter()                              # Make your own tool
- ff2 := functionfitter()                              # and another



Example
In the following example, we use the default Functionfitter tool to fit a straight line to some x and y vectors. The straight line is specified algebraicly.

% glish
- xx := 1:10                       # Create some data 
- yy := 2 + 3*xx
- 
- include 'functionfitter.g'       # Load default functionfitter tool
- dff.setdata(xx,yy,xunit='Hz')    # Set data
- dff.setfunction ('p0 + p1*x')    # Set functional form to fit (default pars [0,0])
- print dff.fit()                  # Linear fit (initial guess not needed)
[2 3]
-
- est := 1.2 * dff.getsolution()   # Set guess slightly wrongly
- dff.setparameters (est)
- dff.fit (fixed=[F,T])            # Fit holding second parameter fixe
[-1.3 3.6]                         # and see fit go a bit wrong
- print dff.getchisq()             # Print chi-squared
29.7
- dff.plot()                       # Plot data, model and fit



Example
In this example we add some noise to our simple data vectors, do a linear fit and recover the errors in the parameters.
% glish
- x := 1:10                           # Make some data
- y := 2 + 3*x
-
- include 'randomnumbers.g'           # Load randomnumbers tool
- r := randomnumbers()
- g := r.normal(0,0.1,10)             # Generate Gaussian noise
- y +:= g;                            # add to data
-
- include 'functionfitter.g'          # Load default functionfitter tool
- dff.setdata (x, y)
- dff.setfunction ('p0 + p1*x')       # Functional form is a straight line
- dff.fit()                           # Now do a linear fit and see solution
[1.96462 2.98964]   
- dff.geterror()                      # Recover errors in parameters
[0.178235 0.0287251]  
- dff.plot(model=F)                   # Plot data and fit






Example
In the following example, we use the default Functionfitter tool to fit a 2-D Gaussian to some data.
% glish
- include 'functionals.g'          # Load basic functionals module
- n := 50                          # Size of arrays
-                                  # Create a Gaussian2D functional with parameters
- p := [1.0, n/2, n/2, n/3, 0.5, 30*pi/180]    # Amp, posx, posy, major, ratio, pa
- g2d := dfs.gaussian2d(p)         
-
-                                  # Generate coordinate and value arrays
- x := [];                         # X packed as tuplet coordinate vectors in a 1-D vector
- z := [];                         # i.e. like ( [x1,x2]$_1$, [x1,x2]$_2$, [x1,x2]$_3$ )
#
- k := 1;
- l := 1;
- for (j in 1:n) {
    for (i in 1:n) {
      x[l] := i;                     # Fill x
      x[l+1] := j;
      l +:= 2;
#
      z[k] := g2d.f([i,j])           # Fill function
      k +:= 1
    }
  }
#
- include 'functionfitter.g'         # Create fitter
- dff.setfunction(g2d)               # Set functional
- dff.setdata (x, z)                 # Set data
- sol := dff.fit(F)                  # Do non-linear fit
- print 'Expected = ', p             # True result
Expected =  [1 25 25 16.6667 0.5 0.523599] 
- print 'Solution = ', sol           # Fitted result
Solution =  [1 25 25 16.6667 0.5 0.523599]




next up previous contents index
Next: functionfitter - Constructor Up: fitting - Module Previous: fitting - Module   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