MATLAB: Poly1 fit error “Warning: Equation is badly conditioned. Removed repeated data points or try centering and scaling.”

curve fittingpoly1weighted least squares

I am running into an error when I try to perform a weighted poly 1 fit. The weights I am using are the inverse of the errors on the data points squared (1/error^2). The commands I am using are:
options=fitoptions('Weights',1/(toterr(good)).^2);
[p,goodness_linear] = fit(opttime(good),x(good),'poly1',options);
I end up getting the error that is quoted in the question title. I originally, incorrectly, had the weights set as
options=fitoptions('Weights',(toterr(good)));
That did not throw an error. I should say that all of the errors on the data points have the same magnitude of 0.8492 (i.e. all the values in toterr are 0.8492), which makes me wonder if I even really need to do a weighted fit. But still, if I could get the fit to work with the weights of 1/(toterr^2) that would be ideal.

Best Answer

"if I could get the fit to work with the weights of 1/(toterr^2)..." Why? The reason for using weighted regression is generally to reduce influence of points of known less value; in your case by the above that's not so.
As for the warning, what's happening is when applying weights you've caused the X'X matrix to become nearly colinear. You can try the normalization or if there are a large number of identical (or nearly so) points, remove a significant fraction of them and see if it helps the conditioning.
Just to see, try
cond(X'X)
where
X=[ones(size(x)) x];
and x is the (column) vector of your independent variable.
ADDENDUM Of course, do the above with/without weighting to see the difference in condition numbers caused by the weights and with/without normalization and any duplicated values removal...