Qu

interp1

Builtin index · Signal processing

result = interp1(x, y, xq, [method="linear"|"spline"|"nearest"])

Input: x, y, xq, method · Output: result

What it is

x, y: matching length-N vectors (the known points, x strictly increasing); xq: a number or length-M vector, the query point(s); method (optional string). 1-D interpolation through (x, y) (MATLAB's interp1). Extrapolation beyond x's range is always on — "linear" continues the boundary segment's own slope, "spline" extends the natural cubic spline's boundary segment, "nearest" just keeps returning the nearest endpoint's y — never a clamp or an error, matching what "interpolation *and* extrapolation" implies.

Overloads

The method string picks between three genuinely different curve shapes through the same six points — worth plotting side by side rather than trusting the names alone.

xq can be number or vector.

3 forms. Each carries its own inputs, outputs and worked example — pick the tab for the one you are calling.

CodeInputOutputDescription
result = interp1(x, y, xq, [method="linear"|"spline"|"nearest"])
x vectorx, y: matching length-N vectors (the known points, x strictly increasing).
y vectorx, y: matching length-N vectors (the known points, x strictly increasing).
xq number or vectorxq: a number or length-M vector, the query point(s).
method string, optional (default "linear"|"spline"|"nearest")method (optional string).
result number or vector
Straight segments between consecutive known points. Look for sharp corners at each of the six original samples (drawn as a separate scatter).

Examples

Written for this form and run by the build -- the output below is what the engine actually produced.

x = [0, 2, 4, 6, 8, 10]
y = [0, 1.8, 1.2, 3.5, 3.0, 4.8]
xq = 0 to 10 step 0.1
plot(xq, interp1(x, y, xq, "linear"))
scatter(x, y)
output
· plot: figure 43 (101 points)
· scatter: figure 44 (6 points)
-1 0 1 2 3 4 5 0 1 2 3 4 5 6 7 8 9 10 x=0, y=0 x=2, y=1.8 x=4, y=1.2 x=6, y=3.5 x=8, y=3 x=10, y=4.8 -1 0 1 2 3 4 5 0 1 2 3 4 5 6 7 8 9 10 x=0, y=0 x=2, y=1.8 x=4, y=1.2 x=6, y=3.5 x=8, y=3 x=10, y=4.8
CodeInputOutputDescription
result = interp1(x, y, xq, [method="linear"|"spline"|"nearest"])
x vectorx, y: matching length-N vectors (the known points, x strictly increasing).
y vectorx, y: matching length-N vectors (the known points, x strictly increasing).
xq number or vectorxq: a number or length-M vector, the query point(s).
method string, optional (default "linear"|"spline"|"nearest")method (optional string).
result number or vector
A natural cubic spline through the same points. Look for a smooth curve with no corners, that can overshoot slightly between points the straight segments above would not.

Examples

Written for this form and run by the build -- the output below is what the engine actually produced.

x = [0, 2, 4, 6, 8, 10]
y = [0, 1.8, 1.2, 3.5, 3.0, 4.8]
xq = 0 to 10 step 0.1
plot(xq, interp1(x, y, xq, "spline"))
scatter(x, y)
output
· plot: figure 45 (101 points)
· scatter: figure 46 (6 points)
-1 0 1 2 3 4 5 0 1 2 3 4 5 6 7 8 9 10 x=0, y=0 x=2, y=1.8 x=4, y=1.2 x=6, y=3.5 x=8, y=3 x=10, y=4.8 x=0, y=0 x=2, y=1.8 x=4, y=1.2 x=6, y=3.5 x=8, y=3 x=10, y=4.8 -1 0 1 2 3 4 5 0 1 2 3 4 5 6 7 8 9 10 x=0, y=0 x=2, y=1.8 x=4, y=1.2 x=6, y=3.5 x=8, y=3 x=10, y=4.8 x=0, y=0 x=2, y=1.8 x=4, y=1.2 x=6, y=3.5 x=8, y=3 x=10, y=4.8
CodeInputOutputDescription
result = interp1(x, y, xq, [method="linear"|"spline"|"nearest"])
x vectorx, y: matching length-N vectors (the known points, x strictly increasing).
y vectorx, y: matching length-N vectors (the known points, x strictly increasing).
xq number or vectorxq: a number or length-M vector, the query point(s).
method string, optional (default "linear"|"spline"|"nearest")method (optional string).
result number or vector
Snaps each query point to whichever known sample is closest. Look for a blocky, staircase-like curve instead of anything smooth or straight.

Examples

Written for this form and run by the build -- the output below is what the engine actually produced.

x = [0, 2, 4, 6, 8, 10]
y = [0, 1.8, 1.2, 3.5, 3.0, 4.8]
xq = 0 to 10 step 0.1
plot(xq, interp1(x, y, xq, "nearest"))
scatter(x, y)
output
· plot: figure 47 (101 points)
· scatter: figure 48 (6 points)
-1 0 1 2 3 4 5 0 1 2 3 4 5 6 7 8 9 10 x=0, y=0 x=2, y=1.8 x=4, y=1.2 x=6, y=3.5 x=8, y=3 x=10, y=4.8 x=0, y=0 x=2, y=1.8 x=4, y=1.2 x=6, y=3.5 x=8, y=3 x=10, y=4.8 x=0, y=0 x=2, y=1.8 x=4, y=1.2 x=6, y=3.5 x=8, y=3 x=10, y=4.8 -1 0 1 2 3 4 5 0 1 2 3 4 5 6 7 8 9 10 x=0, y=0 x=2, y=1.8 x=4, y=1.2 x=6, y=3.5 x=8, y=3 x=10, y=4.8 x=0, y=0 x=2, y=1.8 x=4, y=1.2 x=6, y=3.5 x=8, y=3 x=10, y=4.8 x=0, y=0 x=2, y=1.8 x=4, y=1.2 x=6, y=3.5 x=8, y=3 x=10, y=4.8

How it works

interp1(x, y, xq, [method]) — method is "linear" (default, extrapolates via the boundary segment's slope), "spline" (natural cubic, shares splineplot's solver), or "nearest". xq may be a scalar or a vector; extrapolation beyond x's range is always on (there's no separate flag to opt in — a query point outside the data is still answered, never an error), matching what "interpolation *and* extrapolation" implies.

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.