Home > Python > Ordinary Differential Equation-2

Ch. 12.1 1st Order ODE (RK2 Method)

Chapters Index
Program 1

1st Order ODE (RK2 Method)

Python 3.11 Ready

                                
Click "Run Code" to execute script and render figures.

Mathematical Problem Formulation

An object in free fall under the influence of gravity, with constant acceleration: \(a = 9.81 \, \text{m/s}^2\)

$$ \frac{dv}{dt}=a ;\quad v(t=0)=0 $$

The following code implements the Runge-Kutta 2nd order (RK2) method to calculate the velocity of the object over a time period.

Theoretical Background & Explanation

RK2 Method

In general, the RK2 method for solving an ODE of the form \( \frac{dy}{dt} = f(t, y) \) over a time interval \([t_0, t_{\text{end}}]\) with initial condition \( y(t_0) = y_0 \) is implemented as follows:

  1. Compute \( k_1 \), an estimate of the slope at the beginning of the interval: \[ k_1 = f(t, y) \]
  2. Compute \( k_2 \), an estimate of the slope at the end of the interval using \( k_1 \): \[ k_2 = f\left(t + h, y + h \cdot k_1\right) \]
  3. Update the value of \( y \) for the next step \( y_{\text{next}} \) by averaging these slopes: \[ y_{\text{next}} = y + \frac{h}{2} \cdot (k_1 + k_2) \]