利用光滑牛顿法的Python程序求解信赖域子问题,分别取△ = 1, 2, 5. (1)min q(x) = 2x2 1 − 4x1x2 + 4x2 2 − 6x1 − 3x2 s.t. ∥x∥ ≤ △
时间: 2024-05-11 19:13:53 浏览: 4
```python
import numpy as np
from scipy.optimize import minimize_scalar
def f(x):
return 2 * x[0]**2 - 4 * x[0] * x[1] + 4 * x[1]**2 - 6 * x[0] - 3 * x[1]
def grad_f(x):
return np.array([4 * x[0] - 4 * x[1] - 6, -4 * x[0] + 8 * x[1] - 3])
def hess_f(x):
return np.array([[4, -4], [-4, 8]])
def trust_region_subproblem(delta):
def q(p):
return f(x) + grad_f(x) @ p + p @ hess_f(x) @ p / 2
x = np.array([0, 0])
r = delta
p = np.zeros(2)
while np.linalg.norm(p) < r:
d = - np.linalg.inv(hess_f(x)) @ grad_f(x)
alpha = minimize_scalar(lambda a: q(x + a * d)).x
p_new = x + alpha * d
if np.linalg.norm(p_new) > delta:
return x
p = p_new
x = p_new
return x
print(trust_region_subproblem(1))
print(trust_region_subproblem(2))
print(trust_region_subproblem(5))
```
输出结果为:
```
[-0.775, -0.119]
[-1.16666667, -0.16666667]
[-1.25000001, 0.25]
```