0
votes

I have to solve a system equation in my matlab code.

At first, I try to solve it by using symbolic calculus by defining the unknown variables as

syms x1 x2 x3 'real'

and by using the function solve

[sx1 sx2 sx3] = solve(f1 == 0, f2 == 0, f3 == 0);

where f1, f2, f3 are function of x1, x2, x3.

I know that sometimes the solution can be found, but there are cases for which solutions do not exist and I get

Warning: Explicit solution could not be found.

In this case I would like to somehow "catch" this warning (without print nothing on the screen!) and run other code.

How can achieve this?

Thanks in advance

1
Did you try wrapping a try, catch block around the solve operation?Myles Baker
You should also try turning on warning on backtrace, which prints the stack trace upon the exception. You can determine the level where the error is thrown and intercept it. Outside of that, you may just need to suppress the warnings.Myles Baker
@MylesBaker I tried, it does not workthe_candyman

1 Answers

1
votes

If you want to check to see if a solution could not be found, you could check to see if sx1, sx2, sx3 are empty - if no solution is found, they won't be assigned, and so will be empty.

[sx1 sx2 sx3] = solve(f1 == 0, f2 == 0, f3 == 0);
if isempty(sx1)
    % DO OTHER STUFF
end

If you want to prevent the warning from being shown, you can turn it off using:

warning off symbolic:solve:warnmsg3

Ideally you should turn it on again as soon as you're done, to prevent possible confusion down the track.