Roots, interpolation, quadrature, ODEs & linear systems
Six self-contained experiments, each a genuinely working numerical engine written in plain JavaScript — no libraries. You type a real function f(x) (parsed by a hand-rolled tokenizer and recursive-descent evaluator) and every control drives the computation: iterations animate, convergence and error are tabulated per step, and the exact value is compared against the numerical estimate. Pick an experiment on the left, then move through Aim, Theory, Procedure, the live Simulation, a graded Self-assessment, and References.
1 · Root finding
Bisection brackets a root between a and b with f(a)·f(b) < 0 and halves the interval each step, keeping the half that still changes sign. It always converges (linearly), gaining one bit per iteration: the error bound is (b−a)/2 to the power n.
Newton-Raphson uses the tangent line. From the Taylor expansion, the next iterate is the x-intercept of the tangent at the current point. It converges quadratically near a simple root but needs the derivative and can diverge.
The secant method replaces f' by a finite-difference slope through the last two points, giving superlinear order about 1.618 without needing a derivative. False position (regula falsi) uses the same secant formula but, like bisection, keeps a sign-changing bracket, so it is guaranteed to converge.
The approximate relative error reported each step is |x(n+1) − x(n)| / |x(n+1)|; iteration stops when it falls below the tolerance or the iteration cap is reached.
- Open Simulation. A default function and bracket are loaded.
- Edit f(x) (for example
x^3 - x - 2,cos(x) - x,exp(-x) - x). The plot redraws live; a red box means the expression failed to parse. - Choose a method. For bracketing methods set a and b so the curve crosses between them; for open methods set the start point(s).
- Set the tolerance and max iterations, then press Run. Watch the iterate markers march toward the root.
- Read the per-step table: the current estimate, f(x) and the approximate error. Note how Newton roughly doubles the number of correct digits each step while bisection halves the bracket.
- Press Step to advance one iteration at a time, or Reset to clear.
Function & iteratesready
^ for powers, and functions sin cos tan exp log sqrt abs. Constants pi and e are available. The dashed grey line is y = 0.Controls
| n | a | b | x | f(x) | err |
|---|
- Burden & Faires — Numerical Analysis, 9th ed., Ch. 2 (Solutions of Equations in One Variable). Brooks/Cole.
- Atkinson — An Introduction to Numerical Analysis, 2nd ed., Sec. 2. Wiley.
- Virtual Labs (IIT) — Numerical Analysis: Root Finding, nalwss-coep.vlabs.ac.in.
2 · Polynomial interpolation
Given n+1 distinct points there is exactly one polynomial of degree at most n passing through all of them. The Lagrange form writes it as a weighted sum of basis polynomials, each of which is one at its own node and zero at every other node.
The Newton divided-difference form builds the same polynomial incrementally, so adding a point costs only one extra term. The coefficients are the leading divided differences computed from a triangular table.
f[x(i),...,x(i+k)] = ( f[x(i+1),...] − f[x(i),...] ) / ( x(i+k) − x(i) )
Both are exact at the nodes; between them they can oscillate (Runge phenomenon) for high degree on equally spaced data. The interpolation error involves the (n+1)-th derivative of the true function and the node product.
- Open Simulation. Several points are preloaded and the interpolating polynomial is drawn through them.
- Click on the canvas to add a point, or use x, y and Add point. Use Random or Clear to reshape the data set.
- Toggle Lagrange and Newton; both curves should coincide exactly — they are the same polynomial in two algebraic dresses.
- Enter a query x and read the interpolated value; a vertical marker shows it on the curve.
- Read the printed divided-difference table — its top row gives the Newton coefficients.
Interpolant0 points
Data & query
- Burden & Faires — Numerical Analysis, 9th ed., Ch. 3 (Interpolation and Polynomial Approximation). Brooks/Cole.
- Hildebrand — Introduction to Numerical Analysis, 2nd ed., Ch. 2. Dover.
- Virtual Labs (IIT) — Numerical Analysis: Interpolation, nalwss-coep.vlabs.ac.in.
3 · Numerical integration
A Newton-Cotes rule replaces f by an interpolating polynomial on each panel and integrates that exactly. With h = (b−a)/n the composite trapezoidal rule joins consecutive points by straight lines:
Simpson's 1/3 rule fits a parabola over each pair of panels (so n must be even) and is exact for cubics:
Simpson's 3/8 rule fits a cubic over each group of three panels (n a multiple of 3):
The reported error is the absolute difference from a high-resolution reference value (Simpson with many points), so you can watch the trapezoidal error fall like h squared while Simpson falls like h to the fourth.
- Open Simulation. The default integrand and limits are loaded and the panels are shaded.
- Edit f(x), the limits a and b, and the number of subintervals n with the slider.
- Pick a rule. For Simpson 1/3, n is rounded up to an even number; for 3/8, to a multiple of three (the panel says which).
- Read the estimate, the reference value and the absolute error.
- Increase n and watch the error drop — the panel reports the empirical order of accuracy as you do.
Quadrature panelsready
Controls
- Burden & Faires — Numerical Analysis, 9th ed., Ch. 4 (Numerical Differentiation and Integration). Brooks/Cole.
- Davis & Rabinowitz — Methods of Numerical Integration, 2nd ed. Academic Press.
- Virtual Labs (IIT) — Numerical Analysis: Numerical Integration, nalwss-coep.vlabs.ac.in.
4 · ODE solvers
For dy/dx = f(x,y) with y(x0) = y0, a one-step method advances by h. Euler's method follows the slope at the start of the step — it is first order, so halving h roughly halves the error.
Heun's method (improved Euler / RK2) averages the slope at the start with a predicted slope at the end, giving second order:
y(n+1) = y(n) + (h/2)(k1 + k2)
Classical RK4 takes a weighted average of four slope samples and is fourth order — halving h cuts the error by sixteen:
k3 = f(x+h/2, y+h·k2/2), k4 = f(x+h, y+h·k3)
y(n+1) = y(n) + (h/6)(k1 + 2k2 + 2k3 + k4)
The panel reports the maximum absolute error against the analytic solution; choose a preset problem so the exact curve is known.
- Open Simulation and pick a preset ODE — each comes with its analytic solution.
- Set the initial point x0, y0, the end x and the step h (or the number of steps).
- Enable the methods you want to compare. The analytic curve is the dashed green line; numerical solutions are coloured polylines.
- Read each method's max error. Halve h and confirm Euler error halves, Heun quarters, RK4 drops about sixteen-fold.
- Try a larger h to see Euler drift away while RK4 still tracks the true curve.
Solution curvesready
Controls
- Burden & Faires — Numerical Analysis, 9th ed., Ch. 5 (Initial-Value Problems for ODEs). Brooks/Cole.
- Hairer, Norsett & Wanner — Solving Ordinary Differential Equations I, 2nd ed. Springer.
- Virtual Labs (IIT) — Numerical Analysis: ODE Solvers, nalwss-coep.vlabs.ac.in.
5 · Linear systems
Gaussian elimination uses row operations to reduce A to upper-triangular form, then back-substitutes. Partial pivoting swaps in the row with the largest pivot magnitude at each column to limit round-off growth and avoid division by a tiny pivot.
LU decomposition records those multipliers in a unit lower-triangular L and the result in an upper-triangular U so that PA = LU. Solving then needs only a forward solve Ly = Pb and a back solve Ux = y — cheap to repeat for many right-hand sides.
Gauss-Seidel is iterative: it updates each unknown in place using the most recent values of the others. It converges when A is diagonally dominant; the spectral radius of the iteration matrix governs the rate.
The residual norm ||Ax − b|| measures how well the current solution satisfies the system.
- Open Simulation. A 3×3 system is preloaded; edit any entry of A or b.
- Choose size (2, 3 or 4) and use Random for a fresh diagonally dominant system, or Reset.
- Press Gauss to run elimination with partial pivoting — each pivot and row operation is logged and the triangular matrix is shown.
- Press LU to print L and U and the permutation; verify the solution matches Gauss.
- Press Gauss-Seidel to iterate; watch the iterates and the residual fall (the badge warns if the matrix is not diagonally dominant).
Working & stepsready
Solution
- Golub & Van Loan — Matrix Computations, 4th ed., Ch. 3 (General Linear Systems). Johns Hopkins.
- Burden & Faires — Numerical Analysis, 9th ed., Ch. 6 & 7. Brooks/Cole.
- Virtual Labs (IIT) — Numerical Analysis: Systems of Linear Equations, nalwss-coep.vlabs.ac.in.
6 · Least-squares curve fitting
Least squares chooses coefficients that minimise the sum of squared vertical residuals between the model and the data. For a degree-m polynomial the model is a linear combination of the powers of x, so minimising leads to the normal equations — a square system in the unknown coefficients.
normal equations: (X transpose X) c = X transpose y
Here X is the Vandermonde-style design matrix whose columns are 1, x, x squared, and so on. For a straight line this reduces to the familiar slope and intercept formulas.
Goodness of fit is summarised by R squared = 1 − SS_res / SS_tot, which is one for a perfect fit and falls toward zero as the model explains less of the variance. Raising the degree always lowers SS_res but risks over-fitting the noise.
- Open Simulation. A scatter of noisy points is preloaded and a fit is drawn through it.
- Click to add points, or press Generate to sample a known curve plus noise; use Clear to start over.
- Set the degree (1 = straight line). The fitted curve and its equation update immediately.
- Read the printed coefficients, the residual sum of squares and R squared.
- Raise the degree and watch R squared climb toward one while the curve starts to chase individual points — the signature of over-fitting.
Scatter & fit0 points
Fit controls
- Lawson & Hanson — Solving Least Squares Problems. SIAM Classics.
- Burden & Faires — Numerical Analysis, 9th ed., Sec. 8.1 (Discrete Least Squares Approximation). Brooks/Cole.
- Virtual Labs (IIT) — Numerical Analysis: Curve Fitting, nalwss-coep.vlabs.ac.in.