Solve first-order ODEs numerically using RK4 given dy/dx = f(x,y) and an initial condition.
Solves the initial value problem dy/dx = f(x,y), y(x₀) = y₀ using the classical 4th-order Runge-Kutta method (RK4). This method has local error O(h⁵) and global error O(h⁴), making it highly accurate for smooth ODEs. Enter f(x,y) using x and y as variables.
RK4 method
y(n+1) = y(n) + (h/6)(k1 + 2k2 + 2k3 + k4)
More steps give higher accuracy but take longer. For most problems, 100-1000 steps suffice. If the solution diverges, try more steps (smaller h).
Convert a second-order ODE y'' = g(x,y,y') into a system of first-order ODEs by substituting v = y', then solve iteratively.