Python Is My Python Bisection Method Code Correct?

Click For Summary
The discussion focuses on reviewing a Python implementation of the bisection method for finding roots of a polynomial function. The code provided successfully runs without errors, but several improvements are suggested for clarity and efficiency. One key point is the use of conditional expression chaining, which is considered unnecessary and potentially confusing. Instead, a more conventional approach using logical operators is recommended for better readability. Additionally, the use of the NumPy library's `sign` function is proposed to enhance efficiency by avoiding multiple evaluations of the function at the same points. Concerns are raised about the return statement, questioning whether the variable 'a' is the best approximation at that point and how the code handles cases where 'a' or 'b' might be an exact root. Overall, the discussion emphasizes the importance of code clarity, efficiency, and robustness in programming practices.
ver_mathstats
Messages
258
Reaction score
21
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.
 
Technology news on Phys.org
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?
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 3 ·
Replies
3
Views
1K
Replies
55
Views
6K
  • · Replies 2 ·
Replies
2
Views
1K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 18 ·
Replies
18
Views
1K