简体   繁体   English

如何解决 AttributeError: 'list' object has no attribute 'rhs' while solving differential equation using python?

[英]How to solve AttributeError: 'list' object has no attribute 'rhs' while solving differential equation using python?

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))

this is the python program for solving a differential equation deq.这是用于求解微分方程 deq 的 python 程序。 but while running this code it shows attribute error.但是在运行此代码时它显示属性错误。

I tried changing the values and changing the equation我尝试更改值并更改方程式

First off, you need to update SymPy to the latest version (1.11).首先,您需要将 SymPy 更新到最新版本 (1.11)。

Then, plt.plot doesn't know how to deal with sympy objects.然后, plt.plot不知道如何处理 sympy 对象。 Hence you need to use sympy's plot .因此,您需要使用 sympy 的plot So, you have to modify the last line code to this:因此,您必须将最后一行代码修改为:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM