% TestBackslashOperator.m Feb. 2005 L Braile % Test and plot the backslash operator (\) or left divide 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]'; % Note ' makes y a column vector npts = length(x); X = [ones(npts,1) x']; % set up the least squares coefficient matrix b = X\y; % Use the \ to calculate least squares solution % 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,b(1),b(2)); % 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