简体   繁体   中英

Detecting Matlab failure to solve sparse linear system

I want to programmatically find out when A\\b failed (for sparse A) so that I can run some problem-specific logic. Using the backslash operator

A\b

I get warnings printed to the console but I want to know about these conditions (singular or nearly singular) programmatically so I can do some problem-specific stuff.

For dense systems, I can do

[soln, cond_recip] = linsolve(A,b);
if cond_recip < 1e-15, ..., end

But linsolve does not work for sparse matrices and I do not want to densify my matrices.

Try the following:

%# temporarily set warning to issue errors (maybe there are others?)
s = warning('error', 'MATLAB:nearlySingularMatrix'); %#ok<CTPCT>

try
    x = magic(4)\[34; 34; 34; 34];
catch ME
    disp(ME.message)
    %#.. problem specific stuff..
end

%# restore warning state
warning(s);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM