Least Squares Line Fit with Outlier Rejection

Easy MATLAB Math Asked at Bosch
Solve this problem

Fit a straight line to noisy measurements and iteratively discard outliers more than three standard deviations from the fit.

Constraints

  • Function: [slope, offset, kept] = robust_fit(x, y).
  • Iterate until no points are removed or 10 iterations pass.
  • Reject points beyond 3 sigma of the residuals.
  • Must not remove so many points that fewer than 2 remain.
  • Return the indices of the kept points.
  • Implement the fit directly, not with polyfit.
Starter codeMATLAB
1function [slope, offset, kept] = robust_fit(x, y)2% TODO3end