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

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!