function coeff=fitplot(xin,yin,n)
%function coeff=fitplot(xin,yin,n)
% fits n degree polynomial to input vectors xin, yin; fit coefficients in
% coeff.  The fit is y=coeff(1)*x^n + ... + coeff(n)*x +coeff(n+1).
% Then the polynomial is evaluated in 100 equally spaced points between the
% minimum and maximum values of the xin vector.  These values are in
% the vectors xout and yout, but are not communicated back to the main 
% program.  The points xin and yin are ploted as symbols,
% and the fit is plotted as line.
coeff=[];
xout=[];
yout=[];
coeff=polyfit(xin,yin,n);

yy=polyval(coeff,xin);
residrms=norm(yy-yin)/sqrt(length(yin));

disp('---------------------------')
disp('RMS value of residual')
disp(residrms)

ym=mean(yin);
r2=(norm(yy-ym)/norm(yin-ym))^2;
disp('R_square=')
disp(r2)
disp('---------------------------')

x1=min(xin);
x2=max(xin);
xout=[x1:(x2-x1)/99:x2];
yout=polyval(coeff,xout);
h=plot(xout,yout,'-',xin,yin,'o');
set(h,'linewidth',1.5,'markersize',10);

