Skip to content

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 [a,b][a,b] into nn panels and sum weighted function values: trapezoid error is O(h2)O(h^2), Simpson O(h4)O(h^4) for smooth integrands, each O(n)O(n) 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

Lessons behind this question