% wlin.m takes on the general weighted linear least squares fitting problem. % The basic situation is that A * b = where A is a (N*m) set of m fitting % curves, b is a m vector of fitting weights for the respective curves and C % is a N vector of variables with white noise. The white noise on C is such % that even though we don't know the absolute strength, we can take the % variance of each to be given by var(C(j)) = phi / w(j). % wlin finds the best values of b, and their uncertainties. function [b, std_b] = wlin(A,C,w) [N,m] = size(A); % Figure out the size of the data % Figure out the D and E matrices D = zeros(m,m) ; for i=1:m for j=i:m for k=1:N D(i,j) = D(i,j)+w(k)*A(k,i)*A(k,j); end D(j,i) = D(i,j); end end E = A*inv(D); % Find b b = zeros(m,1); for i =1:m for j=1:N b(i) = b(i) + E(j,i)*w(j)*C(j); end end %Find phi F = C- A*b ; epsilon = 0.0 ; for i=1:N epsilon = epsilon + F(i)*F(i)*w(i); end phi = epsilon / (N-m); % Find the errors in b std_b = zeros(m,1); for i=1:m for j=1:N std_b(i) = std_b(i) + E(j,i)^2.0 *w(j) ; end std_b(i) = (std_b(i) * phi)^0.5 ; end