21 KiB
21 KiB
In [1]:
import numpy as np
import matplotlib.pyplot as plt
In [2]:
def f(x):
return 4*x**3 - 24*x**2 + 48*x - 32
In [3]:
def bissection(interval, f, tolerance=1e-5):
x = (interval[1]+interval[0])/2
fx = f(x)
while abs(fx) > tolerance:
if f(interval[0]) > 0 and fx > 0:
interval = [x,interval[1]]
else:
interval = [interval[0],x]
x = (interval[1]+interval[0])/2
fx = f(x)
return x
In [4]:
xs = np.arange(0,4,0.1)
ys = np.array(f(xs))
root=bissection([1,3],f)
plt.plot(xs,ys,'r')
plt.plot(root,0,'.b')
Out[4]:
In [ ]: