% Linearer fit unter berücksichtigung des Y- Fehlers
% also mit Gewichtung

function [a,b,da,db,chi2] = linfit(x,y,dy);

if numel(dy)==1
    dy = ones(size(y))*dy;
end

g=1./(dy.^2);
alpha11=sum(g);
alpha12=sum(g.*x);
alpha22=sum(g.*(x.^2));

beta1=sum(g.*y);
beta2=sum(g.*y.*x);
D=alpha11*alpha22-alpha12^2;
	
a=(beta1*alpha22 - beta2*alpha12)/D;
b=(beta2*alpha11 - beta1*alpha12)/D;
chi2=sum(g .* (y-a-b*x).^2);	
	
db=sqrt(alpha11/D);
da=sqrt(alpha22/D);
