Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def equation(x):
- return 2 * x**3 + 2 * x**2 - 2 * x - 7
- def bisection_method(a, b, tolerance=1e-3, max_iterations=100):
- if equation(a) * equation(b) >= 0:
- raise ValueError("The equation may not have a root in the given interval")
- for iteration in range(max_iterations):
- c = (a + b) / 2
- if equation(c) == 0 or abs(b - a) / 2 < tolerance:
- return c, iteration
- if equation(c) * equation(a) < 0:
- b = c
- else:
- a = c
- raise ValueError("The method did not converge")
- # Solve the equation using the bisection method
- a = -10
- b = 10
- root, iteration = bisection_method(a, b)
- print("Root:", root)
- print("Value at the root:", equation(root))
- print("Iteration:", iteration)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement