title: Orthogonal distance regression in MATLAB
body: <p>Given two random variables/measurements (<i>x</i>, <i>y</i>), both measured with error (error-in-variables case),<br/> is there a routine in MATLAB to calculate the estimators (<i>a</i>, <i>b</i>) of a regression line <i>y</i>(<i>i</i>)=<i>a</i>·<i>x</i>(<i>i</i>)+<i>b</i> using the method of orthogonal distance regression?</p>
  
  <p>Here's my implementation of Maximum Likelihood estimator:</p>
  
  <pre><code>x= [1.0, 0.6, 1.2, 1.4, 0.2];
  y=[0.5, 0.3, 0.7, 1.0, 0.2];
  
  mx = mean(x);
  my = mean(y);
  p = (x(:) - mx) .^ 2;
  q = (y(:) - mx) .^ 2;
  w = p .* q;
  sxx = sum(p);
  syy = sum(q);
  sxy = sum(w);    w=p.*q;   sxy=sum(w);
  
  l = 1;  %# orthogonal distance regression
  a = (syy - l * syy + sqrt((syy - l * sxx) ^ 2 + 4 * l * sxy^2)) / (2 * sxy);
  b = my - a * mx;
  </code></pre>
  
  <hr>
  
  <p><strong>EDIT</strong> (addressed to EitanT):<br/></p>
  
  <p>Here's a comparison of my estimators and yours:</p>
  
  <p><img src="https://i.stack.imgur.com/KKNBZ.jpg" alt="comparison"></p>