%% linfit2.m fits a 1st order polynomial to the inputted x and y data 
%% vectors with known error in y. It determines the required 
%% co-efficents, as well as estimating the absolute error in the 
%% coefficents. All equations are from p256, Chap 15 of Numerical
%% Recipes (couldn't get ones from Sze Tan's notes to work(?))
%% 

function [c1,c0,c1e,c0e]=linfit2(xv,yv,yve)

% xv = data x-vector 
% yv = data y-vector
% yve = error in data y-vector
%
% c1 = 1st order coeffient of fit
% c0 = 0th order coeffient of fit
% c1e = error in 1st order coeffient
% c0e = error in 0th order coeffient

%convert to column vectors
xv=xv(:);
yv=yv(:);
yve=yve(:);

s=sum(1./(yve.^2));
sx=sum(xv./(yve.^2));
sy=sum(yv./(yve.^2));
sxx=sum((xv.^2)./(yve.^2));
sxy=sum((xv.*yv)./(yve.^2));

del=s*sxx-sx^2;
c0=(sxx*sy-sx*sxy)/del;
c1=(s*sxy-sx*sy)/del;

c0e=sqrt(sxx/del);
c1e=sqrt(s/del);
