2) Afterwards"/>
  简体   繁体   中英

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. What I've done so far is:

1) inserting the differential equation, assigning the values and solving it:

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

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. The point with this is to calculate the values C1 and C2 from x(0) and X'(0). 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. Could you tell me what I am doing wrong?

Thanks in advance!

By setting F0 = 0 your differential equation becomes a homogeneous equation. C1 and C2 are constants of integration. So, I don't think they should be functions of t . The two initial conditions on x(0) and x'(0) give two equations in C1 and C2 which we can solve.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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