package jasext.hist.fitter;

import jas2.hist.*;
import java.util.*;

public class LeastSquaresFit extends Fitter
{
	
	/**
	 * 
	 * This algorithm based on CURFIT program from "Data Reduction and Error Analysis
	 * for the Physical Sciences" by Philip R Bevington, Page 237. It performs a least
	 * squared fit using the Marquardt algorithm.
	 * @param func The function to be fit
	 * @param x The x values of the points to be fit
	 * @param y The y values of the points to be fit
	 * @param sigmaY The errors (assumed symmetric) on the y values
	 * @exception FitFailed if the fit was not successful 
	 */
	public void fit(Fittable1DFunction func, double[] x, double[] y, double[] sigmaY) throws FitFailed
	{
		int npts = x.length;
		if (npts != y.length || npts != sigmaY.length) 
			throw new IllegalArgumentException("Invalid arguments to fit");

		double[] a = (double[]) func.getParameterValues().clone();
		int nterms = a.length;

		m_sigmaA = new double[nterms];

		int nFree = npts - nterms;
		if (nFree <= 0) throw new FitFailed("Insufficient degrees of Freedom");
		
		// Calculate weights

		double[] weight = new double[npts];
		for (int i=0; i 10000) 
					throw new FitFailed("Fit exceeded maximum number of iterations");		
			
				double chisqr = calculateChiSquared(func,x,y,weight,a,nFree);;

				// Evaluate Alpha and Beta matrices

				for (int j=0; j 

Ads help maintain this website.