Coding and speeding up a numerical integrator
Question
Write an algorithm that numerically integrates a function over an interval, and explain how you would make it faster or more accurate.
Short answer
Start with the trapezoid or Simpson rule, then refine adaptively. Split into panels and sum weighted function values: trapezoid error is , Simpson for smooth integrands, each evaluations. Optimise by halving the step and reusing previous points until successive estimates agree, Richardson/Romberg extrapolation, adaptive subdivision where the integrand varies, or Gaussian quadrature for smooth functions.
Adapted from Joshi, Denson & Downes, Quant Job Interview Questions and Answers, Q5.1, p.157; question and answer rewritten by SelfMadeQuant.com.
See the hint and the full worked solution in the question bank (free account).
Open the worked solution