更新时间:2021-07-25 16:12:36
dsolve()
中的参数ics
不能真正起作用(问题4720 ),因此您必须手动进行替换.您可以尝试:
The Parameter ics
in dsolve()
does not really work (Issue 4720), so you have to do the substitutions manually. You could try:
from IPython.display import display
import sympy as sy
sy.init_printing() # LaTeX-like pretty printing for IPython
t = sy.Symbol("t", real=True)
m, k = sy.symbols('m k', real=True) # gives C_1 Exp() + C_2 Exp() solution
# m, k = sy.symbols('m k', positive=True) # gives C_1 sin() + C_2 cos() sol.
a0, b0 = sy.symbols('a0, b0', real=True)
y = sy.Function('y')
Eq1 = sy.Eq(m*sy.diff(y(t), t, 2) + k*y(t))
print("ODE:")
display(Eq1)
print("Generic solution:")
y_sl0 = sy.dsolve(Eq1, y(t)).rhs # take only right hand side
display(sy.Eq(y(t), y_sl0))
# Initial conditions:
cnd0 = sy.Eq(y_sl0.subs(t, 0), a0) # y(0) = a0
cnd1 = sy.Eq(y_sl0.diff(t).subs(t, 0), b0) # y'(0) = b0
# Solve for C1, C2:
C1, C2 = sy.symbols("C1, C2") # generic constants
C1C2_sl = sy.solve([cnd0, cnd1], (C1, C2))
# Substitute back into solution:
y_sl1 = sy.simplify(y_sl0.subs(C1C2_sl))
print("Solution with initial conditions:")
display(sy.Eq(y(t), y_sl1))