How can I solve the max iterations exceeded?

9 ビュー (過去 30 日間)
fs
fs 2017 年 1 月 24 日
回答済み: fs 2017 年 1 月 24 日
I'm trying to solve root finding problem by using Newton method when I run the code I got this message. How can I avoid this message? Is there a problem with my code?
max_iter=10; err=1; tol=10^-6; n_iter=0;
while err>tol
n_iter=n_iter+1;
delta=-F(r)/dF(r);
r=r+delta;
err=abs(delta/r);
if n_iter>max_iter
warning('max iterations exceeded')
break
end
end
end
%%%%%%%%%%%%%%%%%%%%%
function out=F(r)
K=9*10^9; e= 1.6*10^-19; p=0.33*10^-10; Alpha=1.74637*10^-16;
out=-K*(e^2./r)+ Alpha*exp(-r./p);
end
%%%%%%%%%%%%%%
function out=dF(r)
K=9*10^9; e= 1.6*10^-19; p=0.33*10^-10; Alpha=1.74637*10^-16;
out=K*(e^2./r^2)- (Alpha/p)*exp(-r./p);
end
  2 件のコメント
James Tursa
James Tursa 2017 年 1 月 24 日
Probably a problem with your code (e.g., recursion without proper convergence criteria, etc), but impossible to say without seeing your code. Can you post it?
John Chilleri
John Chilleri 2017 年 1 月 24 日
Providing your code would be useful, but without it, I can only suggest that you look into changing the MaxIter option, as the default may be too low for you.

サインインしてコメントする。

採用された回答

James Tursa
James Tursa 2017 年 1 月 24 日
編集済み: James Tursa 2017 年 1 月 24 日
Had to hunt for awhile to find a place where the function switches signs, but try this for a starting point:
r = 0.1e-9

その他の回答 (2 件)

Steven Lord
Steven Lord 2017 年 1 月 24 日
It's impossible to say for certain without seeing a sample of your code, but more likely than not you're accidentally doing something like this (question 3.18 in this FAQ, if that link doesn't work.) If you call a function from within itself, you need to ensure that you have a base case that does not call the function itself, to end the recursion.

fs
fs 2017 年 1 月 24 日
Thank you very much. As I go smaller and smaller for r, I got more n_iter. Thank you again.

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

タグ

タグが未入力です。

Community Treasure Hunt

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

Start Hunting!

Translated by