Pictures for the Shelf

Pictures of numerical methods

The pictures for numerical analysis (roadmap 13.3): Newton's tangents, interpolation and Runge's phenomenon, the pieces of a quadrature rule, stability regions, and observed orders of convergence.

Newton's method

Newton's step replaces ff by its tangent at xkx_k and goes to where the tangent meets the axis: xk+1=xk−f(xk)/f′(xk)x_{k+1} = x_k - f(x_k)/f'(x_k). newtonFigure(f, x0, n) draws each tangent; near a simple root the error is squared at each step, so the tangents crowd together fast.

Interpolation

Through nn points there is one polynomial of degree below nn. interpolationFigure(f, n) builds it by divided differences and draws ff, the interpolant and the error. For f(x)=1/(1+25x2)f(x) = 1/(1 + 25x^2) on equally spaced points the error near the ends grows with nn (Runge's phenomenon); with nodes: "chebyshev", the points cos⁡(2i+1)π2n\cos\frac{(2i+1)\pi}{2n}, it shrinks.

Quadrature

quadratureFigure(f, [a, b], n, { rule }) shades what the rule integrates: rectangles for the midpoint rule, trapezoids, or for Simpson's rule a parabola through each pair of panels, which integrates cubics exactly.

Stability regions

A one-step method applied to y′=λyy' = \lambda y multiplies yy by R(hλ)R(h\lambda) at each step: R(z)=1+zR(z) = 1 + z for Euler, 1+z+z2/21 + z + z^2/2 for Heun, ∑k≤4zk/k!\sum_{k \le 4} z^k/k! for the classical Runge–Kutta method. The solution stays bounded when ∣R(z)∣≤1|R(z)| \le 1. stabilityRegion(R) shades that set; Euler's is the disc of radius 1 about −1-1.

Orders of convergence

If the error behaves like ChpC h^p, then log⁡E=log⁡C+plog⁡h\log E = \log C + p \log h: a line of slope pp on log–log axes. convergenceFigure(rows) plots (h,E)(h, E) and fits the slope, the observed order.

Try itEdit the code and run it with @ratmath/graphics and Core's Rational in scope. The last line's value is shown; PRINT(…) adds lines, TEX(…) typesets math, and SHOW(svg) draws a picture.
Ctrl/⌘ + Enter runs