# How do I find out what specifically is causing the error "Matrix is close to singular or badly scaled" in the sgolay function?

1 view (last 30 days)
Daniel Pollard on 7 Jan 2021
Commented: Image Analyst on 7 Jan 2021
I'm using the sgolay function from the Signal Processing toolbox to calculate some derivatives. The function is inside a loop as I feed it different parameters;
[~,g] = sgolay(poly_order, window_length).
However, it seems like for all or almost all of the iterations, I'm getting the warning
Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 3.820809e-39.
> In sgolay (line 114)
where RCOND is not quite the same value each time. Line 114 in the function is
G = Q/R';.
I have a basic understanding of the error; the / command computes an inverse of a matrix, and the matrix cannot easily be inverted. However, I don't know what I can do differently to remove this warning. Can anyone help?
Daniel Pollard on 7 Jan 2021
If anyone sees this, I have discovered the command
warnings off;
which basically does what I wanted, but obviously that doesn't exactly solve the problem.

Image Analyst on 7 Jan 2021
What are the poly_order and window_length you're using? I can see that if window_length is less than poly_order + 1, then you would have a problem. For example, you can't fit a parabola (order 2) with only 1 or 2 points. You'd need at least 3 and preferably more.
Daniel Pollard on 7 Jan 2021
It iterates over different values of those variables. For example, most recently I ran it with poly_order = [2, 3, 4, ..., 30, 31], and window_length = 51. You have to use a window length greater than the polynomial order or it throws an error and stops running.
Image Analyst on 7 Jan 2021
Yes, like I said.
But the other problem is that if you're trying to fit a 31 order polynomial -- have you ever seen what happens when the polynomial order gets real high? You get all kinds of humps and instabilities between training points. I have no doubt that a polynomial like x^31 would cause problems. You shouldn't go more than about 3 or 4.
Attach your data if you need more help.

### Categories

Find more on Correlation and Convolution in Help Center and File Exchange

R2020b

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!