In this tutorial, we will learn how to implement the **Runge-Kutta** method in Python for solving Ordinary Differential Equations (ODEs).

The Runge-Kutta method is a widely used numerical method for solving ODEs providing a high level of accuracy while reducing computation time.

We will implement the fourth-order Runge-Kutta, also known as RK4, which is one of the most popular versions of this method.

### Step 1: Define the Differential Equation

Before programming the Runge-Kutta algorithm in Python, you need to define the differential equation to be solved. This equation will be used as the input to the algorithm. You can define the function using the `def`

keyword in Python. In this tutorial, we will solve the following first-order ODE:

1 |
dy/dt = -k * y |

Here is how you can define the above differential equation in Python:

1 2 |
def dydt(t, y, k): return -k*y |

### Step 2: Implement the Runge-Kutta Algorithm

Now we implement the Runge-Kutta algorithm in Python. In the below code, `runge_kutta`

accepts a differential equation function, initial conditions, step size, and number of steps as inputs, and returns a list of approximated solutions for each time step.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
def runge_kutta(f, t0, y0, h, n, k): t = t0 y = y0 solutions = [(t, y)] for i in range(n): k1 = f(t, y, k) k2 = f(t + h/2, y + h*k1/2, k) k3 = f(t + h/2, y + h*k2/2, k) k4 = f(t + h, y + h*k3, k) y = y + h * (k1 + 2*k2 + 2*k3 + k4) / 6 t = t + h solutions.append((t, y)) return solutions |

### Step 3: Set up Initial Conditions and Parameters

To use the Runge-Kutta algorithm, you need to set up the initial conditions and parameters for the differential equation. For this example, let’s consider the following initial conditions and parameters:

1 2 3 4 5 |
t0 = 0 # Initial time y0 = 1 # Initial value k = 0.5 # Constant h = 0.1 # Time step n = 40 # Number of steps |

### Step 4: Run the Algorithm and Display Results

With everything set up, now all you have to do is call the `runge_kutta`

function and display the results.

1 2 3 4 |
results = runge_kutta(dydt, t0, y0, h, n, k) for t, y in results: print(f"t: {t:.2f}, y: {y:.6f}") |

### Full Code

Here is the complete code for implementing the Runge-Kutta method in Python:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
def dydt(t, y, k): return -k*y def runge_kutta(f, t0, y0, h, n, k): t = t0 y = y0 solutions = [(t, y)] for i in range(n): k1 = f(t, y, k) k2 = f(t + h/2, y + h*k1/2, k) k3 = f(t + h/2, y + h*k2/2, k) k4 = f(t + h, y + h*k3, k) y = y + h * (k1 + 2*k2 + 2*k3 + k4) / 6 t = t + h solutions.append((t, y)) return solutions t0 = 0 y0 = 1 k = 0.5 h = 0.1 n = 40 results = runge_kutta(dydt, t0, y0, h, n, k) for t, y in results: print(f"t: {t:.2f}, y: {y:.6f}") |

### Output

t: 0.00, y: 1.000000 t: 0.10, y: 0.951267 t: 0.20, y: 0.904837 t: 0.30, y: 0.860708 t: 0.40, y: 0.818731 t: 0.50, y: 0.778801 t: 0.60, y: 0.740818 t: 0.70, y: 0.704688 t: 0.80, y: 0.670320 t: 0.90, y: 0.637628 t: 1.00, y: 0.606531 t: 1.10, y: 0.576950 t: 1.20, y: 0.548811 t: 1.30, y: 0.522043 t: 1.40, y: 0.496585 t: 1.50, y: 0.472377 t: 1.60, y: 0.449295

### Conclusion

In this tutorial, we have successfully implemented the Runge-Kutta method in Python to solve a first-order ODE. With a good understanding of the algorithm, you can now adapt it to solve other types of ODEs or higher-order ODEs.

Remember that the Runge-Kutta method is useful for approximating solutions of ODEs with high accuracy and reduced computational effort, making it an essential tool in the physics, engineering, and applied mathematics fields.