我正在为单个变量函数编写一个最小化函数。我想创建一个'xmin'数组,其中包含在二阶导数中替换的x的值,使得二阶导数大于0。
代码似乎在调用'xmin'时出错。我不知道如何用优化值替换未知'x'的值。
这就是我的尝试:
from sympy import Symbol
import numpy as np
from sympy import solve
x = Symbol('x')
def fun(x):
return x**2
First = fun(x).diff(x)
Second = First.diff(x)
xopt = solve(First, x)
xmin = solve(Second, x=xopt)
xmin = [i for i in xmin if i > 0]
print('First derivative:\n', First)
print('Second derivative:\n', Second)
print('Optimal points:\n', xopt)
print('Minimum:\n',xmin)
print('End')