Runge–Kutta of 4th order

621 views Asked by At

Lets consider I have a system of 4 ODE: dX/dt = F(X), where X is a vector(4-demension) and F: R^4 -> R^4. F is called vectorDE_total_function, and I'm trying to calculate the solution using RK-4.

def solvingDES():
    previous_vector = np.array ([theta_1, omega_1, theta_2, omega_2]);
    for current_time in time:
        temp_vector = previous_vector;
        RK_vector = np.array([0.0,0.0,0.0,0.0]);
        for c in [6,3,3,6]:
            RK_vector = vectorDE_total_function(previous_vector + c * RK_vector/6) * time_step;
            temp_vector += RK_vector / c;
        previous_vector = temp_vector;
        current_time += 1;

And it looks like I'm wrong somewhere, but I'm not sure where. Is it seems legit?

1

There are 1 answers

7
Lutz Lehmann On BEST ANSWER

It is a strange, seldom seen way to implement it and it only works for classical RK4, other Runge-Kutta methods would not work like that. But the general idea seems correct.

You have a common error in an usually unexpected place. Setting

temp_vector = previous_vector;

and later

previous_vector = temp_vector;

you do not make a copy of the vectors, but make both object references share the same vectors. Use

temp_vector = previous_vector.copy();

or

previous_vector = temp_vector[:];

to force copying of the vector data.