bisection method disadvantage?

So, I wrote code for a function to find its roots using the bisection method. I used initial guesses x=4 and 6, knowing that there are no zeros between this interval. When I use the bisection method with the following while loop condition:
while iteration<=30 || abs(xb-xa)> 10^-4
Matlab will spit out that the root in this interval = '6'. What is it about this function/graph interval (from 4 to 6) that makes the bisection method gives me a false zero?
Does it have to do with the graph looking like a straight, horizontal line in this interval?

5 Comments

We would need to see your code, not just the one line you show.
Jan
Jan on 26 Feb 2018
Edited: Jan on 26 Feb 2018
Your initial interval does not contain a zero? Then it should not work.
It is very unlikely that abs(xb-xa)== 10^-4 is matched exactly. I guess you mean: abs(xb-xa) >= 1e-4. (Note that 10^-4 is an expensive power operation, while 1e-4 is a cheap constant.)
James is right: Without seeing the code, it is impossible to tell you, where the problems are. Please insert it by editing the question (not by posting it as comment or answer) and use the "{} Code" button for a proper formatting. Thanks.
Rachel Dawn
Rachel Dawn on 26 Feb 2018
Edited: Rachel Dawn on 26 Feb 2018
@Jan Simon @James Tursa The code is correct, and there shouldn't be a zero in that interval. Also, I meant to put ">", not ==. The point is that I'm trying to figure out why the bisection method would give me an answer for a root that is not zero. I thought maybe it had to do with the fact that the graph showed a straight line (I attached the picture). It was for an assignment and I was supposed to explain why the bisection method would give me a wrong answer in this case (aside from the fact that there's no zero).
You state "... the code is correct ..." and also that the code will "... give me an answer for a root that is not zero ..."
These two statements seem to be mutually exclusive. But again, we can't help you until you show us the code and how you are calling the code. How can we possibly know where a logic error or argument input error is unless we see your code?
@Rachel: You ask us why your code produces a certain output, but do not want to show us the code? This cannot work.
Does it have to do with the graph looking like a straight,
horizontal line in this interval?
This is a bold guess. Guessing does not help in computer science. Remember that the data are stored in doubles with 15 significant digits. There is no way to observe straightness by looking on the data.
Your initial interval is [4, 6] and 6 is the final output. Another bold guess is, that your algorithm returns the upper limit, if no zero is contained in the interval.

Sign in to comment.

Answers (1)

Jan
Jan on 26 Feb 2018
Actually your should be able to find out by your own, why this value is replied. Simply use the debugger to step through your code line by line: https://www.mathworks.com/help/matlab/matlab_prog/debugging-process-and-features.html .

Asked:

on 26 Feb 2018

Answered:

Jan
on 26 Feb 2018

Community Treasure Hunt

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

Start Hunting!