I want to make a Python program that will run a bisection method to determine the root of:
f(x) = -26 + 85x - 91x2 +44x3 -8x4 + x5
The Bisecti
I'm trying to make some modifications :
This is my code:
import numpy as np
def bisection3(f,x0,x1,tol,max_iter):
c = (x0+x1)/2.0
x0 = c
Xs = 0.3604217029603
err_Abs = np.linalg.norm(x0-Xs)
itr = 0
f_x0 = f(x0)
f_x1 = f(x1)
xp = [] # sucesion que converge a la raiz
errores_Abs = []
errores_Aprox = []
fs = [] # esta sucecion debe converger a cero
while(((x1-x0)/2.0 > tol) and (itr< max_iter)):
if f(c) == 0:
return c
elif f(x0)*f(c) < 0:
x1 = c
else :
x0 = c
c = (x0+x1)/2.0
itr +=1
err_Abs = np.linalg.norm(x0-Xs)
err_Aprox = np.linalg.norm(x1 - x0)
fs.append(f(c))
xp.append(c)
errores_Abs.append(err_Abs)
errores_Aprox.append(err_Aprox)
return x0,errores_Abs, errores_Aprox,fs,xp
And i have an example of execution :
f = lambda x : 3*x + np.sin(x) - np.exp(x)
X0_r1 , err_Abs_r1,err_Aprox_r1, fs_r1 , xp_r1 = bisection3(f,0,0.5,1e-5,100)