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

一曲冷凌霜 提交于 2019-12-11 18:27:37

问题


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:

  • orange curve is the dx nullcline, (x,y,z), where dx=0, a cubic polynomial
  • purple curve is the dy nullcline, dy=0, a square polynomial

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?


回答1:


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.



来源:https://stackoverflow.com/questions/49469047/in-sympy-why-is-my-solution-nonlinsolve-returning-the-wrong-answer

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!