Runge-Kutta integration in Python

时间:2018-06-18 11:37:48

标签: scipy runge-kutta

I need to use the Runge-Kutta integration method to obtain a certain value. The value I'm looking for is defined by:

dψ/dt=(K*δ(t)-ψ(t))/T

where T and K are known floats and I have the arrays of δ called "delta2" and of ψ called "psi", both taken at a number of time steps.

Now, I need to obtain the value of ψ using the Range-Kutta method but my code doesn't seem to work. here it is:

def f(x,t):
     return (K*delta2[index(t)]-x)/T

x0=spl_psi1(0)

def rk4( f, x0, t ):
    n = len( t )
    x = np.array( [ x0 ] * n )

for i in xrange( n - 1 ):
    h = t[i+1] - t[i]
    k1 = h * f( x[i], t[i] )
    k2 = h * f( x[i] + 0.5 * k1, t[i] + 0.5 * h )
    k3 = h * f( x[i] + 0.5 * k2, t[i] + 0.5 * h )
    k4 = h * f( x[i] + k3, t[i+1] )
    x[i+1] = x[i] + ( k1 + 2.0 * ( k2 + k3 ) + k4 ) / 6.0
return x 

0 个答案:

没有答案