简体   繁体   English

Python 中的 Sympy 没有给出想要的结果

[英]Sympy in Python not giving the desired result

I am trying to use sympy to solve a simple 2nd order ODE: gamma*y''(x) + 4*y(x)=0.我正在尝试使用 sympy 来解决一个简单的二阶 ODE:gamma*y''(x) + 4*y(x)=0。 I keep getting an error "name x is not defined".我不断收到错误“名称 x 未定义”。 And everytime I try to correct one error, another error pops up.每次我尝试纠正一个错误时,都会弹出另一个错误。 Please could someone tell me what is wrong with my code.请有人告诉我我的代码有什么问题。 Also, how do I plot the solution?另外,我如何绘制解决方案?

Originally I have issues with:最初我有以下问题:

from sympy import* 
from sympy.solvers import dsolve
import sympy as sp
from pylab import*
dsolve?
f = sp.symbols("f", cls=sp.Function)
f(x)
ode1 = gamma*f(x).diff(x,2) + 4*f(x)
f_init = [1,0]
gamma = 0.01
sol1 = dsolve(ode1, f(x), f_init)
print(ode1, sol1)


UPDATE: this made it was what I was looking for to make it work for me:更新:这使它成为我正在寻找的使它对我有用的东西:

from sympy import* 
from sympy.solvers import dsolve
import sympy as sp
from pylab import*
import matplotlib.pyplot as plt

x = sp.symbols('x')
f = sp.Function("f")

gamma = 0.01
ode1 = sp.Eq(gamma*f(x).diff(x,2)+4*x)
sol1 = sp.dsolve(ode1, ics={f(0): 0, sp.diff(f(x), x).subs(x,0): 1})

print(ode1, sol1)

At least this should be a start, but then not sure what would you like to achieve further, take a look at:至少这应该是一个开始,但不确定你想进一步实现什么,看看:

from sympy.solvers import dsolve
import sympy as sp

x = sp.symbols('x')
f = sp.Function("f")
gamma = 0.01
ode1 = sp.Eq(gamma*f(x).diff(x,2)+4*x)
sol1 = sp.dsolve(ode1, ics={f(0): 0, sp.diff(f(x), x).subs(x,0): 1})

print(ode1, sol1)

Prints out:打印出来:

Eq(4*x + 0.01*Derivative(f(x), (x, 2)), 0) Eq(f(x), -200*x**3/3 + x)

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

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