Gustavo Mirapalheta
Gustavo Mirapalheta

Reputation: 997

sympy seems to fail solving a system of non-linear equations while being able to solve them separately

I'm trying to find the points (x,y) where the first derivatives of a funtion f(x,y) are zero. The function is f = x * log((y^2)/x) - x*(y^2) + 3*x.

If I try solving the system of equations in sympy I get the answer (x,1) and (x,-1), which I think means "whatever the value of x" and y either equal to 1 or -1. The code can be seen below:

    import sympy as sp
    x, y = sp.symbols("x y", real = True)
    f = x * sp.log((y**2)/x) - x*(y**2) + 3*x
    sp.solve([f.diff(x), f.diff(y)],[x,y])

    :> [(x, -1), (x, 1)]

If I try solving the derivative of f in relation to y first, for y, I get as a result [1,-1] as expected. :

    sp.solve(f.diff(y),y)

    :> [-1,1]

Then if I try replacing y by either 1 or -1 in the expression of the derivative of f in relation to x and then solve for x I get as result:

    sp.solve(f.diff(x).subs(y,1),x)

    :> [E]

The pairs [E,-1] and [E,1] are the solutions for the system of equations. But why sympy can't give me these pairs of values when I try solving the system of equations initially?

Upvotes: 2

Views: 239

Answers (1)

smichr
smichr

Reputation: 19135

I'm not sure why the system-solver fails, but if you use manual=True you can get your solutions:

>>> eqs=[f.diff(x), f.diff(y)]
>>> solve(eqs,manual=1)
[{y: -1, x: E}, {y: 1, x: E}]

Upvotes: 1

Related Questions