Matlab terminate code if warning occurs

386 Views Asked by At

I am running a code where by I want to maximise a parameter (basically the range of integration) by increasing it in steps until a warning occurs. It involves inverting a linear system Ax=b and I want the script to stop if a warning like the following occurs:

Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 1.530374e-16.

Does anyone know of a method to do this?

2

There are 2 best solutions below

0
On

$RCOND$ is the reciprocal of conditional number. Smaller it is, closer to singlular the matrix is. Perhaps the numbers of your matrix is too small, you can try to change the initial conditions or enlarge the values by a certain proportion. If you need the inverse of a matrix, you can use pinv().

0
On

You can use something like this (clean the warning and throwing an error when the warning occurs):

n = 20;
A = rand(n);
A = A^10;

lastwarn('');
x = A \ ones(n,1);
if (~isempty(lastwarn))
    error(lastwarn);
end