% Testpolyfit.m Feb. 2005 L Braile % Test and plot the polyfit m-file for calculating the % least squares straight line function with sample data x = [1 2 3 4 5 6 7 8 9]; y = [1.5 1.8 2.0 3.6 4.1 6.8 6.4 4.0 9.4]; nord = 1; % order of the polynomial, in this case = 1 (straight line) [P,S] = polyfit(x,y,nord); b0 = P(2); b1 = P(1); % Calculate and print % least squares line and statistics. % plot the data and least squares line plot(x,y,'ro','markersize',12,'linewidth',3) % Plot the data set(gca,'fontsize',16,'linewidth',2) hold on xlabel('x-values','fontsize',16) ylabel('y-values','fontsize',16) title('Plot of sample data and least squares straight line fit','fontsize',16) yt = feval('sline',x,b0,b1); % Calculate the fitted line plot(x,yt,'k-','linewidth',3) % Plot the fitted line axis([0 10 0 10]); % Set axis limits for plot hold off