Search code examples
pythonpython-2.7numerical-methodsbisection

ImportError: No module named error


In Numerical Methods in Engineering with Python, 2nd Edition, Author: Jaan Kiusalaas, I wrote the same module found on page 146 that calculates the roots, f(x) = 0, using the bisection method:

from math import log,ceil
import error

def bisection2(f,x1,x2,switch=0,tol=1.0e-9):
        f1 = f(x1)
        if f1 == 0.0: return x1
        f2 = f(x2)
        if f2 == 0.0: return x2
        if f1*f2 > 0.0: error.err('Root is not bracketed')
        n = ceil(log(abs(x2 - x1)/epsilon)/log(2.0))
        for i in range(n):
            x3 = 0.5*(x1 + x2); f3 = f(x3)
            if (switch == 1) and (abs(f3) > abs(f1)) \
                                         (abs(f3) > abs(f2)):
                return None
            if f3 == 0.0:return x3
            if f2*f3 < 0.0:
                x1 = x3; f1 = f3
            else:
                x2 = x3; f2 = f3
        return (x1 + x2)/2.0

I am encountering the following error:

ImportError Traceback (most recent call last) /usr/lib/python2.7/dist-packages/IPython/utils/py3compat.pyc in execfile(fname, *where) 173 else: 174 filename = fname --> 175 builtin.execfile(filename, *where)

/home/uwhpsc/Desktop/bisection2/bisection2.py in () 1 from math import log,ceil ----> 2 import error 3 4 def bisection2(f,x1,x2,switch=0,tol=1.0e-9): 5 f1 = f(x1)

ImportError: No module named error

Kindly could anyone please advise me how to solve this issue?


Solution

  • To make this code work, you can remove the line that says:

    import error
    

    And on the line that says:

    if f1*f2 > 0.0: error.err('Root is not bracketed')
    

    Change that to:

    if f1*f2 > 0.0: quit('Root is not bracketed')