Companion Website

Computational Physics

From Equations to Simulation

Chapter 9

Initial Value Problems for Ordinary Differential Equations

These six programs introduce fixed-step methods, adaptive Runge–Kutta–Fehlberg integration (RKF45), and velocity Verlet for particle dynamics. They progress from a single first-order equation to systems, oscillations, projectile motion, and a gravitational orbit. Each program is available in MATLAB, Python, C++, and Fortran. Initial data and parameters appear in the driver, followed by the equations and numerical method. Change the supplied equations and initial conditions to study another mathematical or physical model.

All programs print and save the initial point and subsequent solution points; adaptive programs save accepted points only. MATLAB and Python also plot the solutions. The supplied examples include analytic comparisons. Update or disable these comparisons when changing the model or initial data. Use each program’s documented example when checking its output; some inputs differ from the book’s printed examples.

RKF45 controls an estimate of local error, so its tolerance is not a bound on accumulated global error. The system programs apply a single absolute tolerance to all components, which is appropriate when the components have comparable magnitudes. The spring and projectile programs also provide h_max to limit the spacing of output points for plotting. Velocity Verlet uses a fixed step and assumes the force depends only on position; velocity-dependent forces such as drag require a different method. Further guidance, including libraries for stiff problems, is on the Chapter 9 resources page.

Program MATLAB Python C++ Fortran
0. Single first-order ODE: fixed steps Compares Euler, modified Euler (Heun), and classical RK4 on a smooth equation with an analytic solution. Download Download Download Download
1. Single first-order ODE: adaptive steps Uses RKF45 for a test equation with rapid variation; reports accepted steps and rejected trials. Download Download Download Download
2. System of first-order ODEs Solves n coupled equations with adaptive RKF45; the mathematical example has four components and an analytic solution. Download Download Download Download
3. Single second-order ODE: damped spring Writes position and velocity as two first-order equations; uses adaptive RKF45 and an underdamped analytic comparison. Download Download Download Download
4. Two second-order ODEs: projectile motion Solves four position and velocity equations with RKF45; the default case is gravity only, with an analytic comparison, and quadratic air resistance can be switched on. Download Download Download Download
5. Particle dynamics: gravitational orbit Uses fixed-step velocity Verlet for a particle orbiting a fixed central mass, with a circular-orbit analytic comparison. Download Download Download Download

← Back to Chapter 9