Interpolation · grids and surfaces
Two-Dimensional Interpolation (interp2)
Two-dimensional interpolation estimates a value at a point (x, y) that lies between the nodes of a rectangular grid, given the function values at the nodes. A routine like interp2 receives the x coordinates of the grid columns, the y coordinates of the rows, a matrix of values Z, and one or more query points. It returns estimates of the surface height at those points.
The method generalizes the one-dimensional case: instead of an interval, the query falls into a rectangular cell with four corners, and the estimate blends their values.
Finding the cell
First locate the column interval [x_i, x_{i+1}] containing x, and the row interval [y_j, y_{j+1}] containing y. Each is a one-dimensional search. Then compute the fractional positions inside the cell, s = (x − x_i)/(x_{i+1} − x_i) and t = (y − y_j)/(y_{j+1} − y_j), both between 0 and 1.
Orientation is the classic trap. In most matrix conventions, Z(j, i) has the row index tied to y and the column index tied to x, so the matrix looks transposed against the picture of the grid. When results come out mirrored or are off by a bad amount, check this before anything else. The page on elements of a matrix explains how row and column indexing work.
Bilinear interpolation
Bilinear interpolation interpolates along x on both horizontal edges of the cell, then along y between those two results. In terms of the corner values it is
z = (1−s)(1−t)·z₀₀ + s(1−t)·z₁₀ + (1−s)t·z₀₁ + st·z₁₁
where z₁₀ is the corner at the larger x and the smaller y. The four weights are non-negative and add to 1, so the estimate never leaves the range of the corner values.
A worked cell: let the corners be z₀₀ = 1, z₁₀ = 3, z₀₁ = 2 and z₁₁ = 6 on the unit square, and query the point s = 0.25, t = 0.5. Interpolating along the bottom edge gives 1 + 0.25 × (3 − 1) = 1.5. Along the top edge it gives 2 + 0.25 × (6 − 2) = 3. Then between them, 1.5 + 0.5 × (3 − 1.5) = 2.25.
The weight form gives the same number. The four weights are (1−s)(1−t) = 0.375, s(1−t) = 0.125, (1−s)t = 0.375 and st = 0.125, which add to 1. The sum is 0.375 × 1 + 0.125 × 3 + 0.375 × 2 + 0.125 × 6 = 0.375 + 0.375 + 0.75 + 0.75 = 2.25.
These corner values come from the function 1 + 2x + y + 2xy, and its value at (0.25, 0.5) is 1 + 0.5 + 0.5 + 0.25 = 2.25 as well. That is no accident: bilinear interpolation reproduces any function of the form a + bx + cy + dxy exactly, which makes such functions good test data.
What the surface looks like
Along every grid line the surface is a straight segment, but across a cell it is curved: the xy term makes it a saddle. The estimate is continuous across cell edges, while the slope jumps at them. On a contour plot the contour lines show small kinks at the grid lines, a sign that the surface came from bilinear blending.
Smoother alternatives
Bicubic interpolation uses a 4 by 4 neighborhood of nodes and matches slopes across cell edges, which gives a smooth surface at the cost of overshoot near sharp features. It can return values outside the range of the corner values, which is wrong for quantities that must stay positive, such as concentrations. Nearest-neighbor interpolation returns the closest node’s value and is the right choice for category labels and for masks.
Edges and bad inputs
A query outside the grid has no surrounding cell. Return an error or a missing marker, or extend the edge cell, and make the choice visible to the caller. Grid coordinates must be strictly increasing; duplicates divide by zero in the fractions s and t.
For non-rectangular data, such as scattered measurements, the grid method does not apply directly. The data are first triangulated, or resampled onto a grid by a different technique.
Where it shows up
Resizing an image is two-dimensional interpolation: each output pixel maps to a position between input pixels, and the four neighbors are blended. The image processing overview places that operation among its neighbors. Smoothing the result with a kernel is a two-dimensional convolution.
Numerical integration over such surfaces is the subject of two-dimensional Gaussian quadrature, and the surrounding methods are collected under interpolation and calculus.