function nthpoly(n,datafile) % computes the fit parameter to nth polynomial % using least square method i=1:n; data=load(datafile); X=data(:,1); Y=data(:,2); EY=data(:,3); np=length(X); A=zeros(np,n+1); for j=0:n A(:,j+1)=X .^ j; end V=diag(EY .^ 2); covariance=inv(A'*inv(V)*A); params=covariance*(A'*inv(V))*Y; disp(''); fprintf("parameters:\n"); disp(params); disp(''); fprintf("covariance matrix:\n"); disp(covariance); sigma2=diag(covariance)'; sigma= sqrt(sigma2); sigij=sigma'*sigma; disp(''); correlation= covariance ./ sigij; disp('correlation:'); disp(correlation); end