2017-04-23 181 views
0

對於微分方程mx'' + kx = 0(其中x''x相對於t的雙重導數),如何解決這個問題x(t)?我的意思是如何得到這個公式:如何求解二階微分方程?

x(t) = c1*cos(sqrt(k/m)*t) + c2*sin(sqrt(k/m)*t) 

我試了一下:

t, g, k, m, w0, a_0, b_0, c1, c2 = symbols('t g k m w0 a_0 b_0 c1 c2') 
x = symbols('x', cls=Function) 
w0 = sqrt(k/m) 
diffeq = Eq(x(t).diff(t, t) + k*x, 0) 

但聲明diffeq = Eq(x(t).diff(t, t) + k*x, 0)拋出一個錯誤:

TypeError: unbound method as_base_exp() must be called with x instance as first argument (got nothing instead) 
+2

你試過[dsolve的'第一個例子()'](http://docs.sympy.org/dev/modules/solvers/ode.html#dsolve)? – kennytm

+0

@kennytm;我編輯了這個問題。 – haccks

+0

@kennytm;好的,我弄錯了。我想出瞭如何去做。 – haccks

回答

0

我做了工作,並能解決方程爲係數C1C2。下面是代碼:

t, a0, b0, k, m, g = symbols('t a0 b0 k m g') 
x = Function('f') 
diffeq = m*Derivative(x(t), t, t) + k*x(t) + m*g 
# print(diffeq) 
x_sl30 = dsolve(diffeq, x(t)).rhs 
print(x_sl30) 

# Initial condition, x(0) = a0 and x'(0) = b0 
c_0 = Eq(x_sl30.subs(t, 0), a0) 
c_1 = Eq(x_sl30.diff(t).subs(t, 0), b0) 
# print(c_0) 
# print(c_1) 
C1, C2 = symbols("C1, C2") 
C1C2_sl = solve([c_0, c_1], (C1, C2)) 
#Substitute the value of C1 and C2 in the original equation 
x_sl31 = x_sl30.subs(C1C2_sl) 
print(x_sl31)