简体   繁体   English

谐波振荡器的对称微分方程

[英]sympy differential equation of harmonic oscillator

I am here because I've been trying to solve a differential equation using sympy and unfortunately I've not succeed so far. 我在这里是因为我一直在尝试使用sympy求解微分方程,但是不幸的是,到目前为止我还没有成功。 What I've done so far is: 到目前为止,我所做的是:

1) inserting the differential equation, assigning the values and solving it: 1)插入微分方程,赋值并求解:

import sympy as sp
from IPython.display import display
import numpy as np
import matplotlib.pyplot as plt
sp.init_printing()

F0=sp.symbols('F0')
Wd=sp.symbols('Wd')
A=sp.symbols('A')
B=sp.symbols('B')
x=sp.Function('x')
t=sp.symbols('t')

eq=sp.Eq(x(t).diff(t,2)+A*x(t).diff(t)+(B**2)*x(t),F0*sp.cos(Wd*t))
display(eq)

等式1

sol=sp.dsolve(eq,x(t)).rhs
display(sol)

<code> sol </ code>的内容

2) Afterwards I substitute the values of all declared symbols , set the initial conditions so that I can clear the equation 2)之后,我替换所有声明的符号的值,设置初始条件,以便清除方程式

consts = {A:  0.1, 
      B:  0.01,
      F0:  0.0,
      Wd: 0.01,
      }

sol=sp.simplify(sol.subs(consts))
display(sol)

x0=5

#to evaluate initial conditions - x(0)
cnd0=sp.Eq(sol.subs(t,0),x0)
C1 = sp.symbols("C1") 
sol_c1=sp.solve([cnd0],(C1))

display(sol_c1)

C2s=sp.simplify(sol.subs(sol_c1))
display(C2s)

C2S

3) Then I repeat the same process to the first derivate. 3)然后我对第一个导数重复相同的过程。 The point with this is to calculate the values C1 and C2 from x(0) and X'(0). 这样做的目的是根据x(0)和X'(0)计算值C1和C2。 Here goes the code 代码在这里

sold=sp.diff(sol,t)
display(sold)

xd0=0
#to evaluate initial conditions - derivative x'(0)
cnd1=sp.Eq(sold.subs(t,0),xd0)
sold_c1=sp.solve([cnd1],(C1))

display(sold_c1)

C2d=sp.simplify(sol.subs(sold_c1))
display(C2d)

C2D

4) When I try to build the equation with C2s and C2d and solve it in order to finally get an equation where C2 is dependent of t, python throws an error. 4)当我尝试用C2s和C2d建立方程并求解它以便最终得到C2依赖于t的方程时,python抛出错误。 Could you tell me what I am doing wrong? 你能告诉我我在做什么错吗?

Thanks in advance! 提前致谢!

By setting F0 = 0 your differential equation becomes a homogeneous equation. 通过设置F0 = 0您的微分方程变成了齐次方程。 C1 and C2 are constants of integration. C1C2是积分常数 So, I don't think they should be functions of t . 因此,我认为它们不应该是t函数。 The two initial conditions on x(0) and x'(0) give two equations in C1 and C2 which we can solve. x(0)x'(0)上的两个初始条件在C1C2给出了两个方程式,我们可以对其求解。

import sympy as sp
from IPython.display import display
import numpy as np
import matplotlib.pyplot as plt
sp.init_printing()

F0=sp.symbols('F0')
Wd=sp.symbols('W_d')
A=sp.symbols('A')
B=sp.symbols('B')
x=sp.Function('x')
t=sp.symbols('t')

eq=sp.Eq(x(t).diff(t,2)+A*x(t).diff(t)+(B**2)*x(t),F0*sp.cos(Wd*t))
display(eq)

sol=sp.dsolve(eq,x(t)).rhs # x(t)
display(sol)

sold=sp.diff(sol,t) # x'(t)
display(sold)

consts = {A:  0.1,
      B:  0.01,
      F0:  0.0,
      Wd: 0.01,
      }

sol=sp.simplify(sol.subs(consts))
display(sol)

sold=sp.simplify(sold.subs(consts))
display(sold)

x0=5
#to evaluate initial conditions - x(0)
cnd0=sp.Eq(sol.subs(t,0),x0)

xd0=0
#to evaluate initial conditions - derivative x'(0)
cnd1=sp.Eq(sold.subs(t,0),xd0)

c1c2 = sp.linsolve([cnd0,cnd1],sp.var('C1,C2'))
display(c1c2)

Please let me know if I misunderstood anything about your differential equation. 如果我对您的微分方程有误解,请告诉我。

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

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