import numpy as np
import matplotlib.pyplot as plt
def newton_method(f, f_prime, x0, tolerance=0.00001, max_iterations=100):
x_values = [x0]
iterations = 0
while True:
x1 = x0 - f(x0) / f_prime(x0)
x_values.append(x1)
iterations += 1
if abs(x1 - x0) < tolerance or iterations >= max_iterations:
print(f"iteration complete! {iterations}")
break
x0 = x1
return x_values
# Define the function and its derivative
def function(x):
return 0.5 * x**2
def derivative(x):
return x
# Set initial values
initial_guess = 50
# Run Newton's method
resulting_values = newton_method(function, derivative, initial_guess)
# Plot the convergence process
x_values = np.linspace(-50, 50, 1000)
y_values = function(x_values)
plt.plot(x_values, y_values, label='object Function f(x) = 0.5 * x^2')
plt.scatter(resulting_values, function(np.array(resulting_values)), color='red', label='Convergence Points')
plt.title("Newton's Method in Convex Function")
plt.xlabel('x')
plt.ylabel('f(x)')
plt.legend()
plt.show()