# Runge-Kutta 4th Order Method | Python

Python Programming
Basics
Numerical Methods

The Runge-Kutta 4th order method (RK4) is a widely used technique for solving ordinary differential equations (ODEs).

It is a more accurate and efficient method compared to the 2nd order Runge-Kutta method, as it uses four estimates of the derivative at different points to calculate the next solution estimate.

Here is the code of a Python program that implements the Runge-Kutta 4th order method for a given ODE:

## Runge-Kutta 4th Order Method in Python

				
import math

def rk4_solver(f, y0, t0, tf, h):
# Runge-Kutta 4th Order Method [By Bottom Science]
t = t0
y = y0
while t <= tf:
k1 = h * f(t, y)
k2 = h * f(t + h/2, y + k1/2)
k3 = h * f(t + h/2, y + k2/2)
k4 = h * f(t + h, y + k3)
y = y + (k1 + 2*k2 + 2*k3 + k4) / 6
t = t + h
return y

import numpy as np

def f(t, y):
return math.sin(t) - y

y0 = 0
t0 = 0
tf = 10
h = 0.1

y_sol = rk4_solver(f, y0, t0, tf, h)

print("Solution at t = ", tf, " is: ", y_sol)

# OUTPUT - Solution at t =  10  is:  0.07776903758337268