Question
///I need to add a graph where it can be seen how the error of each iteration decreases. There are 20 iterations. Help./// #python Bisection
///I need to add a graph where it can be seen how the error of each iteration decreases. There are 20 iterations. Help.///
#python Bisection algorithm
%reset -f
import numpy as np
from numpy import linalg as al
#function to evaluate --------------------------
def f(x):
equation = x**3 +4*x**2-10
return (equation)
#Main program -------------------------
a = -5.0
b = 5.0
tol = 1.0E-5
print ("*****Bisection algorithm *****")
if np.sign(f(a)) == np.sign(f(b)):
print ("Initial conditions not met!!!")
exit(0)
i = 0
while (b-a > tol ):
print ( "a=%.5f " %a, "f(a)=%.5f " %f(a), "b=%.5f "%b, "f(b)=%.5f " %f(b))
print ( "The error of the searched root is =%.5f " %(1.36523-a))
m = a+(b-a)/2
if np.sign(f(a)) == np.sign(f(m)):
a = m
else:
b = m
i = i+1
print ("The root searched is: %.5f" %a, "with ", i, " iterations")
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started