Source

NUMA21 / task3_4.py

from __future__ import division
from allscipy import *

def f(x):
	return 3.0*x**2 - 5.0

def bisec(f, a, b, tol = 10E-9):
	""" Performs the bisection method on the function f in the
	initial interval [a,b].
	"""
	while abs(a - b) > tol:
		if f(a)*f((a + b)/2.0) < 0:
			b = (a + b)/2.0
		else:
			a = (a + b)/2.0

	return (a,b), (a+b)/2.0