0% found this document useful (0 votes)
23 views1 page

Bisection

This document demonstrates using the bisection method to find the root of a function. It defines a bisection method function that takes a function, lower bound, upper bound, and tolerance as inputs. The function is then applied to find the root of a cubic polynomial between 0 and 20 within a tolerance of 0.000000001.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views1 page

Bisection

This document demonstrates using the bisection method to find the root of a function. It defines a bisection method function that takes a function, lower bound, upper bound, and tolerance as inputs. The function is then applied to find the root of a cubic polynomial between 0 and 20 within a tolerance of 0.000000001.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

import math

import numpy as np
import matplotlib.pyplot as plt

def bisection(f,a,b,tol):
if a >= b or f(a)*f(b) >= 0 or tol <=0 :
print("Bisection method fails.")
return None
n = math.ceil(math.log(b-a,2)-math.log(tol,2))
print(n)
for k in range(n):
p = (a+b)/2
fp = f(p)
if(fp * f(a) < 0):
b = p
fb = fp
else:
a = p
fa = fp
p = (a+b)/2
return p

tol = 0.000000001
f = lambda x: x**3 - 30*x**2 + 2552
a = 0
b = 20
print(bisection(f, a, b, tol))

t = np.linspace(0,20,num=1000)
y = t**3 - (30)*(t**2) + 2552
fig = plt.figure(num=0,dpi=120)
plt.plot(t,y,label= 'f(x)')
plt.title("Biseccion")
plt.show()

You might also like