Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import numpy as np
- def equation(x):
- return 2 * x**3 + 2 * x**2 - 2 * x - 7
- def derivative(x):
- return 6 * x**2 + 4 * x - 2
- def newton_method(initial_guess, tolerance=1e-3, max_iterations=100):
- x = initial_guess
- for iteration in range(max_iterations):
- delta = equation(x) / derivative(x)
- x_k = x - delta
- #x -= delta
- if equation(x_k) * equation(x_k + np.sign(x_k - x)*tolerance) < 0:
- return x_k, iteration
- x = x_k
- raise ValueError("The method did not converge")
- # Solve the equation using Newton's method
- initial_guess = 1.0
- root, iteration = newton_method(initial_guess)
- print("Root:", root)
- print("Value at the root:", equation(root))
- print("Iterations:", iteration)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement