Infintite Loop- bisection search how to stop

조회 수: 3 (최근 30일)
Michael  Kurniawan
Michael Kurniawan 2012년 10월 4일
function rootx = bisectionSearch (fhandle,a,b,epsilon)
while (b-a) > epsilon %assume a<b
m =(b+a)/2
fhandlem=fhandle(m);
if fhandlem == 0
rootx = m %If f(m) is equal to zero then return the root as m
else if sign (fhandle(m))== sign (fhandle(a)) %
a=m; % If f(m) has the same sign as f(a) Replace a with m
else
m=b;
end
end
end
m;
end
Hi guys this is an infinte loop and I do not know why or how to fix it Please help if possible. when I test it it keeps giving me m values repeatly of 1.75 and I dont think thats even the right answer. please Help!
  댓글 수: 2
Matt J
Matt J 2012년 10월 4일
It gives you m=1.75 repeatedly with what input arguments?
Michael  Kurniawan
Michael Kurniawan 2012년 10월 4일
[x]=bisectionSearch(f,1,2,0.01) f= @(x)sin(x)-log(x+1)

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

채택된 답변

Matt J
Matt J 2012년 10월 4일
편집: Matt J 2012년 10월 4일
In the case fhandlem==0, you should BREAK to terminate the loop. Also, you need b=m instead of m=b.
Finally, you could use an ELSEIF structure instead of ELSE...IF.
  댓글 수: 7
Michael  Kurniawan
Michael Kurniawan 2012년 10월 4일
Thank you!!
Matt J
Matt J 2012년 10월 4일
Move rootx=m to the last line of the function.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by