共轭梯度法python实现FR
时间: 2024-03-15 15:38:34 浏览: 110
python实现共轭梯度法
4星 · 用户满意度95%
```python
# FR格式共轭梯度法的Python实现示例
import numpy as np
def FR_CG(x0, A, b, max_iter=100, tol=1e-5):
x = x0
r = b - np.dot(A, x)
p = r
rsold = np.dot(r, r)
for i in range(max_iter):
Ap = np.dot(A, p)
alpha = rsold / np.dot(p, Ap)
x = x + alpha * p
r = r - alpha * Ap
rsnew = np.dot(r, r)
if np.sqrt(rsnew) < tol:
break
p = r + (rsnew / rsold) * p
rsold = rsnew
return x
```
阅读全文