Skip to content

Instantly share code, notes, and snippets.

@nxvipin
Created September 24, 2012 11:38
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save nxvipin/3775568 to your computer and use it in GitHub Desktop.
Save nxvipin/3775568 to your computer and use it in GitHub Desktop.
Some numerical methods in python.
"""
Bisection, Secant & Newton Raphson Method.
"""
import math
"""
* Variable Description:
*
* f : Given function
* f_ : Derivative of f
* [a, b] : End point values
* TOL : Tolerance
* NMAX : Max number of iterations
"""
def bisection(f, a, b, TOL=0.001, NMAX=100):
"""
Takes a function f, start values [a,b], tolerance value(optional) TOL and
max number of iterations(optional) NMAX and returns the root of the equation
using the bisection method.
"""
n=1
while n<=NMAX:
c = (a+b)/2.0
print "a=%s\tb=%s\tc=%s\tf(c)=%s"%(a,b,c,f(c))
if f(c)==0 or (b-a)/2.0 < TOL:
return c
else:
n = n+1
if f(c)*f(a) > 0:
a=c
else:
b=c
return False
def secant(f,x0,x1, TOL=0.001, NMAX=100):
"""
Takes a function f, start values [x0,x1], tolerance value(optional) TOL and
max number of iterations(optional) NMAX and returns the root of the equation
using the secant method.
"""
n=1
while n<=NMAX:
x2 = x1 - f(x1)*((x1-x0)/(f(x1)-f(x0)))
if x2-x1 < TOL:
return x2
else:
x0 = x1
x1 = x2
return False
def newtonraphson(f, f_, x0, TOL=0.001, NMAX=100):
"""
Takes a function f, its derivative f_, initial value x0, tolerance value(optional) TOL and
max number of iterations(optional) NMAX and returns the root of the equation
using the newton-raphson method.
"""
n=1
while n<=NMAX:
x1 = x0 - (f(x0)/f_(x0))
if x1 - x0 < TOL:
return x1
else:
x0 = x1
return False
if __name__ == "__main__":
def func(x):
"""
Function x^3 - x -2
We will calculate the root of this function using different methods.
"""
return math.pow(x,3) - x -2
def func_(x):
"""
Derivate of the function f(x) = x^3 - x -2
This will be used in Newton-Rahson method.
"""
return 3*math.pow(x,2)-1
#Invoking Bisection Method
res = bisection(func,1,2).
print res
#Invoking Secant Method
res = bisection(func,1,2).
print res
#Invoking Newton Raphson Method
res = newtonraphson(func,func_,1)
print res
@stephenbez
Copy link

It looks like the lines that compare with the tolerance are wrong.

For example:

    if x2-x1 < TOL

should be:

    if abs(x2-x1) < TOL

otherwise, it will incorrectly return early anytime x1 is larger than x2.

If you are looking for libraries that already implement these functions, look here:
http://docs.scipy.org/doc/scipy/reference/optimize.html

@Ewen2015
Copy link

Ewen2015 commented Nov 6, 2015

I guess it missed "n += 1" in the while loop.
When the Newton and the secant don't converge, it becomes endless loop(死循环).

@gridley
Copy link

gridley commented Apr 23, 2017

Yep, I was looking for a secant method function online out of laziness. Need to use abs() when you check if guesses are below the tolerance in newton and secant methods.

@haris989
Copy link

In your main function, you have used bisection in place of secant!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment