from pycppad import *
def pycppad_test_runge_kutta_4_correct() :
def fun(t , y) :
n = y.size
f = numpy.zeros(n)
f[0] = 1.
index = numpy.array( range(n-1) ) + 1
f[index] = (index + 1) * y[index-1]
return f
n = 5 # size of y(t) (order of method plus 1)
ti = 0. # initial time
dt = 2. # a very large time step size to test correctness
yi = numpy.zeros(n) # initial value for y(t); i.e., y(0)
# take one 4-th order Runge-Kutta integration step of size dt
yf = runge_kutta_4(fun, ti, yi, dt)
# check the results
t_jp = 1. # t^0 at t = dt
for j in range(n-1) :
t_jp = t_jp * dt # t^(j+1) at t = dt
assert abs( yf[j] - t_jp ) < 1e-10 # check yf[j] = t^(j+1)