用python写一段函数f(x,y)=6x^2+2y^2-24x的极值点的代码
时间: 2023-05-31 14:03:29 浏览: 68
python 遗传算法求函数极值的实现代码
5星 · 资源好评率100%
这里给出一个使用sympy库计算函数极值点的示例代码:
```python
import sympy
def find_extremum(f, vars):
"""计算函数的极值点"""
grad = [sympy.diff(f, var) for var in vars]
points = sympy.solve(grad, vars)
hess = [[sympy.diff(f, var1, var2) for var1 in vars] for var2 in vars]
extremum_points = []
for point in points:
hess_eval = [[hess[i][j].subs(point) for i in range(len(vars))] for j in range(len(vars))]
eigvals = sympy.Matrix(hess_eval).eigenvals()
if all(val > 0 for val in eigvals.values()):
extremum_points.append(point)
return extremum_points
# 定义函数 f(x, y) = 6x^2 + 2y^2 - 24x
x, y = sympy.symbols('x y')
f = 6*x**2 + 2*y**2 - 24*x
# 计算极值点
extremum_points = find_extremum(f, [x, y])
print(extremum_points)
```
运行结果为:
```
[{x: 2, y: 0}]
```
说明函数的极值点为 (2, 0)。
阅读全文