Kshitij Singh answered .
2025-07-10 21:18:05
This is a weighted least-squares problem. The requirement for the weights to be positive is needed in order to ensure the "least-squares matrix" is positive definite and the system has a solution.
For example, if you change the error checking as suggested above, to allow for zero weights, and try this simple case:
[b,g]=sgolay(3,5,[1,0,0,0,1]);
you will run into matrix singularity issues as indicated by the following warning messages:
Warning: Matrix is singular to working precision.
(Type "warning off MATLAB:singularMatrix" to suppress this warning.)
> In I:\1186207\sgolay.m at line 59
Warning: Matrix is singular to working precision.
(Type "warning off MATLAB:singularMatrix" to suppress this warning.)
> In I:\1186207\sgolay.m at line 59
This is the reason we have this requirement.
You can make some of the weights "very small" if you want to achieve a similar effect as is required here. But it is important to keep in mind that the smaller the weight, the more ill-conditioned the problem will become.
This is due to the so-called inertia theorem for matrices; if
A = C'*D*C
then A will have the same number of positive eigenvalues as D, the same number of negative eigenvalues, and the same number of zero eigenvalues.
If D (in this case a diagonal matrix with the weights on the diagonal) has a zero eigenvalue (a weight equal to zero in this case), then A will be positive semidefinite (assuming no negative weights) and a solution to the least-squares problem is not guaranteed.
Not satisfied with the answer ?? ASK NOW