Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # The function is x - b - e^x
- def func(x):
- return x + 2 - np.exp(x)
- # Prints root of func(x)
- # with error of EPSILON
- def bisection(a,b):
- if (func(a) * func(b) >= 0):
- print("You have not assumed right a and b\n")
- return
- c = a
- while ((b-a) >= 0.01):
- # Find middle point
- c = (a+b)/2
- # Check if middle point is root
- if (func(c) == 0.0):
- break
- # Decide the side to repeat the steps
- if (func(c)*func(a) < 0):
- b = c
- else:
- a = c
- print("The value of root is : ","%.4f"%c)
- # Driver code
- # Initial values assumed
- a =-1
- b =1
- bisection(a, b)
- x = np.linspace(-5, 5, 100)
- plt.plot(x, func(x))
- plt.grid()
- plt.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement