牛顿共轭梯度法python
时间: 2023-10-13 15:26:50 浏览: 85
牛顿共轭梯度法是一种求解无约束优化问题的方法,它结合了牛顿法和共轭梯度法的优点,可以收敛速度更快。下面给出Python实现的代码:
```python
import numpy as np
def newton_cg(f, df, d2f, x0, max_iter=1000, tol=1e-8):
"""
Newton-CG algorithm for unconstrained optimization.
Parameters:
f: callable, objective function.
df: callable, gradient of the objective function.
d2f: callable, Hessian of the objective function.
x0: numpy.ndarray, initial point.
max_iter: int, maximum number of iterations.
tol: float, tolerance for stopping criterion.
Returns:
x: numpy.ndarray, the optimal point.
"""
x = x0
g = df(x)
d = -g
k = 0
while k < max_iter and np.linalg.norm(g) > tol:
alpha = np.dot(d, g) / np.dot(d, np.dot(d2f(x), d))
x = x + alpha * d
g_new = df(x)
beta = np.dot(g_new, np.dot(d2f(x), d)) / np.dot(d, np.dot(d2f(x), d))
d = -g_new + beta * d
g = g_new
k += 1
return x
```
其中,`f`、`df`、`d2f`分别是目标函数、目标函数的梯度和目标函数的海森矩阵。`x0`是初始点,`max_iter`是最大迭代次数,`tol`是收敛精度。函数返回最优点`x`。
使用时,需要先定义目标函数、目标函数的梯度和海森矩阵,然后调用`newton_cg`函数即可。例如:
```python
def f(x):
return x[0]**2 + x[1]**2
def df(x):
return np.array([2*x[0], 2*x[1]])
def d2f(x):
return np.array([[2, 0], [0, 2]])
x0 = np.array([1, 1])
x_opt = newton_cg(f, df, d2f, x0)
print(x_opt) # 输出 [0. 0.]
```
以上代码演示了如何使用Newton-CG算法求解二次函数的最小值。