title: Fitting a line to 2d points in MATLAB
body: <p>I try to calculate a line that can fit given several points with 2-d coordinate in MATLAB. But the  result is not I expected. There may be something I understand wrong. Can anyone help me out? Thanks a lot. The code is as follows:</p>
  
  <pre><code>ptsAroundVCP_L=[180,188;177,191;174,191;171,191;168,188;] % points with 2-d coordinate 
  curLinePar_L=polyfit(ptsAroundVCP_L(:,2),ptsAroundVCP_L(:,1),1); % parameter of the fitted line
  
  %% begin to plot
  plotx=1:256;    
  figure(11);hold on;
  plot(ptsAroundVCP_L(:,2),ptsAroundVCP_L(:,1),'sb');    
  ploty_L=polyval(curLinePar_L,plotx);
  plot(plotx,ploty_L,'r');
  hold off;
  </code></pre>
  
  <p>The output is shown as follows. But what I expected is that the fitted line should go vertically in this case. I think there is something wrong with the line fitting. 
  <img src="https://i.stack.imgur.com/Dqqd7.jpg" alt="enter image description here"></p>