MATLAB Answers

0

Return if No Solution

Asked by Allison Bushman on 13 Sep 2019 at 20:37
Latest activity Commented on by Walter Roberson
on 13 Sep 2019 at 21:24
I am asked to solve the equation: Ax = B for two randomly generated matrices. Furthermore, I am instructed that the script should return if no solution is found.
Here is my code:
n=100;
A=randn(n);
B=randn(n);
x = A\B;
I believe I know how to solve the equation, but don't know how to return if no solution is found. Is there a different way that I should be writing this? If not, how to I return if no solution is found?

  1 Comment

the cyclist
on 13 Sep 2019 at 20:49
Can you be more specific as to what you mean by "no solution"?
Do you mean if
x = A\B;
returns an error message, or something else?

Sign in to comment.

2 Answers

Walter Roberson
Answer by Walter Roberson
on 13 Sep 2019 at 20:55
 Accepted Answer

You can test rank(A) . If it is < n then no solution is possible.
However, there are full rank matrices for which the matrix is too ill-conditioned for an inverse to be calculated. You can example cond() or rcond() and make a decision as to whether the matrix is too badly conditioned to proceed on.

  0 Comments

Sign in to comment.


Answer by David Hill on 13 Sep 2019 at 21:05

If A will always be a square matrix, then if the det(A)==0 then return (no solution).

  1 Comment

Walter Roberson
on 13 Sep 2019 at 21:24
det()==0 is not as good a test as rank() because det() is more prone to round-off error.

Sign in to comment.