Skip to content
Tutorials Center
My learning

Topics

Lesson 16 of 47 ยท Ordinary Differential Equations

Chapter 3 Review: Ordinary Differential Equations

Review chapter 3: initial value problems, Euler, Runge-Kutta, symplectic leapfrog and solve_ivp, with a method comparison table and a ten-question quiz.

You now know the standard time-stepping methods of computational physics and, more importantly, how to choose between them. This review collects the methods in one picture, compares them side by side, and tests your understanding with a long quiz.

A cheat sheet of six cards: Euler, midpoint, RK4, leapfrog and Verlet, solve_ivp, and what to always check
Chapter 3 in one picture: pick the method for the job, then check it.

What you will review

  • First-order systems and initial value problems.
  • Order, cost and stability of Euler, midpoint, RK4 and leapfrog.
  • Using solve_ivp, events and tolerances.

Recap table

Method Order Evaluations per step Energy behavior Use it for
Explicit Euler 1 1 Grows on oscillators Learning, quick sketches
Semi-implicit Euler 1 1 Bounded (symplectic) Simple long mechanical runs
Midpoint (RK2) 2 2 Slow drift Cheap upgrade over Euler
RK4 4 4 Slow drift Smooth, short-to-medium runs
Leapfrog / Verlet 2 1 (reused) Bounded (symplectic) Orbits, oscillators, molecular dynamics
solve_ivp RK45 4(5) 6 Drifts like RK General purpose with error control
solve_ivp LSODA, Radau varies varies Drifts Stiff problems

Lessons: overview, Euler, Runge-Kutta, oscillators and energy, projectile with drag.

A decision guide

  1. Do you need long-term energy behavior (planets, vibrating atoms, many oscillations)? Use leapfrog/Verlet with a fixed step.
  2. Do you need high accuracy over a modest time with smooth forces? Use RK4 or solve_ivp with tight tolerances.
  3. Is the system stiff (fast and slow time scales mixed, or a very small step is forced by stability)? Use an implicit method: solve_ivp(..., method="Radau") or "LSODA".
  4. Do you need to stop at an event (landing, collision)? Use solve_ivp events.
  5. Whatever you chose: check a known limit, a conserved quantity and a halved step.

Putting it together: the pendulum with all methods

The final example runs a pendulum for 20 time units with Euler, RK4, leapfrog and solve_ivp, then compares each method's energy drift on one plot.

Python Energy error of four methods on a pendulum

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Over this run RK4 is still the most accurate of the fixed-step methods, but its error grows steadily with time, while the leapfrog error stays inside a band no matter how long you run. Choose by the length of the run and by what must be conserved.

Chapter review quiz

Quick check ยท 10 questions

of 10 answered You scored / 10 ยท perfect!

  1. 1. A mass-spring system is written x'' = -(k/m) x. In first-order form the state is

    Adding v = x' as an unknown turns one second-order equation into two first-order ones.
  2. 2. Which method has global error proportional to h?

    Euler is first order.
  3. 3. How many times does the error shrink when h is halved for a 4th-order method?

    2^4 = 16.
  4. 4. For dy/dt = -5y, Euler is stable if

    Stability needs |1 - 5h| < 1, so h < 2/5 = 0.4.
  5. 5. Which pair of properties best describes leapfrog?

    Leapfrog is second order and symplectic, hence bounded energy error.
  6. 6. A simulated planet's orbit slowly spirals outward over many orbits. A likely cause is

    A conservative system should not gain energy; the integrator is to blame.
  7. 7. What does an event with terminal = True do in solve_ivp?

    That is how you stop at ground impact or a collision.
  8. 8. Setting the drag coefficient to zero in a projectile code should reproduce

    The no-drag limit is the standard verification test.
  9. 9. Why is RK4 compared per function evaluation and not per step?

    The cost of expensive forces is the number of evaluations.
  10. 10. A stiff problem is best attacked with

    Implicit methods stay stable at steps far larger than explicit methods allow.

Summary

  • Rewrite higher-order ODEs as first-order systems, then choose a stepper.
  • Euler teaches the ideas; RK4 delivers accuracy; leapfrog and Verlet protect energy; solve_ivp gives adaptive control and events.
  • Always verify: known limit, conserved quantity, halved step.
  • Chapter 4 changes the question: what if the system, or the method, is random?

Next: Monte Carlo Methods: Overview