I am trying to create simple code to identify critical points of a multivariate function. For pedagogical reasons, I want to do this completely manually. That is, I first want to compute the gradient, and then I want to solve for points where the gradient is zero. Here is my code:
var('x1 x2')
f(x1,x2) = x1^2 + x2^2
Df=f.derivative()
solve(Df(x1,x2)==(0,0),(x1,x2))
The third step returns what I want. If I evaluate
Df(x1,x2)
it returns (2*x1, 2*x2)
as expected. However the final step returns the following error:
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-33-bc76a33e3a15> in <module>
----> 1 solve(Df(x1,x2)==(Integer(0),Integer(0)),(x1,x2))
/var/tmp/sage-jc4b6yulaujayb9sr94ia88eourzeqip0oidmas3/local/lib/python3.8/site-packages/sage/symbolic/relation.py in solve(f, *args, **kwds)
1045
1046 if not isinstance(f, (list, tuple)):
-> 1047 raise TypeError("The first argument must be a symbolic expression or a list of symbolic expressions.")
1048
1049 # f is a list of such expressions or equations
TypeError: The first argument must be a symbolic expression or a list of symbolic expressions.
The sage solve
functions insists to have a list of equations to solve. (And not an one and only equation involving things with more components.) So let us split into components...
variables = var('x1 x2')
f(x1, x2) = x1^2 + x2^2
Df = f.derivative()
eqs = [ component == 0 for component in Df(x1, x2) ]
solve(eqs, variables)
This gives in the sage interpreter:
sage: solve(eqs, variables)
[[x1 == 0, x2 == 0]]
as expected.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.