Josh.F
Josh.F

Reputation: 3806

In SymPy, why is my solution (nonlinsolve) returning the wrong answer?

I have a system of 3 equations, and I'd like to find a solution for the line of intersection, or the nullcline, of dx=dy.

from sympy import *

x, y, z = symbols('x, y, z')

dx = x - x ** 3 / 3 - z + y
dy = -y ** 2 * 0.1 + z
dz = 0

xy_nullcline = nonlinsolve([dx, dy], [x, y, z])

print(xy_nullcline)

# {
#   (x, -3.16227766016838*sqrt(z), z), 
#   (x, 3.16227766016838*sqrt(z), z)
# }

In the image below, axes are x, y, z and:

The intersection of the two surfaces, my goal, is the set of (x,y,z) where dx=dy=0. You can see clearly in the picture where the intersection is: it's an upward parabolic line, but toward the bottom it pushes out. When I move the purple surface upward a little, that bulge turns into a lone ellipse.

The solution that is found is exactly the purple curve, not the intersection. I have used this same method for finding intersections of other curves, and the result is, as expected, just a line that tracks where one function equals the other. SymPy returns it as FiniteSets, but this comes back as the wrong 2d surface.

Am I doing something wrong? Or is this a bug?

enter image description here

Upvotes: 0

Views: 3541

Answers (1)

user6655984
user6655984

Reputation:

Looks like nonlinsolve ignores the first of two equations. I prefer to use solve. Also, avoid creating floating point coefficients like 0.1; they cause problems on many levels in SymPy. Use dy = -y ** 2 / 10 + z. See Python numbers vs. SymPy Numbers.

In SymPy 1.1.1, I get

>>> solve([dx, dy], [x, y, z])

[{x: -(50**(1/3)*(3*y**2 - 30*y + sqrt(9*y**2*(y - 10)**2 - 400))**(2/3)/10 + 20**(1/3))/(3*y**2 - 30*y + sqrt(9*y**2*(y - 10)**2 - 400))**(1/3), z: y**2/10}, 
 {x: (50**(1/3)*(1 - sqrt(3)*I)**2*(3*y**2 - 30*y + sqrt(9*y**2*(y - 10)**2 - 400))**(2/3) + 40*20**(1/3))/(20*(1 - sqrt(3)*I)*(3*y**2 - 30*y + sqrt(9*y**2*(y - 10)**2 - 400))**(1/3)), z: y**2/10}, 
 {x: (50**(1/3)*(1 + sqrt(3)*I)**2*(3*y**2 - 30*y + sqrt(9*y**2*(y - 10)**2 - 400))**(2/3) + 40*20**(1/3))/(20*(1 + sqrt(3)*I)*(3*y**2 - 30*y + sqrt(9*y**2*(y - 10)**2 - 400))**(1/3)), z: y**2/10}]

So there are three solutions, which are curves parametrized by y, not surfaces.

Upvotes: 1

Related Questions