필터 지우기
필터 지우기

What's wrong with my bisection method function???

조회 수: 1 (최근 30일)
Joe
Joe 2013년 4월 11일
I'm not quite sure what's exactly wrong with my bisection method function that I have written. Can someone please help me figure out what the error is?
function root = bisectIter(f,a,b,tol)
if sign(f(a))==sign(f(b))
error('a and b do not bracket the root');
end
k = 1;
x(k) = (a+b)/2;
while ((k<=tol)&&((b-a)/2)>= tol)
if f(x(k)) == 0
error('bisection condition didnt apply')
end
if (f(x(k))*f(a))<0
b = x(k);
else
a = x(k);
end
k = k + 1;
x(k) = (a+b)/2;
root = x(k);
end
end
  댓글 수: 2
Matt J
Matt J 2013년 4월 11일
편집: Matt J 2013년 4월 11일
Show us what error you're getting and how to reproduce it.
Joe
Joe 2013년 4월 12일
The error "Output argument 'root' not assigned during call..." shows up. I've already checked for the possible causes of this error and none seem to fit. When I alter my code so that it should produce an error when I run it, it does in fact produce that error instead of the output argument error.

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

답변 (1개)

Roger Stafford
Roger Stafford 2013년 4월 12일
In the 'while' condition "((k<=tol)&&((b-a)/2)>= tol)" presumably you have set 'tol' to some very small number to allow a and b to approach each other closely. That means the "k<=tol" part will fail at the very beginning and you will never enter the while-loop. That is why the 'root' argument is never assigned.
In my opinion your 'while' criterion should be based on how close to zero the f function gets instead of the width b-a or the number of trips through the loop.
Also the "f(x(k)) == 0" condition that produces an error message is rather self-defeating. This is the very condition you are striving for, namely to find a root.

카테고리

Help CenterFile Exchange에서 Shifting and Sorting Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by