필터 지우기
필터 지우기

stopping a function after 30 iterations

조회 수: 1 (최근 30일)
David bondi
David bondi 2012년 4월 20일
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
Walter Roberson
Walter Roberson 2012년 4월 20일
What problems do you observe for the other functions?
David bondi
David bondi 2012년 4월 20일
the program just runs and will not stop unless i stop it and it errors in the delta=-FofX(x)/derofF(x) and x=newton(x)

댓글을 달려면 로그인하십시오.

채택된 답변

David bondi
David bondi 2012년 4월 21일
adding this to the correct line fixed the issue while(error>TOL&& (iteration<=30)
thank you

추가 답변 (1개)

Hossein
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.
  댓글 수: 1
David bondi
David bondi 2012년 4월 21일
Thank you, However the equation i have to use is the x^2-1, the point of the exercise is to stop the program after 30 iterations

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Newton-Raphson Method에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by