1

I'm coding the bisection method in Python. I have set a tolerance for the error in my routine. I have two questions:

  1. My function should be able to find the root of an arbitrary continuous scalar-valued function. How do I pass this to bisection_method and use it properly?
  2. How can I record how many iterations it takes to reach the specified tolerance?

Code:

def f(x):
    return(x**2 - 11)

def bisection_method(a, b, tol):
    if f(a)*f(b) > 0:
        #end function, no root.
        print("No root found.")
    else:
        while (b - a)/2.0 > tol:
            midpoint = (a + b)/2.0
            if f(midpoint) == 0:
                return(midpoint) #The midpoint is the x-intercept/root.
            elif f(a)*f(midpoint) < 0: # Increasing but below 0 case
                b = midpoint
            else:
                a = midpoint
        return(midpoint)

answer = bisection_method(-1, 5, 0.0001)
print("Answer:", answer)

which gives the following output:

Answer: 3.31671142578125
4
  • 1
    Concerning (1), I don't think your function works in any case, e.g. for x**2 - 1, bisection_method(-2, 2, 1) would say "No root found", but there are roots -1 and 1. Commented Oct 25, 2018 at 19:02
  • @Prune I originally had it in a list form but it messed up the code programming, that's why I did the manual list Commented Oct 25, 2018 at 19:06
  • @user123: you have to put some normal text in between to reset the formatting. You probably saw how I goofed it up. Commented Oct 25, 2018 at 19:12
  • @Prune okay I didn't know you had to add text, I will next time! thanks Commented Oct 25, 2018 at 19:12

1 Answer 1

1

For the function, simply pass the function name as an argument. I've changed your function's name to root11 and made it the first argument to the bisection.

For the count ... you should have been able to look this up on line. Just count iterations as you would before you learned the for statement. Return this with the final answer.

Note that I removed your check for an exact answer: you'll find it on the next iteration, anyway.

def root11(x):
    return(x**2 - 11)

def bisection_method(f, a, b, tol):
    if f(a)*f(b) > 0:
        #end function, no root.
        print("No root found.")
    else:
        iter = 0
        while (b - a)/2.0 > tol:
            midpoint = (a + b)/2.0

            if f(a)*f(midpoint) < 0: # Increasing but below 0 case
                b = midpoint
            else:
                a = midpoint

            iter += 1
        return(midpoint, iter)

answer, iterations = bisection_method(root11, -1, 5, 0.0001)
print("Answer:", answer, "\nfound in", iterations, "iterations")

import math
answer, iterations = bisection_method(math.cos, 0, 2, 0.0001)
print("Answer:", answer, "\nfound in", iterations, "iterations")

Output:

Answer: 3.31671142578125 
found in 15 iterations
Answer: 1.5706787109375 
found in 14 iterations
Sign up to request clarification or add additional context in comments.

10 Comments

I see what you did there, so that covers the first question I was asked? I was a little confused with the wording
Yes: note that f is now a function parameter -- whatever function name you pass in will become f within bisection_method.
if I wanted to change x*2-11 to cosx would I need to import math?
Yes; you need to have the function available to pass.
That's a separate issue (actually, so was the previous one). Stack Overflow guidelines require a separate posting for a new question. Also, that's the proper place to format the code I need to see, after you've looked up whatever technique you're trying to use.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.