function [a,b, ci]=lreg2(t,y);


% tp=(t-mean(t))
% yp=(y-mean(y))
% 
% a1=sum(tp.*yp')/sum(tp.^2)



%linear regression for dependent variable y and independent variable t
% a is intercept and b slope R is the R^2 value

if min(size(t)) > 1 | min(size(y)) > 1
    error(' This program is designed for 1 D data')
end

%make sure the data are column vectors
t=g2c(t);
y=g2c(y);
    

XX=[ones(size(t)) t];

A=XX \ y;
a=A(1,1);
b=A(2,1);

R=corrcoef(y,t); %calculate the correlation coefficient
R=R(2,1)^2 

T=(R^.5*(length(t)-2)^.5)/((1-R)^.5) ; %T statisitc

ci=tcdf(T,length(t)-2)




% figure; clf;
% plot(t,y,'r*', 'MarkerSize', 12)
% hold on; plot(t,a+b*t, 'b', 'LineWidth', 2);
% title(['Fit is significant at the ', num2str(100*ci), '% confidence interval'], 'FontSize', 17)
% set(gca, 'FontSize', 18);
