收敛速度:Newton 迭代法在单根处至少具有二阶收敛性,但 Newton 迭代法一般只有局部收敛性。
import numpy as np
import matplotlib.pyplot as plt
def f(x):
return x**2 - 2
def df(x):
return 2 * x
def newton_method(f, df, x0, tol=1e-10, max_iter=100):
x = x0
iterations = [x0]
for _ in range(max_iter):
x_new = x - f(x) / df(x)
iterations.append(x_new)
if abs(x_new - x) < tol:
break
x = x_new
return x, iterations
# 初始猜测
x0 = 1.0
# 使用牛顿迭代法求解
root, iterations = newton_method(f, df, x0)
print(f"Approximate root: {root}")
# 绘制迭代过程
plt.plot(iterations, marker='o')
plt.xlabel('Iteration')
plt.ylabel('x')
plt.title('Newton Method Convergence')
plt.grid(True)
plt.show()