我是Python中的初級/中級。我已經編寫了一個4th-order Runge-Kutta method (RK4)到Python。它基本上解決了擺錘,但這不是重點。我希望能夠將函數f直接傳遞給RK4函數,即RK4(y_0,n,h)應該成爲RK4(f,y_0,n) ,H)。這可以帶來很大的好處,我可以將RK4用於描述其他系統的其他f函數,而不僅僅是這一個鐘擺。如何將函數傳遞給Python中的函數?
我曾經玩過簡單的功能到RK4,但我做錯了什麼。我如何在Python中做到這一點?
import numpy as np
def RK4(y_0, n, h):
#4th order Runge-Kutta solver, takes as input
#initial value y_0, the number of steps n and stepsize h
#returns solution vector y and time vector t
#right now function f is defined below
t = np.linspace(0,n*h,n,endpoint = False) #create time vector t
y = np.zeros((n,len(y_0))) #create solution vector y
y[0] = y_0 #assign initial value to first position in y
for i in range(0,n-1):
#compute Runge-Kutta weights k_1 till k_4
k_1 = f(t[i],y[i])
k_2 = f(t[i] + 0.5*h, y[i] + 0.5*h*k_1)
k_3 = f(t[i] + 0.5*h, y[i] + 0.5*h*k_2)
k_4 = f(t[i] + 0.5*h, y[i] + h*k_3)
#compute next y
y[i+1] = y[i] + h/6. * (k_1 + 2.*k_2 + 2.*k_3 + k_4)
return t,y
def f(t,vec):
theta=vec[0]
omega = vec[1]
omegaDot = -np.sin(theta) - omega + np.cos(t)
result = np.array([omega,omegaDot])
return result
test = np.array([0,0.5])
t,y = RK4(test,10,0.1)
感謝您的澄清。我來自MatLab(與一些C)。所以Python不停地驚訝於:-)。 – seb 2013-03-21 10:57:29