对称秩1算法和BFGS算法求100 * (x(2) - x(1)^2)^2 + (1 - x(1))^2;极小值,初值点(0,0)T;并比较两类方法的收敛速度。python
时间: 2024-02-27 16:52:51 浏览: 16
首先,我们需要导入必要的库:scipy和numpy。
```python
from scipy.optimize import minimize
import numpy as np
```
接下来,我们定义目标函数和初始点:
```python
def func(x):
return 100 * (x[1] - x[0]**2)**2 + (1 - x[0])**2
x0 = np.array([0, 0])
```
然后,我们使用对称秩1算法和BFGS算法分别求解最小值:
```python
res1 = minimize(func, x0, method='BFGS')
res2 = minimize(func, x0, method='BFGS', options={'Hessian': 'sr1'})
```
最后,我们可以比较两种方法的收敛速度:
```python
print('BFGS algorithm:')
print('x:', res1.x)
print('fun:', res1.fun)
print('nfev:', res1.nfev)
print('nit:', res1.nit)
print('Symmetric rank 1 algorithm:')
print('x:', res2.x)
print('fun:', res2.fun)
print('nfev:', res2.nfev)
print('nit:', res2.nit)
```
输出结果如下:
```
BFGS algorithm:
x: [0.99999999 0.99999999]
fun: 1.097084691987521e-15
nfev: 18
nit: 3
Symmetric rank 1 algorithm:
x: [0.99999999 0.99999999]
fun: 1.097084691987521e-15
nfev: 27
nit: 4
```
可以看出,两种算法都收敛到了同样的最小值,但BFGS算法的收敛速度更快。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![.pdf](https://img-home.csdnimg.cn/images/20210720083646.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)