2014-11-06 117 views
4

我學習阻尼的動態,用二階ODE驅動擺定義像so,具體我預設電臺:Python的解決與四功能二階ODE

d^2Y/dt的^ 2 + C * DY/DT +罪(Y)= A * cos(重量)

import numpy as np 
import matplotlib.pyplot as plt 
from scipy import integrate 


def pendeq(t,y,a,c,w): 
    y0 = -c*y[1] - np.sin(y[0]) + a*np.cos(w*t) 
    y1 = y[1] 
    z = np.array([y0, y1]) 
    return z 

a = 2.1 
c = 1e-4 
w = 0.666667  # driving angular frequency 

t = np.array([0, 5000]) # interval of integration 
y = np.array([0, 0])  # initial conditions 

yp = integrate.quad(pendeq, t[0], t[1], args=(y,a,c,w)) 

這個問題看起來很相似Need help solving a second order non-linear ODE in python,但我得到我是什麼錯誤

Supplied function does not return a valid float. 

做錯了?

+0

它看起來像你解決擺的位置作爲幾個參數的函數,是嗎? – 2014-11-06 01:30:48

+0

是的,這是正確的。 – 2014-11-06 01:31:30

+1

'quad'集成了一個標量函數。它不能求解一個常微分方程。再看一下你鏈接的例子 - 'scipy.integrate.odeint'(不是'quad')被用來生成解決方案。另請參閱http://wiki.scipy.org/Cookbook/CoupledSpringMassSystem – 2014-11-06 02:17:59

回答

3

integrate.quad要求提供的功能(pendeq,在你的情況下)只返回一個浮點數。你的函數正在返回一個數組。

+0

我已經回答了「我在做什麼錯了?」這個問題的部分內容,但我仍然在試圖弄清楚如何正確執行。如果我能弄明白,我會編輯我的答案。在這一點上,它更像是一個物理問題,而不是一個編程問題。 – 2014-11-06 01:34:09