Getting Started | Documentation | Glish | Learn More | Programming | Contact Us |
Version 1.9 Build 1556 |
|
Package | utility | |
Module | fitting |
include "functionfitter.g"
functionfitter | Construct a functionfitter tool |
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).
- 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
% 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
% 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
% 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]