def newton_method(f, df, x0, tol=1e-6, max_iter=1000):
x = x0
for i in range(max_iter):
x_new = x - f(x) / df(x)
if abs(x_new - x) < tol:
return x_new
x = x_new
return x
def f(x):
return x**2 - 2
def df(x):
return 2*x
x0 = 1.5
root = newton_method(f, df, x0)
print("The root is:", root)
以下是一个使用Python实现的简单牛顿法示例,用于求解方程 f(x) = x^2 - 2 的根:
于 2024-05-27 17:14:07 首次发布