How to suppress this error?

31 visualizzazioni (ultimi 30 giorni)
Jakob Sievers
Jakob Sievers il 25 Set 2020
Risposto: Real User il 21 Mar 2024
Hi there
I am receiving the following error message:
Warning: Matrix is singular, close to singular or badly scaled. Results may be inaccurate. RCOND = NaN.
> In mpower>integerMpower (line 80)
In ^ (line 49)
In gaussfit (line 111)
The line in question is:
a = (F2)^(-1)*F'*(y-f0) + a0;
I wish to suppress, not solve, this problem and so I added the following to the beginning of my script, and yet still receive the error message. What am I doing wrong?
warning('off','MATLAB:singularMatrix')
  4 Commenti
Jakob Sievers
Jakob Sievers il 25 Set 2020
This is not a script I originally wrote so I couldn't say. It just worked for my purpose. Though if you have a better way, not to mention a faster way, of doing it I am all ears. My process involves using this equation quite often and would love to shave some time off of it if possible.
Jakob Sievers
Jakob Sievers il 25 Set 2020
I just had a look and the file (which fits a gaussian curve) is from 2012, so it might indeed be overdue for an update:
function [sigma, mu,error] = gaussfit( x, y, sigma0, mu0,runmode,idix)
% [sigma, mu] = gaussfit( x, y, sigma0, mu0 )
% Fits a guassian probability density function into (x,y) points using iterative
% LMS method. Gaussian p.d.f is given by:
% y = 1/(sqrt(2*pi)*sigma)*exp( -(x - mu)^2 / (2*sigma^2))
% The results are much better than minimazing logarithmic residuals
%
% INPUT:
% sigma0 - initial value of sigma (optional)
% mu0 - initial value of mean (optional)
%
% OUTPUT:
% sigma - optimal value of standard deviation
% mu - optimal value of mean
%
% REMARKS:
% The function does not always converge in which case try to use initial
% values sigma0, mu0. Check also if the data is properly scaled, i.e. p.d.f
% should approx. sum up to 1
%
% VERSION: 23.02.2012
%
% EXAMPLE USAGE:
% x = -10:1:10;
% s = 2;
% m = 3;
% y = 1/(sqrt(2*pi)* s ) * exp( - (x-m).^2 / (2*s^2)) + 0.02*randn( 1, 21 );
% [sigma,mu] = gaussfit( x, y )
% xp = -10:0.1:10;
% yp = 1/(sqrt(2*pi)* sigma ) * exp( - (xp-mu).^2 / (2*sigma^2));
% plot( x, y, 'o', xp, yp, '-' );
warning off;
warning('off','MATLAB:singularMatrix')
error=0; %no error
% Maximum number of iterations
Nmax = 50;
if nargin==4
runmode=1; %output warnings!
end
if( length( x ) ~= length( y ))
fprintf( 'x and y should be of equal length\n\r' );
exit;
end
n = length( x );
x = reshape( x, n, 1 );
y = reshape( y, n, 1 );
%sort according to x
X = [x,y];
X = sortrows( X );
x = X(:,1);
y = X(:,2);
%Checking if the data is normalized
dx = diff( x );
dy = 0.5*(y(1:length(y)-1) + y(2:length(y)));
s = sum( dx .* dy );
if( s > 1.5 | s < 0.5 )
fprintf( 'Data is not normalized! The pdf sums to: %f. Normalizing...\n\r', s );
error=1;
y = y ./ s;
end
X = zeros( n, 3 );
X(:,1) = 1;
X(:,2) = x;
X(:,3) = (x.*x);
% try to estimate mean mu from the location of the maximum
[ymax,index]=max(y);
mu = x(index);
% estimate sigma
sigma = 1/(sqrt(2*pi)*ymax);
if( nargin == 3 )
sigma = sigma0;
end
if( nargin == 4 )
mu = mu0;
end
%xp = linspace( min(x), max(x) );
% iterations
ii=0;
while ii<Nmax
ii=ii+1;
% yp = 1/(sqrt(2*pi)*sigma) * exp( -(xp - mu).^2 / (2*sigma^2));
% plot( x, y, 'o', xp, yp, '-' );
dfdsigma = -1/(sqrt(2*pi)*sigma^2)*exp(-((x-mu).^2) / (2*sigma^2));
dfdsigma = dfdsigma + 1/(sqrt(2*pi)*sigma).*exp(-((x-mu).^2) / (2*sigma^2)).*((x-mu).^2/sigma^3);
dfdmu = 1/(sqrt(2*pi)*sigma)*exp(-((x-mu).^2)/(2*sigma^2)).*(x-mu)/(sigma^2);
F = [ dfdsigma dfdmu ];
a0 = [sigma;mu];
f0 = 1/(sqrt(2*pi)*sigma).*exp( -(x-mu).^2 /(2*sigma^2));
if any(isnan(F(:)))
error=1;
ii=Nmax;
else
F2=F'*F;
if rank(F2)<min(size(F))
error=1;
ii=Nmax;
else
a = (F2)^(-1)*F'*(y-f0) + a0;
sigma = a(1);
mu = a(2);
if( sigma < 0 )
sigma = abs( sigma );
if runmode==1
fprintf( 'Instability detected! Rerun with initial values sigma0 and mu0! \r' );
fprintf( 'Check if your data is properly scaled! p.d.f should approx. sum up to 1 \r' );
end
error=1;
ii=Nmax;
end
end
end
end

Accedi per commentare.

Risposte (2)

the cyclist
the cyclist il 25 Set 2020
Two thoughts:
First: Are you certain you have the correct warning? After you run your code that gives the warning, what is the output of
w = warning('query','last')
(Your warning does look like it matches the one you are turning off, so I'm guessing that's not the issue.)
Second: I believe that turning off warnings only persists for the session. Does this warning happen during the same session in which you turned it off?
  1 Commento
Jakob Sievers
Jakob Sievers il 25 Set 2020
The above solution worked for my needs. Thanks for your attention though :)

Accedi per commentare.


Real User
Real User il 21 Mar 2024
warning('off','MATLAB:nearlySingularMatrix')
But the cyclist's answer works for any error (that's how I found this).

Categorie

Scopri di più su Mathematics and Optimization in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by