Python runs in your browser. Heavy or infinite loops may freeze your browser tab. Use "Stop" if needed; for heavy jobs, run locally.
Program 1
1st Order ODE (RK2 Method)
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.
Output 1
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:
- Compute \( k_1 \), an estimate of the slope at the beginning of the interval: \[ k_1 = f(t, y) \]
- 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) \]
- 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) \]