如何解决属性错误:"列表"对象在使用python求解微分方程时没有属性"rhs"?


from sympy import *
import matplotlib.pyplot as plt
x,y=symbols('x y', real =True)
M=5*x*sqrt(x)+7*y**2/sqrt(x)
N=28*y*sqrt(x)
if diff(M,y) == diff(N,x):
print("The equation is exact")
else:
print("The equation is not Exact")
y=Function('y')
deq=(5*x*sqrt(x)+7*y(x)**2/sqrt(x))+(28*y(x)*sqrt(x))*diff(y(x),x)
ysoln=dsolve(deq,y(x),hint='1st_exact',ics={y(1):1})
print("The solution of the given differential equation is:")
pprint(ysoln)
plt.plot(ysoln.rhs, (x,-2,2))

这是一个用于求解微分方程deq的python程序。但是当运行这段代码时,它显示属性错误。

我试着改变值和改变方程

首先,您需要将SymPy更新到最新版本(1.11)。

那么,plt.plot不知道如何处理sympy对象。因此,您需要使用sympy的plot。因此,你必须将最后一行代码修改为:

plot(ysoln.rhs, (x,-2,2))

最新更新