%% pwpolfit.m conducts a n'th order least squares weighted fit to the
%% inputted data set. The weighting is assumed to 1/err^2 where err is
%% is the inputted error of the i'th point. The output is a single
%% vector containing the coefficients of the fit.
%%

function c=pwpolfit(xv,yv,yve,n)

% xv = x vector of data to be fitted (vector)
% yv = y vector of data to be fitted (vector)
% yve = error in y vector of data to be fitted (vector)
% n = polynominal order of fit required (scalar)
% c = coefficients of fit

wt=1./(yve.^2);
c0=1./10.^(n:-1:0);
options=optimset('Display','off');
c=fminsearch('pwpolmin',c0,options,xv,yv,wt);

