define a stopper in f-solve loop

2 visualizzazioni (ultimi 30 giorni)
milad
milad il 4 Lug 2015
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!

Risposta accettata

Walter Roberson
Walter Roberson il 4 Lug 2015
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".
  2 Commenti
milad
milad il 5 Lug 2015
Hi Walter. The answer you give is so useful, but it stops the F-solve iterative process even for "equation solved, f-solve stalled". Also the first term 'While' take the problem to an infinitive loop if the exit-flag is equals to 1.
Walter Roberson
Walter Roberson il 5 Lug 2015
There is no specific exit status for "equation solved, but inaccuracy is possible". Any positive exit status other than +1 could be interpreted as falling into that category. Anyhow, you can use (for example)
if ~ismember(exitflag, [1, 3, 4])
If you prefer a "for" loop then use a "for" loop. The "break" remains the same.

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by