如何在sympy中绘制已求解方程的结果?

问题描述

我有一个方程式

(1 - y)/y^4 = x^2

,我需要绘制y(x)。我已使用以下代码解决了该问题:

from sympy import symbols,Eq,solve

x,y = symbols('x y')
equation = Eq((y-1)*y**(-4),x**2)

solution = solve(equation,y)
print(solution)

我得到了这个输出的整体图:

[Piecewise((-sqrt(-(-1/x**4)**(1/3))/2 - sqrt((-1/x**4)**(1/3) - 2/(x**2*sqrt(-(-1/x**4)**(1/3))))/2,Eq(x**(-2),0)),(-sqrt(2*(sqrt(-1/(27*x**6) + 1/(256*x**8)) + 1/(16*x**4))**(1/3) + 2/(3*x**2*(sqrt(-1/(27*x**6) + 1/(256*x**8)) + 1/(16*x**4))**(1/3)))/2 - sqrt(-2*(sqrt(-1/(27*x**6) + 1/(256*x**8)) + 1/(16*x**4))**(1/3) - 2/(x**2*sqrt(2*(sqrt(-1/(27*x**6) + 1/(256*x**8)) + 1/(16*x**4))**(1/3) + 2/(3*x**2*(sqrt(-1/(27*x**6) + 1/(256*x**8)) + 1/(16*x**4))**(1/3)))) - 2/(3*x**2*(sqrt(-1/(27*x**6) + 1/(256*x**8)) + 1/(16*x**4))**(1/3)))/2,True)),Piecewise((-sqrt(-(-1/x**4)**(1/3))/2 + sqrt((-1/x**4)**(1/3) - 2/(x**2*sqrt(-(-1/x**4)**(1/3))))/2,(-sqrt(2*(sqrt(-1/(27*x**6) + 1/(256*x**8)) + 1/(16*x**4))**(1/3) + 2/(3*x**2*(sqrt(-1/(27*x**6) + 1/(256*x**8)) + 1/(16*x**4))**(1/3)))/2 + sqrt(-2*(sqrt(-1/(27*x**6) + 1/(256*x**8)) + 1/(16*x**4))**(1/3) - 2/(x**2*sqrt(2*(sqrt(-1/(27*x**6) + 1/(256*x**8)) + 1/(16*x**4))**(1/3) + 2/(3*x**2*(sqrt(-1/(27*x**6) + 1/(256*x**8)) + 1/(16*x**4))**(1/3)))) - 2/(3*x**2*(sqrt(-1/(27*x**6) + 1/(256*x**8)) + 1/(16*x**4))**(1/3)))/2,Piecewise((sqrt(-(-1/x**4)**(1/3))/2 - sqrt((-1/x**4)**(1/3) + 2/(x**2*sqrt(-(-1/x**4)**(1/3))))/2,(sqrt(2*(sqrt(-1/(27*x**6) + 1/(256*x**8)) + 1/(16*x**4))**(1/3) + 2/(3*x**2*(sqrt(-1/(27*x**6) + 1/(256*x**8)) + 1/(16*x**4))**(1/3)))/2 - sqrt(-2*(sqrt(-1/(27*x**6) + 1/(256*x**8)) + 1/(16*x**4))**(1/3) + 2/(x**2*sqrt(2*(sqrt(-1/(27*x**6) + 1/(256*x**8)) + 1/(16*x**4))**(1/3) + 2/(3*x**2*(sqrt(-1/(27*x**6) + 1/(256*x**8)) + 1/(16*x**4))**(1/3)))) - 2/(3*x**2*(sqrt(-1/(27*x**6) + 1/(256*x**8)) + 1/(16*x**4))**(1/3)))/2,Piecewise((sqrt(-(-1/x**4)**(1/3))/2 + sqrt((-1/x**4)**(1/3) + 2/(x**2*sqrt(-(-1/x**4)**(1/3))))/2,(sqrt(2*(sqrt(-1/(27*x**6) + 1/(256*x**8)) + 1/(16*x**4))**(1/3) + 2/(3*x**2*(sqrt(-1/(27*x**6) + 1/(256*x**8)) + 1/(16*x**4))**(1/3)))/2 + sqrt(-2*(sqrt(-1/(27*x**6) + 1/(256*x**8)) + 1/(16*x**4))**(1/3) + 2/(x**2*sqrt(2*(sqrt(-1/(27*x**6) + 1/(256*x**8)) + 1/(16*x**4))**(1/3) + 2/(3*x**2*(sqrt(-1/(27*x**6) + 1/(256*x**8)) + 1/(16*x**4))**(1/3)))) - 2/(3*x**2*(sqrt(-1/(27*x**6) + 1/(256*x**8)) + 1/(16*x**4))**(1/3)))/2,True))]

我想在0 <= y <= 10 <= x <= some_large_number的边界上绘制它。我还无法弄清楚该怎么做,但是我敢肯定,如果我的cpu使用PITA,那对我来说一定很容易。

此外,对我而言,以解析方式解决此问题也无济于事。我会很高兴以数字方式执行此操作,但是我也不知道该怎么做。 Sympy只是向我展示的一种解决方案。

解决方法

符号似乎是mixing in complex solutions。我不确定在这种情况下如何应对。请注意,对于y小于1的情况,LHS为负,因此永远不要等于实数的平方。

但是,如果您最感兴趣的是情节,plot_implicit会派上用场:

from sympy import symbols,Eq
from sympy.plotting import plot_implicit

x,y = symbols('x y') # symbols('x y',real=True,positive=True) doesn't seem to make a difference
equation = Eq(((y - 1) * y ** (-4)),x ** 2)

plot_implicit(equation)

example plot

PS:要找到曲线的最右点,请对左侧solve(equation.lhs.diff())求微分,得出解y = 4/3,对应于x = 2*sqrt(3)/3。寻找y >= 1的数值解很简单。 nsolve可以找到小于x0的给定2*sqrt(3)/3的数值解,例如nsolve(equation.subs(x,.1),1)给出1.01042350456793