void
JPlotFitQuad2::CalculateFirstPass()
{
	JFloat Y, X, X2, YX, X3, YX2, X4, Sig;
	JFloat tempa, tempb, tempc, det;
	JSize i,j, k;
	JArray<JFloat> yAdjError;
	
	const JPlotDataBase* data = GetData();
	J2DDataPoint point;
	JSize rcount = GetRealElementCount();
	for (i=1; i<= rcount; i++)
		{
		J2DDataPoint point = GetRealElement(i);
		JFloat newVal = 1;
		if (point.yerr != 0)
			{
			newVal = point.yerr;
			}
		yAdjError.AppendElement(newVal);
		}

	JMatrix odata(rcount, 3, 1.0);
	JVector yData(rcount);
	for (i=1; i<= rcount; i++)
		{
		point = GetRealElement(i);
		JFloat yerr = yAdjError.GetElement(i);
		odata.SetElement(i, 1, 1/(yerr*yerr));
		odata.SetElement(i, 2, point.x/(yerr*yerr));
		odata.SetElement(i, 3, point.x*point.x/(yerr*yerr));
		yData.SetElement(i, point.y/(yerr*yerr));
		}
	JMatrix tData = odata.Transpose();
	JMatrix lData = tData * odata;
	JMatrix rData = tData * yData;
	JMatrix parms(3,1);
	JGaussianElimination(lData, rData, &parms);

	for (k=1; k<= 4; k++)
		{
		Y = 0;
		X = 0;
		X2 = 0;
		YX = 0;
		X3 = 0;
		YX2 = 0;
		X4 = 0;
		Sig = 0;
		for (i=1; i<= rcount; i++)
			{
			point = GetRealElement(i);
			JFloat yerr = yAdjError.GetElement(i);
			Y += point.y/(yerr*yerr);
			X += point.x/(yerr*yerr);
			X2 += point.x*point.x/(yerr*yerr);
			YX += point.y*point.x/(yerr*yerr);
			X3 += point.x*point.x*point.x/(yerr*yerr);
			YX2 += point.x*point.x*point.y/(yerr*yerr);
			X4 += point.x*point.x*point.x*point.x/(yerr*yerr);
			Sig += 1/(yerr*yerr);
			}
		JFloat cv1 = 0, cv2 = 0, cv3 = 0;
		for (i=1; i<= rcount; i++)
			{
			point = GetRealElement(i);
			JFloat syi = yAdjError.GetElement(i);
			JFloat yi = point.y;
			JFloat xi = point.x;
			for (j = 1; j <= rcount; j++)
				{
				point = GetRealElement(j);
				JFloat syj = yAdjError.GetElement(j);
				JFloat yj = point.y;
				JFloat xj = point.x;
				cv1 += xi*xj*xj*(xi*yj-yi*xj)/(syi*syi*syj*syj);
				cv2 += (xi*xj*xj*(yi - yj))/(syi*syi*syj*syj);
				cv3 += (xi*xj*xj*(xj - xi))/(syi*syi*syj*syj);
				}
			}
		det = Sig*(X2*X4-X3*X3) + X*(X3*X2-X*X4) + X2*(X*X3-X2*X2);
		tempa = (Y*(X2*X4-X3*X3) + X*(X3*YX2-YX*X4) + X2*(YX*X3-X2*YX2))/det;
		tempb = (Sig*(YX*X4-YX2*X3) + Y*(X3*X2-X*X4) + X2*(X*YX2-YX*X2))/det;
		tempc = (Sig*cv1 + X*cv2 + Y*cv3)/det;

		for (i=1; i<=rcount; i++)
			{
			J2DDataPoint point = GetRealElement(i);
			JFloat newVal = 
				sqrt(point.yerr*point.yerr + (tempb+2*tempc*point.x)*(tempb+2*tempc*point.x)*point.xerr*point.xerr);
			if (newVal == 0)
				{
				newVal = 1;
				}
			yAdjError.SetElement(i, newVal);
			}
		}
//	itsAParameter	= tempa;
//	itsBParameter	= tempb;
//	itsCParameter	= tempc;
	itsAParameter	= parms.GetElement(1, 1);
	itsBParameter	= parms.GetElement(2, 1);
	itsCParameter	= parms.GetElement(3, 1);

	itsChi2Start = 0;
	for (i=1; i<= rcount; i++)
		{
		point = GetRealElement(i);
		JFloat yerr = yAdjError.GetElement(i);
		itsChi2Start += pow(point.y - tempa - tempb*point.x - tempc*point.x*point.x,2)/(yerr*yerr);
		}

	itsAErrParameter = 0;
	itsBErrParameter = 0;
	itsCErrParameter = 0;
}
void
JPlotFitExp::CalculateFirstPass()
{
	J2DDataPoint point;
	const JSize count	= GetRealElementCount();
	JSize rcount		= 0;
	for (JIndex i = 1; i <= count; i++)
		{
		point	= GetRealElement(i);
		if (point.y > 0)
			{
			rcount++;
			}
		}
	JMatrix odata(rcount, 2, 1.0);
	JVector yData(rcount);

	rcount	= 0;
	for (JIndex i = 1; i <= count; i++)
		{
		point	= GetRealElement(i);
		if (point.y > 0)
			{
			rcount++;
			JFloat yerr = point.yerr;
			if (yerr == 0)
				{
				yerr = 1;
				}
			else
				{
				yerr	= log((point.y - point.yerr)/point.y);
				}
			odata.SetElement(rcount, 1, 1/(yerr*yerr));
			odata.SetElement(rcount, 2, log(point.x)/(yerr*yerr));
			yData.SetElement(rcount, log(point.x)/(yerr*yerr));
			}
		}

	JMatrix tData = odata.Transpose();
	JMatrix lData = tData * odata;
	JMatrix rData = tData * yData;
	JMatrix parms(2,1);
	JGaussianElimination(lData, rData, &parms);
	JVector eparms(2);
	eparms.SetElement(1, exp(parms.GetElement(1,1)));
	eparms.SetElement(2, parms.GetElement(2,1));
	SetCurrentParameters(eparms);

	itsChi2Start = 0;
	for (JIndex i = 1; i <= count; i++)
		{
//		do
//			{
			point = GetRealElement(i);
//			}
//		while (point.y == 0);
		JFloat yerr = point.yerr;
		if (yerr == 0)
			{
			yerr = 1;
			}
		itsChi2Start += pow(point.y - FunctionN(point.x),2)/(yerr*yerr);
		}

}