MATLAB: Define a stopper in f-solve loop

"if" for "f-solve"

hello my dear friends I want to solve a nonlinear equation by F-solve, in which a variable will changed in each loop, but I want to stop the iterative process when the answer of F-solve will be " equation solved, but inaccuracy is possible" or " no solution found", and print the last solution. what should I do exactly? could you please help me Thank you!

Best Answer

while true
[x, fval, exitflag] = fsolve(....);
if exitflag ~= 1
break;
end
end
any of the exit statuses other than +1 could be interpreted as inaccuracy. That can include that the search was going well but that it was taking more iterations than you configured to allow, and can include that the search noticed that round-off error is interfering with pinning down the solution to within the tolerance you requested. Generally speaking, negative exit statuses are the "hard" exits indicating that there is no solution to be found in the area, and positive exits other than +1 indicate that you are running into difficulty with numeric accuracy but that there is likely a solution "nearby".