用牛顿法迭代python实现,这个为什么没输出啊
```python
import numpy as np
from timeit import default_timer as timer
start_time = timer()
def f(x):
# 定义目标函数
result = 0
for i in range(1, 10):
result += ((1 - x[i-1])**2 + 100*(x[i] - x[i-1])**2)
return result
def gradient(x):
# 计算梯度向量
grad = np.zeros(len(x))
for i in range(1, 10):
grad[i-1] = 2 * 200 * x[i-1] - 200 * x[i-1] * x[i] + x[i-1] - 1
grad[i] = 200 * (x[i] - x[i-1]**2)
return grad
def hessian(x):
# 计算黑塞矩阵
hess = np.zeros((10, 10))
for i in range(1, 9):
hess[i][i-1] = -400 * x[i-1]
hess[i][i] = 202 + 1200 * x[i-1]**2 - 400 * x[i]
hess[i][i+1] = -400 * x[i]
hess[0][0] = 202 + 1200 * x[0]**2 - 400 * x[1]
hess[0][1] = -400 * x[0]
hess[9][8] = -400 * x[8]
hess[9][9] = 200
return hess
def newton_method(x0, max_iter=10):
x = x0.copy()
for k in range(max_iter):
grad = gradient(x)
hess = hessian(x)
delta_x = -np.linalg.solve(hess, grad) # 解线性方程组 H Δx = -∇f(x)
x += delta_x
print(f"Iteration: {k+1}, Function: {f(x)}")
return x
x0 = np.zeros(10)
newton_method(x0)
end_time = timer()
execution_time = end_time - start_time
print(execution_time)
```