# Is My Python Bisection Method Code Correct?

• ver_mathstats
In summary, the code provided uses bisect() to approximate the root of a polynomial function poly(x) using the bisection method. The code uses conditional expression chaining, which can be avoided for better readability and efficiency. It also uses numpy's sign function to determine the sign of the function at a and m, and updates a or b accordingly. However, the code may not always provide the best approximation and may not handle exact roots accurately.
ver_mathstats
Python:
import math

def poly(x):
return (x**4 + 2*x**3 - 7*x**2 - 8*x + 12)

def bisect(f,a,b,tol=1e-6):
while (b-a)>tol:
m=(a+b)/2
if (f(a)>=0>=f(m)) or (f(a)<=0<=f(m)):
b=m
else:
a=m
return (f(a),a)

print(bisect(poly,-4,-2.5))

Here is the code I have using a guide by my teacher. I put a test value at the end just to see if there was an error when I ran it which there was not. Could this please be checked over as I am unsure if I did this right? Thank you.

ver_mathstats said:
Could this please be checked over as I am unsure if I did this right? Thank you.
Does it come up with the right answer?

There are are couple of things that I notice:
Python:
if (f(a) >= 0 >= f(m)) or (f(a) <= 0 <= f(m)):
This uses an unusual feature of Python called conditional expression chaining which in many other languages will throw an error but in some others will appear to work but give the wrong answer. It is also completely unnecessary here and
Python:
if ((f(a) >= 0 and 0 >= f(m)) or (f(a) <= 0 and 0 <= f(m)):
will compile to exactly the same code. In this situation there are two schools of thought:
• You should use all the features of a language that are available if they improve understanding for people that know the language well;
• You should avoid 'quirks' of any particular language if they may be misunderstood by people that may not know the language as well as you do.
This also applies to human language of course, and the general rule is that you should adapt your language to the audience. If was writing to a lawyer I might have said "this dilemma applies mutatis mutandis in human language" (well I wouldn't of course, but I couldn't think of a better example). So as this is (I assume) a general introduction to programming course, I would avoid conditional expression chaining.

But more importantly,
Python:
from numpy import sign
...
if (sign(f(a)) == sign(f(m)):
a = m
else
b = m
Has the same result, is easier to read and more efficient because f(a) and f(m) are only evaluated once.

I would also think again about
Python:
    return (f(a),a)
• - is a always the best approximation you have calculated at this point?
• - it is possible that a or b is by chance an exact (within machine epsilon) root, what does your code do then?

## 1. What is the bisection method in python?

The bisection method is a numerical algorithm used to find the root of a continuous function. It involves repeatedly dividing an interval in half and checking which half contains the root.

## 2. How does the bisection method work in python?

The bisection method works by first defining an interval [a, b] where the root of the function lies. Then, the midpoint of the interval is calculated and used to determine which half of the interval contains the root. This process is repeated until the desired accuracy is achieved.

## 3. What are the advantages of using the bisection method in python?

The bisection method has several advantages, including its simplicity and ease of implementation in python. It also guarantees convergence to the root of the function and is not affected by the initial guess of the root.

## 4. What are the limitations of the bisection method in python?

One limitation of the bisection method is that it can be slow to converge, especially if the initial interval is very large. It also requires the function to be continuous and for the root to lie within the given interval.

## 5. How can I implement the bisection method in python?

To implement the bisection method in python, you will need to define the function you want to find the root of, the initial interval, and the desired accuracy. Then, you can use a loop to repeatedly divide the interval and check for convergence until the desired accuracy is achieved.

• Programming and Computer Science
Replies
15
Views
1K
• Programming and Computer Science
Replies
11
Views
1K
• Programming and Computer Science
Replies
5
Views
2K
• Programming and Computer Science
Replies
3
Views
363
• Programming and Computer Science
Replies
55
Views
4K
• Programming and Computer Science
Replies
9
Views
2K
• Programming and Computer Science
Replies
2
Views
800
• Programming and Computer Science
Replies
7
Views
3K
• Programming and Computer Science
Replies
5
Views
972
• Programming and Computer Science
Replies
4
Views
4K