%Darren Pais %fit to N degree polynomial for data %outdata is the fitted result for 100 x values (change spacing to change this) %indata is the input data matrix with pair-wise xy data in seperate rows %vector a contains the polynomial coefficients function [outdata]=fitNdeg(indata,N) for j=N:-1:0 Z(:,N-j+1)=indata(:,1).^j ; end V=inv(Z'*Z)*Z'*indata(:,2) ; a=zeros(N,1) ; for j=1:1:N+1 a(j,1)=V(j,1); end %%%a=V(1,1); b=V(2,1); c=V(3,1); spacing = 100 ; outdata(:,1)=min(indata(:,1)):(max(indata(:,1))-min(indata(:,1)) )/spacing :max(indata(:,1)) ; dataout=0.*outdata(:,1) ; for j=1:1:N+1 dataout=dataout+a(j,1).*outdata(:,1).^(N-j+1); end outdata(:,2)=dataout ; %%+ b.*outdata(:,1).^1 + c.*outdata(:,1).^0 ;