필터 지우기
필터 지우기

Solution is going to infinite iterations

조회 수: 1 (최근 30일)
Shripad
Shripad 2017년 8월 4일
댓글: Torsten 2017년 8월 7일
I am getting infinite iterations, please help me.
clear all
a=0.5;
err=1e-6;
count=0;
x=100;
%fx = a-cos(a);
%dfx= 1+sin(a);
%x1=a-(fx/dfx);
while abs(a-x)>err
fx=x-cos(x);
dfx= 1+sin(x);
x1=x-(fx/dfx);
%x=abs(x1-x);
%x=x1;
if abs(x1-a)>err
x=x1;
%elseif abs(a-x1)>err
% continue
end
count = count + 1;
sprintf('After iteration %2.0d the x is %3.15f',count,x)
end

답변 (1개)

Torsten
Torsten 2017년 8월 4일
편집: Torsten 2017년 8월 4일
Your decision variable in the while statement must be
abs(fx)> err
and/or
abs(x1-x)>err
but not
abs(x1-a)>err
Note that 0.5 is not a root of f(x)=x-cos(x).
Best wishes
Torsten.
  댓글 수: 2
Shripad
Shripad 2017년 8월 4일
Thanks for the help Torsten, your first suggestion (abs(fx)> err ) worked for me. But with your second suggestion (abs(x1-x)>err),which I thought to be more logical, MATLAB was performing only one iteration. Kindly help me to resolve this issue.
Thanks again.
Torsten
Torsten 2017년 8월 7일
After the commands in the while-loop have been executed, MATLAB checks the exit condition and exits if they are no longer true. Since you set x=x1 at the end of the while-loop, the condition to exit (abs(x-x1)<=err) will be satisfied after the first iteration. The remedy is to set x=x1 at the start of the while-loop.
Best wishes
Torsten.

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

카테고리

Help CenterFile Exchange에서 Graphics Object Identification에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by