function [QR] = quadFitT(a, varargin)
%usage Qcf= quadFit(a)
%returns a matirx wholse rows are coeffs of a quadratic fit to pcurves
%where the 1st column  the squared term, the second the linear and the
%third the y-intercept
%pi coeffs: Qcf(i,:)
%sqaurecoefs: Qcf(:,1)
%linear coeffs: Qcf(:,2)
%Y-intercepts: Qcf(:,3)
%%LAST EDITTED on NOV162007, now can input an argument 33 which defines t0,
%%whihc defies how much of the early time curve you wanna fit

if length(varargin)
    numT=varargin{1};
end;
%we also want to get a residual measure for each curve
%this will help us know hwen things are super low etc...
t0=length(a.t)-numT+1;
size(a);
Qcf=nan(3);
G=ones(numT,3);
lt=log10(a.t(t0:end))';
G(:,1)=lt.^2;
G(:,2)=lt;
beast=inv(G'*G)*G';
Qcf(1,:)=beast*log10(abs(a.p1(t0:end)))';
Qcf(2,:)=beast*log10(abs(a.p2(t0:end)))';
Qcf(3,:)=beast*log10(abs(a.p3(t0:end)))';

%QuadFit Timeseries', %P1Q stands for P1:Quadratic Fit
Pqf(:,1)=Qcf(1,1)*G(:,1)+Qcf(1,2)*G(:,2)+Qcf(1,3)*ones(numT,1);
Pqf(:,2)=Qcf(2,1)*G(:,1)+Qcf(2,2)*G(:,2)+Qcf(2,3)*ones(numT,1);
Pqf(:,3)=Qcf(3,1)*G(:,1)+Qcf(3,2)*G(:,2)+Qcf(3,3)*ones(numT,1);
%P(:)
%CALCULATE RESIDUAL VECTORS
R=nan(3,numT);
R(1,:)=(Qcf(1,1)*G(:,1)+Qcf(1,2)*G(:,2)+Qcf(1,3)*ones(numT,1))-(log10(abs(a.p1(t0:end)))');
R(2,:)=(Qcf(2,1)*G(:,1)+Qcf(2,2)*G(:,2)+Qcf(2,3)*ones(numT,1))-(log10(abs(a.p1(t0:end)))');
R(3,:)=(Qcf(3,1)*G(:,1)+Qcf(3,2)*G(:,2)+Qcf(3,3)*ones(numT,1))-(log10(abs(a.p1(t0:end)))');
%%%%

%CALCULATE c
%P2-P3=
%P
d23=Pqf(:,2)-Pqf(:,3);
d12=Pqf(:,1)-Pqf(:,2);

S=dot(d23,ones(1,numT))/sqrt(numT); %S is the shift needed to best fit P2, P3




QR.Qcf=Qcf;%quad curve fit coefficients
QR.Res=R;   % residuals to curve fits
QR.Pqf=Pqf; %approximations ot curve fits
QR.sym=S;   %symmetery number
QR.d23=d23;
QR.d12=d12;

