stopping a function after 30 iterations
2 ビュー (過去 30 日間)
古いコメントを表示
I have been working all day on this coding, i can not figure out how to stop it after 30 iterations
function root %finds the root of the equation F0fX(x) using newton-raphson method
clear all;
clc;
x=.8;
x=newton(x);
fprintf('root found at x= %10.8f\n',x);
function [y]=FofX(x)
%nonlinear function
y= x^2+1;
function [y]=derofF(x)
%derivative of the function
y=2*x;
function [x]=newton(x)
%code for finding with newton-raphson method
TOL=5e-09;
error=10*TOL;
%Top of the loop
iterations=0;
while(error>TOL)
delta=-FofX(x)/derofF(x);
x=x+delta;
iterations=iterations+1;
%check for the relative error condition:
%If too big, loop again;if small enough, end.
error=abs(delta/x);
end
fprintf('after%4.0f iterations\n',iterations)
thanks for any help
2 件のコメント
採用された回答
その他の回答 (1 件)
Hossein
2012 年 4 月 21 日
Hi, The function that you are using doesn't have any real root. If you change the function to x^2-1 it works perfectly fine.
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!