Jump to content

Multidimensional interpolation: Difference between revisions

From Wikibase
Restore space after removed comment. AI-assisted (RonzzWikiCowriter) (via update-page on MediaWiki MCP Server)
 
(43 intermediate revisions by 2 users not shown)
Line 1: Line 1:
'''Multidimensional interpolation''' is the process of finding a multivariable function $f(x_1,\cdots,x_m)$ that for a definite set of datapoints $\{(x_{1,i},\cdots,x_{m,i},y_i)\mid i\in [0,n], i \in \mathbb{N}\}$ satisfies $f(x_{1,i},\cdots,x_{m,i})=y_i$.
'''Multidimensional interpolation''' is the process of finding a multivariable function $f(x_1,\cdots,x_m)$ that for a definite set of datapoints $\{(x_{1,i},\cdots,x_{m,i},y_i)\mid i\in [0,n], i \in \mathbb{N}\}$ satisfies $f(x_{1,i},\cdots,x_{m,i})=y_i$.


== Methods ==
== Grid interpolation ==


=== Grid interpolation ===
=== via Lagrange polynomial in multiple variables ===


If a "grid" of $n^m$ datapoints is known, one on each combination of $n$ nodes in each of the $m$ variables:
If a "grid" of $n^m$ datapoints, i.e., values of $y$ at all possible combinations of $n$ particular values in each of the $m$ variables


$$
$$
Line 11: Line 11:
$$
$$


then the interpolant is the multivariable Lagrange polynomial
is known, then a multivariable-version of the Lagrange polynomial can be used to interpolate for $y$.


$$
$$
f(x_1,\cdots,x_m)=\sum_{i_1=0}^{n-1}\cdots\sum_{i_m=0}^{n-1}y_{i_1,\cdots,i_m}\prod_{j=1}^{m}L^{(j)}_{i_j}(x_j),\qquad L^{(j)}_{i}(x_j)=\prod_{\substack{k=0\\k\neq i}}^{n-1}\frac{x_j-x_{j,k}}{x_{j,i}-x_{j,k}}
P(x_1,\cdots,x_m)=\sum_{i_1=0}^{n-1}\cdots\sum_{i_m=0}^{n-1}y_{i_1,\cdots,i_m}\prod_{j=1}^{m}L^{(x_j)}_{i_j}(x_j),\qquad L^{(x_j)}_{i_j}(x_j)=\prod_{\substack{k=0\\k\neq i_j}}^{n-1}\frac{x_j-x_{j,k}}{x_{j,i_j}-x_{j,k}}
$$
$$


where $L^{(j)}_i$ is the [[Polynomial_interpolation#Lagrange_polynomial|Lagrange basis polynomial]] of the variable $x_j$, built on that variable's $n$ nodes.
which is a multi-variable polynomial of degree at most $n-1$ in each variable unique for each set of datapoints.


Each factor $L^{(j)}_{i_j}(x_j)$ equals one at the node $x_{j,i_j}$ and zero at the other $n-1$ nodes of $x_j$. The product of the $m$ factors is therefore one at the single datapoint $(x_{1,i_1},\cdots,x_{m,i_m})$ and zero at every other datapoint, so each of the $n^m$ terms carries exactly its own reading and vanishes at all the rest, and the sum passes through all $n^m$ datapoints.
Similar to the [[Polynomial_interpolation#Lagrange_polynomial|one-dimensional Lagrange polynomial]]s,


The result is the '''tensor product''' interpolant, and it is unique among polynomials of degree at most $n-1$ in each variable: a difference of two of them vanishes at all $n^m$ nodes, and such a difference, read as a polynomial of degree at most $n-1$ in $x_1$, has $n$ roots for every fixed choice of the other variables, so each of its coefficients vanishes at those choices, one variable at a time<ref>{{#cite:Q2392}}</ref>. In the Lagrange basis the coefficients are the readings themselves, so nothing is solved for: the $n^m$ coefficients and the $n^m$ datapoints are the same numbers.
$$
L^{(x_j)}_{i_j}(x_j)=
\begin{cases}
1, & x_j=x_{j,i_j},\\
0, & x_j=x_{j,i_k},\, i_k \in  [0,n-1],\, i_k \neq i_j,\, i_k \in \mathbb{N}
\end{cases}
$$
 
which guarantees that $P(x_1,\cdots,x_m)=y(x_1,\cdots,x_m)$ at all datapoints.<ref>{{#cite:Q2392}}</ref>
 
<blockquote>
$P(x_1,\cdots,x_m)$ is also known as the '''tensor product Lagrange interpolant'''.
</blockquote>


{{Hidden example}}
{{Hidden example}}
'''Surface temperature over a square of weather stations.''' Sixteen stations sit on a $4\times4$ grid, spaced $1^\circ$ of longitude apart from west to east and $1^\circ$ of latitude apart from south to north. The readings are synthetic, constructed for this example:
'''Surface temperature over a square of weather stations.'''  
 
Sixteen weather stations sitting in a $4^\circ \times 4^\circ$ grid measure surface temperature at various latitudes and longitudes.


{| class="wikitable"
{| class="wikitable"
Line 30: Line 44:
! latitude $\downarrow$ / longitude $\rightarrow$ !! $0^\circ$E !! $1^\circ$E !! $2^\circ$E !! $3^\circ$E
! latitude $\downarrow$ / longitude $\rightarrow$ !! $0^\circ$E !! $1^\circ$E !! $2^\circ$E !! $3^\circ$E
|-
|-
! $53^\circ$N
! $50^\circ$N
| 18.4 || 19.1 || 20.6 || 21.8
| 22.8 || 23.6 || 24.9 || 26.1
|-
! $51^\circ$N
| 21.7 || 22.5 || 23.9 || 25.0
|-
|-
! $52^\circ$N
! $52^\circ$N
| 20.1 || 20.9 || 22.3 || 23.5
| 20.1 || 20.9 || 22.3 || 23.5
|-
|-
! $51^\circ$N
! $53^\circ$N
| 21.7 || 22.5 || 23.9 || 25.0
| 18.4 || 19.1 || 20.6 || 21.8
|-
! $50^\circ$N
| 22.8 || 23.6 || 24.9 || 26.1
|}
|}


With $m=2$ and $n=4$ the sum has $n^m=16$ terms, one per station. Number the longitude nodes $x_{1,1}=0$, $x_{1,2}=1$, $x_{1,3}=2$, $x_{1,4}=3$ and the latitude nodes $x_{2,1}=50$, $x_{2,2}=51$, $x_{2,3}=52$, $x_{2,4}=53$, so that $i_1=1$ is the column at $0^\circ$E and $i_2=1$ the row at $50^\circ$N, the bottom row of the table:
What is the interpolated temperature at $51.6^\circ \textbf{N}, 1.4^\circ \textbf{E}$?
 
'''Algebraic solution''':


Let $x_1$ represent the latitude, $x_2$ represent the longitude, and denote the surface temperature reading at row $i_1$, column $i_2$ by $y_{i_1,i_2}$.
Then the interpolating multi-variable Lagrange polynomial is
$$
$$
f(x_1,x_2)=\sum_{i_1=1}^{4}\sum_{i_2=1}^{4}y_{i_1,i_2}\,L_{1i_1}(x_1)\,L_{2i_2}(x_2)
P(x_1,x_2)=\sum_{i_1=1}^{4}\sum_{i_2=1}^{4}y_{i_1,i_2}\,L^{(x_1)}_{i_1}(x_1)\,L^{(x_2)}_{i_2}(x_2)
$$
$$


where $y_{i_1,i_2}$ is the reading in column $i_1$ and row $i_2$, and $L_{ji_j}$ is the general $L^{(j)}_{i_j}$ with the node index counted from $1$ rather than from $0$.
where
 
'''The eight basis polynomials.''' Four are built on the longitude nodes $0,1,2,3$ and are functions of $x_1$; four are built on the latitude nodes $50,51,52,53$ and are functions of $x_2$:


$$
$$
L_{11}(x_1)=-\frac{(x_1-1)(x_1-2)(x_1-3)}{6}=-\frac{1}{6}x_1^3+x_1^2-\frac{11}{6}x_1+1,\qquad L_{12}(x_1)=\frac{x_1(x_1-2)(x_1-3)}{2}=\frac{1}{2}x_1^3-\frac{5}{2}x_1^2+3x_1
L^{(x_1)}_{1}(x_1)=\frac{(x_1-51)(x_1-52)(x_1-53)}{(50-51)(50-52)(50-53)}=-\frac{1}{6}x_1^3+26x_1^2-\frac{8111}{6}x_1+23426,\\
L^{(x_1)}_{2}(x_1)=\frac{(x_1-50)(x_1-52)(x_1-53)}{(51-50)(51-52)(51-53)}=\frac{1}{2}x_1^3-\frac{155}{2}x_1^2+4003x_1-68900
$$
$$


$$
$$
L_{13}(x_1)=-\frac{x_1(x_1-1)(x_1-3)}{2}=-\frac{1}{2}x_1^3+2x_1^2-\frac{3}{2}x_1,\qquad L_{14}(x_1)=\frac{x_1(x_1-1)(x_1-2)}{6}=\frac{1}{6}x_1^3-\frac{1}{2}x_1^2+\frac{1}{3}x_1
L^{(x_1)}_{3}(x_1)=\frac{(x_1-50)(x_1-51)(x_1-53)}{(52-50)(52-51)(52-53)}=-\frac{1}{2}x_1^3+77x_1^2-\frac{7903}{2}x_1+67575,\\
L^{(x_1)}_{4}(x_1)=\frac{(x_1-50)(x_1-51)(x_1-52)}{(53-50)(53-51)(53-52)}=\frac{1}{6}x_1^3-\frac{51}{2}x_1^2+\frac{3901}{3}x_1-22100
$$
$$


$$
$$
L_{21}(x_2)=-\frac{(x_2-51)(x_2-52)(x_2-53)}{6}=-\frac{1}{6}x_2^3+26x_2^2-\frac{8111}{6}x_2+23426,\qquad L_{22}(x_2)=\frac{(x_2-50)(x_2-52)(x_2-53)}{2}=\frac{1}{2}x_2^3-\frac{155}{2}x_2^2+4003x_2-68900
L^{(x_2)}_{1}(x_2)=\frac{(x_2-1)(x_2-2)(x_2-3)}{(0-1)(0-2)(0-3)}=-\frac{1}{6}x_2^3+x_2^2-\frac{11}{6}x_2+1,\qquad L^{(x_2)}_{2}(x_2)=\frac{x_2(x_2-2)(x_2-3)}{(1-0)(1-2)(1-3)}=\frac{1}{2}x_2^3-\frac{5}{2}x_2^2+3x_2
$$
$$


$$
$$
L_{23}(x_2)=-\frac{(x_2-50)(x_2-51)(x_2-53)}{2}=-\frac{1}{2}x_2^3+77x_2^2-\frac{7903}{2}x_2+67575,\qquad L_{24}(x_2)=\frac{(x_2-50)(x_2-51)(x_2-52)}{6}=\frac{1}{6}x_2^3-\frac{51}{2}x_2^2+\frac{3901}{3}x_2-22100
L^{(x_2)}_{3}(x_2)=\frac{x_2(x_2-1)(x_2-3)}{(2-0)(2-1)(2-3)}=-\frac{1}{2}x_2^3+2x_2^2-\frac{3}{2}x_2,\qquad L^{(x_2)}_{4}(x_2)=\frac{x_2(x_2-1)(x_2-2)}{(3-0)(3-1)(3-2)}=\frac{1}{6}x_2^3-\frac{1}{2}x_2^2+\frac{1}{3}x_2
$$
$$


'''The polynomial in $x_1$ and $x_2$.''' Substituting the eight polynomials above leaves one term per station, 16 in all, four on each parallel:
Hence


$$
$$
f(x_1,x_2)=22.8\,L_{11}(x_1)L_{21}(x_2)+23.6\,L_{12}(x_1)L_{21}(x_2)+24.9\,L_{13}(x_1)L_{21}(x_2)+26.1\,L_{14}(x_1)L_{21}(x_2)
P(x_1,x_2)=22.8\,L^{(x_1)}_{1}(x_1)L^{(x_2)}_{1}(x_2)+23.6\,L^{(x_1)}_{1}(x_1)L^{(x_2)}_{2}(x_2)+24.9\,L^{(x_1)}_{1}(x_1)L^{(x_2)}_{3}(x_2)+26.1\,L^{(x_1)}_{1}(x_1)L^{(x_2)}_{4}(x_2)
$$
$$


$$
$$
{}+21.7\,L_{11}(x_1)L_{22}(x_2)+22.5\,L_{12}(x_1)L_{22}(x_2)+23.9\,L_{13}(x_1)L_{22}(x_2)+25.0\,L_{14}(x_1)L_{22}(x_2)
{}+21.7\,L^{(x_1)}_{2}(x_1)L^{(x_2)}_{1}(x_2)+22.5\,L^{(x_1)}_{2}(x_1)L^{(x_2)}_{2}(x_2)+23.9\,L^{(x_1)}_{2}(x_1)L^{(x_2)}_{3}(x_2)+25.0\,L^{(x_1)}_{2}(x_1)L^{(x_2)}_{4}(x_2)
$$
$$


$$
$$
{}+20.1\,L_{11}(x_1)L_{23}(x_2)+20.9\,L_{12}(x_1)L_{23}(x_2)+22.3\,L_{13}(x_1)L_{23}(x_2)+23.5\,L_{14}(x_1)L_{23}(x_2)
{}+20.1\,L^{(x_1)}_{3}(x_1)L^{(x_2)}_{1}(x_2)+20.9\,L^{(x_1)}_{3}(x_1)L^{(x_2)}_{2}(x_2)+22.3\,L^{(x_1)}_{3}(x_1)L^{(x_2)}_{3}(x_2)+23.5\,L^{(x_1)}_{3}(x_1)L^{(x_2)}_{4}(x_2)
$$
$$


$$
$$
{}+18.4\,L_{11}(x_1)L_{24}(x_2)+19.1\,L_{12}(x_1)L_{24}(x_2)+20.6\,L_{13}(x_1)L_{24}(x_2)+21.8\,L_{14}(x_1)L_{24}(x_2)
{}+18.4\,L^{(x_1)}_{4}(x_1)L^{(x_2)}_{1}(x_2)+19.1\,L^{(x_1)}_{4}(x_1)L^{(x_2)}_{2}(x_2)+20.6\,L^{(x_1)}_{4}(x_1)L^{(x_2)}_{3}(x_2)+21.8\,L^{(x_1)}_{4}(x_1)L^{(x_2)}_{4}(x_2)
$$
$$


Collecting like powers reduces this to 16 monomials, of degree at most 3 in each variable; the large coefficients come from the latitude nodes sitting at 50 to 53 rather than at 0 to 3:
On simplification:


$$
$$
f(x_1,x_2)=-\tfrac{1}{45}x_1^3x_2^3+\tfrac{103}{30}x_1^3x_2^2-\tfrac{31823}{180}x_1^3x_2+\tfrac{45511}{15}x_1^3+\tfrac{11}{120}x_1^2x_2^3-\tfrac{283}{20}x_1^2x_2^2+\tfrac{87361}{120}x_1^2x_2-\tfrac{62416}{5}x_1^2
P(x_1,x_2)=-\tfrac{1}{45}x_1^3x_2^3+\tfrac{11}{120}x_1^3x_2^2-\tfrac{31}{360}x_1^3x_2+\tfrac{1}{15}x_1^3+\tfrac{103}{30}x_1^2x_2^3-\tfrac{283}{20}x_1^2x_2^2+\tfrac{199}{15}x_1^2x_2-\tfrac{209}{20}x_1^2
$$
$$


$$
$$
{}-\tfrac{31}{360}x_1x_2^3+\tfrac{199}{15}x_1x_2^2-\tfrac{245249}{360}x_1x_2+\tfrac{174899}{15}x_1+\tfrac{1}{15}x_2^3-\tfrac{209}{20}x_2^2+\tfrac{32657}{60}x_2-\tfrac{93997}{10}
{}-\tfrac{31823}{180}x_1x_2^3+\tfrac{87361}{120}x_1x_2^2-\tfrac{245249}{360}x_1x_2+\tfrac{32657}{60}x_1+\tfrac{45511}{15}x_2^3-\tfrac{62416}{5}x_2^2+\tfrac{174899}{15}x_2-\tfrac{93997}{10}
$$
$$


'''Evaluating at $1.4^\circ$E, $51.6^\circ$N.''' The point goes into the developed form as it stands, one coordinate for each of $x_1$ and $x_2$:
Substitute in $x_1=51.6,x_2=1.4$,


$$
$$
f(1.4,51.6)=-\tfrac{1}{45}(1.4)^3(51.6)^3+\tfrac{103}{30}(1.4)^3(51.6)^2-\tfrac{31823}{180}(1.4)^3(51.6)+\tfrac{45511}{15}(1.4)^3
y(51.6,1.4)=P(51.6,1.4)=-\tfrac{1}{45}(51.6)^3(1.4)^3+\tfrac{11}{120}(51.6)^3(1.4)^2-\tfrac{31}{360}(51.6)^3(1.4)+\tfrac{1}{15}(51.6)^3
$$
$$


$$
$$
{}+\tfrac{11}{120}(1.4)^2(51.6)^3-\tfrac{283}{20}(1.4)^2(51.6)^2+\tfrac{87361}{120}(1.4)^2(51.6)-\tfrac{62416}{5}(1.4)^2
{}+\tfrac{103}{30}(51.6)^2(1.4)^3-\tfrac{283}{20}(51.6)^2(1.4)^2+\tfrac{199}{15}(51.6)^2(1.4)-\tfrac{209}{20}(51.6)^2
$$
$$


$$
$$
{}-\tfrac{31}{360}(1.4)(51.6)^3+\tfrac{199}{15}(1.4)(51.6)^2-\tfrac{245249}{360}(1.4)(51.6)+\tfrac{174899}{15}(1.4)
{}-\tfrac{31823}{180}(51.6)(1.4)^3+\tfrac{87361}{120}(51.6)(1.4)^2-\tfrac{245249}{360}(51.6)(1.4)+\tfrac{32657}{60}(51.6)
$$
$$


$$
$$
{}+\tfrac{1}{15}(51.6)^3-\tfrac{209}{20}(51.6)^2+\tfrac{32657}{60}(51.6)-\tfrac{93997}{10}=22.1162
{}+\tfrac{45511}{15}(1.4)^3-\tfrac{62416}{5}(1.4)^2+\tfrac{174899}{15}(1.4)-\tfrac{93997}{10}=22.1162
$$
$$


The individual terms are of order $10^4$ and cancel down to $22$, so several digits are lost and the sum has to be taken exactly; the code below does that. The nested route of [[#Pointwise_interpolation|pointwise interpolation]] regroups these same 16 terms into two passes, with numbers no larger than the readings, and reaches the same result with nothing to cancel; the developed form is the harder of the two routes to evaluate, not the easier.
'''Numerical solution''':
 
The same arithmetic in code:


<syntaxhighlight lang="python" copy>
<syntaxhighlight lang="python" copy>
Line 135: Line 152:
     return basis
     return basis


longitude = [0, 1, 2, 3]                   # nodes of x1
latitude = [50, 51, 52, 53]                 # nodes of x1
latitude = [50, 51, 52, 53]                 # nodes of x2
longitude = [0, 1, 2, 3]                   # nodes of x2
L1 = lagrange_basis(longitude, x1)         # L11 .. L14
L1 = lagrange_basis(latitude, x1)           # L^(x1)_1 .. L^(x1)_4
L2 = lagrange_basis(latitude, x2)           # L21 .. L24
L2 = lagrange_basis(longitude, x2)         # L^(x2)_1 .. L^(x2)_4
print(L1)
print(L1)
print(L2)
print(L2)
Line 148: Line 165:


# one term per station: reading times its x1 polynomial times its x2 polynomial
# one term per station: reading times its x1 polynomial times its x2 polynomial
f = sp.expand(sum(sp.Rational(str(temperature[j][i])) * L1[i] * L2[j]
p = sp.expand(sum(sp.Rational(str(temperature[j][i])) * L1[j] * L2[i]
                   for j in range(4) for i in range(4)))
                   for j in range(4) for i in range(4)))
print(f)
print(p)


value = f.subs({x1: sp.Rational('1.4'), x2: sp.Rational('51.6')})  # 1.4 E, 51.6 N, exact
value = p.subs({x1: sp.Rational('51.6'), x2: sp.Rational('1.4')})  # 51.6 N, 1.4 E
print(value)
print(value)
print(float(value))
print(float(value))
</syntaxhighlight>
</syntaxhighlight>


The two coordinates go in as the decimals they are, and <code>sp.Rational</code> reads a decimal string exactly, so nothing is rounded: <code>value</code> is the exact $\tfrac{3455657}{156250}$, which prints as <code>22.1162048</code>, the number the developed form gave above.
'''Extra''': To plot the interpolating surface
 
Plotting the surface needs the sum evaluated on a mesh of arguments. This snippet stands on its own:


<syntaxhighlight lang="python" copy>
<syntaxhighlight lang="python" copy>
Line 165: Line 180:
import matplotlib.pyplot as plt
import matplotlib.pyplot as plt


longitude = np.array([0.0, 1.0, 2.0, 3.0])           # nodes of x1
latitude = np.array([50.0, 51.0, 52.0, 53.0])       # nodes of x1
latitude = np.array([50.0, 51.0, 52.0, 53.0])       # nodes of x2
longitude = np.array([0.0, 1.0, 2.0, 3.0])           # nodes of x2
temperature = np.array([[22.8, 23.6, 24.9, 26.1],
temperature = np.array([[22.8, 23.6, 24.9, 26.1],
                         [21.7, 22.5, 23.9, 25.0],
                         [21.7, 22.5, 23.9, 25.0],
Line 172: Line 187:
                         [18.4, 19.1, 20.6, 21.8]])  # row = latitude, column = longitude
                         [18.4, 19.1, 20.6, 21.8]])  # row = latitude, column = longitude


def basis_at(nodes, x):
def L(nodes, x):
     """The Lagrange basis polynomials on `nodes`, evaluated at x."""
     """The Lagrange basis polynomials on `nodes`, evaluated at x."""
     return np.array([np.prod([(x - k) / (i - k) for k in nodes if k != i]) for i in nodes])
     return np.array([np.prod([(x - k) / (i - k) for k in nodes if k != i]) for i in nodes])


def f(x1, x2):
def p(x1, x2):
     """The 16-term sum: one term per station."""
     """2-variable Lagrange polynomial"""
     return (temperature * np.outer(basis_at(latitude, x2), basis_at(longitude, x1))).sum()
     return (temperature * np.outer(L(latitude, x1), L(longitude, x2))).sum()


lon_plot = np.linspace(0.0, 3.0, 121)
lon_plot = np.linspace(0.0, 3.0, 121)
lat_plot = np.linspace(50.0, 53.0, 121)
lat_plot = np.linspace(50.0, 53.0, 121)
surface = np.array([[f(x, y) for x in lon_plot] for y in lat_plot])
surface = np.array([[p(x, y) for y in lon_plot] for x in lat_plot])


fig = plt.figure(figsize=(9.0, 6.8))
fig = plt.figure(figsize=(9.0, 6.8))
Line 193: Line 208:
           s=70, color="#c0392b", edgecolor="black", linewidth=0.9,
           s=70, color="#c0392b", edgecolor="black", linewidth=0.9,
           depthshade=False, label="16 weather stations")
           depthshade=False, label="16 weather stations")
ax.scatter([1.4], [51.6], [f(1.4, 51.6)], s=170, color="black", marker="X",
ax.scatter([1.4], [51.6], [p(51.6, 1.4)], s=170, color="black", marker="X",
           depthshade=False, label=r"query point (1.4$^\circ$E, 51.6$^\circ$N)")
           depthshade=False, label=r"query point (1.4$^\circ$E, 51.6$^\circ$N)")
ax.set_xlabel(r"longitude ($^\circ$E)", labelpad=10)
ax.set_xlabel(r"longitude ($^\circ$E)", labelpad=10)
Line 209: Line 224:
</syntaxhighlight>
</syntaxhighlight>


[[File:Bicubic grid interpolation temperature.png|thumb|none|560px|alt=Three-dimensional plot of a gently curving temperature surface over a longitude-latitude grid, with the sixteen station readings marked as red dots and one query point marked with a cross|The interpolating surface through the sixteen station readings, the tensor product of four longitude and four latitude basis polynomials. The query point at $1.4^\circ$E, $51.6^\circ$N is marked with a cross.]]
[[File:Bicubic grid interpolation temperature.png|thumb|none|560px|alt=Three-dimensional plot of a gently curving temperature surface over a longitude-latitude grid, with the sixteen station readings marked as red dots and one query point marked with a cross|The interpolating surface through the sixteen station readings constructed via Lagrange polynomials in two variables. $1.4^\circ$E, $51.6^\circ$N is marked with a cross.]]


The temperature falls from about $26\ ^\circ$C in the south-east to about $18\ ^\circ$C in the north-west, so the surface is dominated by a plane tilted towards the north-west, with a gentler curvature across it.
As observed, the surface is dominated by a plane tilted gently downwards from the south-east towards the north-west, predicting a gentle decrease in temperature along that direction.
{{End hidden}}
{{End hidden}}


=== Pointwise interpolation ===
== Pointwise interpolation ==


Pointwise interpolation (or '''point evaluation''') asks only for the value of $f$ at one new point $(x_1^*,\cdots,x_m^*)$ inside the data range, without constructing $f$ anywhere else. It fixes what is to be computed, not how $f$ is defined: the rule of interpolation is free, and only one number is wanted out of it.
Pointwise interpolation estimates the value of $y$ at one particular point, $y(x_1^*,\cdots,x_m^*)$, without constructing a general interpolating function.


'''One variable at a time.''' A grid is a family of lines, one along each variable, and each of those lines is an ordinary one-dimensional dataset. A one-dimensional rule applied to the lines of one direction, and then to the lines of the next, reaches the query point. [[Person:Jeffrey R. Chasnov|Chasnov]] states the two-variable case in full: on an $(n+1)\times(n+1)$ grid, first perform $n+1$ one-dimensional interpolations to find the interpolant at the $n+1$ points $(x_0,y^*),\cdots,(x_n,y^*)$, then a single one-dimensional interpolation to find it at $(x^*,y^*)$; MATLAB's <code>interp2</code> and <code>interp3</code> address exactly this problem<ref>{{#cite:Q2421}}</ref>.
=== via successive 1D interpolation in each variable ===


'''The rule, named once.''' Let
If a "grid" of $n^m$ datapoints, i.e., values of $y$ at all possible combinations of $n$ particular values in each of the $m$ variables


$$
$$
\mathcal{I}\big[\{v_k\}_{k=0}^{n-1}\big](t)
\{(x_{1,i_1},\cdots,x_{m,i_m},y_{i_1,\cdots,i_m})\mid i_1,\cdots,i_m \in [0,n-1],\, i_1,\cdots,i_m \in \mathbb{N}\}
$$
$$


be the value at $t$ that the chosen rule gives to readings $v_0,\cdots,v_{n-1}$ at nodes $t_0,\cdots,t_{n-1}$. The rule may be [[Piecewise_linear_interpolation|piecewise linear]], a cubic spline with whatever end condition ([[Cubic_spline_interpolation|cubic spline interpolation]]), a monotone scheme, or a single polynomial through the whole line; only the arithmetic inside a pass changes.
is known, then first perform $n^{m-1}$ 1D interpolations in $x_1$, fixing $x_2,\cdots,x_m$ at all known values, obtaining $n^{m-1}$ interpolating functions $f^{(x_1)}_{i_2,i_3,\cdots,i_m}(x_1)$


'''Two variables.''' With readings $y_{i_1,i_2}$ on $n$ nodes per axis, a pass along $x_2$ holds $x_1$ at each of its nodes in turn:
Now evaluate each $f^{(x_1)}$ at $x_1=x_1^*$ to obtain $n^{m-1}$ interpolated datapoints


$$
$$
y^*_{i_1}=\mathcal{I}\big[\{y_{i_1,i_2}\}_{i_2=0}^{n-1}\big](x_2^*),\qquad \forall i_1\in [0,n-1],\, i_1 \in \mathbb{N}
\{(x_1^*,x_{2,i_2},\cdots,x_{m,i_m},y_{x_1^*,i_2,\cdots,i_m})\mid i_2,\cdots,i_m \in [0,n-1],\, i_2,\cdots,i_m \in \mathbb{N}\}
$$
$$


The $n$ values $y^*_{i_1}$ are the interpolant at $(x_{1,i_1},x_2^*)$, i.e., readings on the line $x_2=x_2^*$, so one more pass finishes:
Then perform $n^{m-2}$ 1D interpolations in $x_2$, fixing $x_3,\cdots,x_m$ at all known values, obtaining $n^{m-2}$ interpolating functions $f^{(x_2)}_{i_3,\cdots,i_m}(x_2)$
 
Now evaluate each $f^{(x_2)}$ at $x_2=x_2^*$ to obtain $n^{m-2}$ interpolated datapoints


$$
$$
f(x_1^*,x_2^*)\approx\mathcal{I}\big[\{y^*_{i_1}\}_{i_1=0}^{n-1}\big](x_1^*)
\{(x_1^*,x_2^*,x_{3,i_3}, \cdots,x_{m,i_m},y_{x_1^*,x_2^*,i_3,\cdots,i_m})\mid i_3,\cdots,i_m \in [0,n-1],\, i_3,\cdots,i_m \in \mathbb{N}\}
$$
$$


'''$m$ variables.''' Each pass removes one variable, leaving $n^{m-1}$ values, then $n^{m-2}$, down to one, so the passes cost $n^{m-1},n^{m-2},\cdots,n^0$ interpolations whatever the rule, that is $(n^m-1)/(n-1)$ in all; for $n=4$ and $m=2,3,4$ that is $5$, $21$, and $85$. A rule that reads only the nodes bracketing its target needs only the lines that bracket the target, so a local rule costs two readings a line and two lines a pass.
Then perform $n^{m-3}$ 1D interpolations in $x_3$, $n^{m-4}$ 1D interpolations in $x_4$, $\ldots$, $n$ 1D interpolations in $x_{m-1}$, and finally $1$ 1D interpolation in $x_m$ to obtain one interpolating function $f^{(x_m)}$.


'''Any order.''' For a rule linear in its readings, with weights $\varphi_k$ fixed by the nodes and by the target, the two passes give
Then the estimate of $y$ at $(x_1^*,\cdots,x_m^*)$ is $f^{(x_m)}(x_m^*)$.<ref>{{#cite:Q2421}}</ref>
 
{{Hidden example}}
'''Surface temperature over a grid of weather stations (particular value by successive 1D interpolations)'''
 
Retake the same example as in [[#Grid interpolation]]:
 
Sixteen weather stations sitting in a $4^\circ \times 4^\circ$ grid measure surface temperature at various latitudes and longitudes.
 
{| class="wikitable"
|+ Temperature ($^\circ$C) at each station
! latitude $\downarrow$ / longitude $\rightarrow$ !! $0^\circ$E !! $1^\circ$E !! $2^\circ$E !! $3^\circ$E
|-
! $50^\circ$N
| 22.8 || 23.6 || 24.9 || 26.1
|-
! $51^\circ$N
| 21.7 || 22.5 || 23.9 || 25.0
|-
! $52^\circ$N
| 20.1 || 20.9 || 22.3 || 23.5
|-
! $53^\circ$N
| 18.4 || 19.1 || 20.6 || 21.8
|}
 
What is the interpolated temperature at $51.6^\circ \textbf{N}, 1.4^\circ \textbf{E}$?
 
'''Algebraic solution''':
 
'''Step 1: interpolate in $x_1$ (latitude).''' Interpolate the four readings of different latitudes with the one-dimensional Lagrange polynomial at each of the four known longitudes, then evaluate the interpolating functions at $x_1^*=51.6$.


$$
$$
f(x_1^*,x_2^*)=\sum_{i_1=0}^{n-1}\sum_{i_2=0}^{n-1}\varphi^{(1)}_{i_1}(x_1^*)\,\varphi^{(2)}_{i_2}(x_2^*)\,y_{i_1,i_2}
L^{(x_1)}_{1}(x_1)=\frac{(x_1-51)(x_1-52)(x_1-53)}{(50-51)(50-52)(50-53)},\qquad L^{(x_1)}_{2}(x_1)=\frac{(x_1-50)(x_1-52)(x_1-53)}{(51-50)(51-52)(51-53)}
$$
$$


whose products are the same under either nesting, so the order of the passes is free<ref>{{#cite:Q2391}}</ref>.
$$
L^{(x_1)}_{3}(x_1)=\frac{(x_1-50)(x_1-51)(x_1-53)}{(52-50)(52-51)(52-53)},\qquad L^{(x_1)}_{4}(x_1)=\frac{(x_1-50)(x_1-51)(x_1-52)}{(53-50)(53-51)(53-52)}
$$


'''The station grid again, by hand.''' Take the sixteen readings of [[#Grid_interpolation|grid interpolation]] and the query at $1.4^\circ$E, $51.6^\circ$N, and take the rule for a line of unit spacing to be the cubic through its four readings, written in differences:
<blockquote>
The four Lagrange basis polynomials in $x_1$, built on the latitude nodes $50,51,52,53^\circ \textbf{N}$, are the same at every longitude.
</blockquote>
 
At $x_1^*=51.6$,


$$
$$
C(u)=w_0+\Delta w_0\,u+\frac{\Delta^2 w_0}{2}u(u-1)+\frac{\Delta^3 w_0}{6}u(u-1)(u-2)
L^{(x_1)}_{1}(51.6)=-0.056,\qquad L^{(x_1)}_{2}(51.6)=0.448,\qquad L^{(x_1)}_{3}(51.6)=0.672,\qquad L^{(x_1)}_{4}(51.6)=-0.064
$$
$$


where $u$ is the distance from the first node and $\Delta$ is the forward difference of the line's readings. On four nodes this is what a not-a-knot spline gives as well, a single cubic piece spanning the line. At $u=1.4$ the last two coefficients are $u(u-1)/2=0.28$ and $u(u-1)(u-2)/6=-0.056$, the same on every parallel, so each longitude pass costs three multiplications on top of its reading:
Hence the four interpolated readings


* $50^\circ$N, $\Delta=(0.8,1.3,1.2)$, $\Delta^2=(0.5,-0.1)$, $\Delta^3=-0.6$: $22.8+1.4(0.8)+0.28(0.5)-0.056(-0.6)=24.0936$
$$
* $51^\circ$N, $\Delta=(0.8,1.4,1.1)$, $\Delta^2=(0.6,-0.3)$, $\Delta^3=-0.9$: $21.7+1.4(0.8)+0.28(0.6)-0.056(-0.9)=23.0384$
y_{x_1^*,i_2}=\sum_{i_1=1}^{4}y_{i_1,i_2}\,L^{(x_1)}_{i_1}(51.6),\qquad i_2=1,2,3,4
* $52^\circ$N, $\Delta=(0.8,1.4,1.2)$, $\Delta^2=(0.6,-0.2)$, $\Delta^3=-0.8$: $20.1+1.4(0.8)+0.28(0.6)-0.056(-0.8)=21.4328$
$$
* $53^\circ$N, $\Delta=(0.7,1.5,1.2)$, $\Delta^2=(0.8,-0.3)$, $\Delta^3=-1.1$: $18.4+1.4(0.7)+0.28(0.8)-0.056(-1.1)=19.6656$
 
Those four are readings on the line $x_1=1.4$, and the same rule on them at $u=1.6$, where $u(u-1)/2=0.48$ and $u(u-1)(u-2)/6=-0.064$, gives


$$
$$
24.0936+1.6(-1.0552)+0.48(-0.5504)-0.064(0.3888)=22.1162048
y_{x_1^*,1}=-0.056(22.8)+0.448(21.7)+0.672(20.1)-0.064(18.4)=20.7744,\\
y_{x_1^*,2}=-0.056(23.6)+0.448(22.5)+0.672(20.9)-0.064(19.1)=21.5808,\\
y_{x_1^*,3}=-0.056(24.9)+0.448(23.9)+0.672(22.3)-0.064(20.6)=22.9800,\\
y_{x_1^*,4}=-0.056(26.1)+0.448(25.0)+0.672(23.5)-0.064(21.8)=24.1352
$$
$$


the number [[#Grid_interpolation|grid interpolation]] reached by summing its 16 monomials, obtained here without forming any of them.
{| class="wikitable"
|+ Interpolated temperature ($^\circ$C) at $51.6^\circ$N
! longitude $x_{2,i_2}$ !! $0^\circ$E !! $1^\circ$E !! $2^\circ$E !! $3^\circ$E
|-
! $y_{x_1^*,i_2}$
| 20.7744 || 21.5808 || 22.9800 || 24.1352
|}


The same passes in code, with the rule passed in as an argument:
'''Step 2: interpolate in $x_2$ (longitude).''' Interpolate the four interpolated readings $y_{51.6^\circ \textbf{N},i_2}$ with a one-dimensional Lagrange polynomial in $x_2$ (longitude$) to obtain the interpolating function $f^{(x_2)}(x_2)$. Evaluate $f$ at $x_2^*=1.4$ for $y(51.6,1.4)$.


<syntaxhighlight lang="python" copy>
Four Lagrange basis polynomials, built on the longitude nodes $0,1,2,3^\circ \textbf{E}$:
import numpy as np
from scipy.interpolate import CubicSpline


temperature = np.array([[22.8, 23.6, 24.9, 26.1],
$$
                        [21.7, 22.5, 23.9, 25.0],
L^{(x_2)}_{1}(x_2)=\frac{(x_2-1)(x_2-2)(x_2-3)}{(0-1)(0-2)(0-3)},\qquad L^{(x_2)}_{2}(x_2)=\frac{x_2(x_2-2)(x_2-3)}{(1-0)(1-2)(1-3)}
                        [20.1, 20.9, 22.3, 23.5],
$$
                        [18.4, 19.1, 20.6, 21.8]])   # row = latitude, column = longitude


def passes(nodes, values, point, rule):
$$
    """Value at `point` of the grid interpolant, one one-dimensional pass per variable.
L^{(x_2)}_{3}(x_2)=\frac{x_2(x_2-1)(x_2-3)}{(2-0)(2-1)(2-3)},\qquad L^{(x_2)}_{4}(x_2)=\frac{x_2(x_2-1)(x_2-2)}{(3-0)(3-1)(3-2)}
$$


    nodes  -- node coordinates of each variable, in the order of the axes of `values`
At $x_2^*=1.4$,
    values -- m-dimensional array of readings, values[i_1, ..., i_m]
    rule  -- rule(nodes, readings, t), the one-dimensional rule evaluated at t
    """
    data = np.asarray(values, dtype=float)
    for axis, target in enumerate(point):        # one pass per variable, first axis first
        data = np.apply_along_axis(
            lambda line: rule(np.asarray(nodes[axis], dtype=float), line, target), 0, data)
    return float(data)


linear  = lambda nodes, line, t: float(np.interp(t, nodes, line))
$$
spline  = lambda nodes, line, t: float(CubicSpline(nodes, line)(t))
L^{(x_2)}_{1}(1.4)=-0.064,\qquad L^{(x_2)}_{2}(1.4)=0.672,\qquad L^{(x_2)}_{3}(1.4)=0.448,\qquad L^{(x_2)}_{4}(1.4)=-0.056
natural = lambda nodes, line, t: float(CubicSpline(nodes, line, bc_type="natural")(t))
$$


latitude, longitude = np.arange(50.0, 54.0), np.arange(0.0, 4.0)
$$
for name, rule in [("piecewise linear", linear), ("natural cubic spline", natural),
y(51.6,1.4)=f^{(x_2)}(1.4)=\sum_{i_2=1}^{4}y_{x_1^*,i_2}\,L^{(x_2)}_{i_2}(1.4)=-0.064(20.7744)+0.672(21.5808)+0.448(22.9800)-0.056(24.1352)
                  ("not-a-knot cubic spline", spline)]:
$$
    print(name, passes([latitude, longitude], temperature, [51.6, 1.4], rule))
</syntaxhighlight>


The spline line prints the value worked out above; the natural end condition gives $22.1157$ instead, and swapping the two variables in <code>nodes</code> and in <code>point</code> alike, with the readings transposed, returns the same three values.
$$
{}=22.1162
$$


<blockquote>
which agrees with the value obtained in [[#Grid interpolation]].
A rule that reads only the nodes bracketing its target does far less of this arithmetic. Piecewise linear passes at the same point touch only the four stations surrounding it: $0.6(22.5)+0.4(23.9)=23.06$ and $0.6(20.9)+0.4(22.3)=21.46$ along the parallels, then $0.4(23.06)+0.6(21.46)=22.10$ along the meridian. That is $0.02\ ^\circ$C from the grid interpolant, but the two are not interchangeable: the bilinear interpolant is merely continuous where cells meet, whereas the grid interpolant is a single polynomial and therefore smooth everywhere.
{{End hidden}}
</blockquote>


== References ==
== References ==


<references/>
<references/>

Latest revision as of 13:27, 28 October 2026

Multidimensional interpolation is the process of finding a multivariable function $f(x_1,\cdots,x_m)$ that for a definite set of datapoints $\{(x_{1,i},\cdots,x_{m,i},y_i)\mid i\in [0,n], i \in \mathbb{N}\}$ satisfies $f(x_{1,i},\cdots,x_{m,i})=y_i$.

Grid interpolation

via Lagrange polynomial in multiple variables

If a "grid" of $n^m$ datapoints, i.e., values of $y$ at all possible combinations of $n$ particular values in each of the $m$ variables

$$ \{(x_{1,i_1},\cdots,x_{m,i_m},y_{i_1,\cdots,i_m})\mid i_1,\cdots,i_m \in [0,n-1],\, i_1,\cdots,i_m \in \mathbb{N}\} $$

is known, then a multivariable-version of the Lagrange polynomial can be used to interpolate for $y$.

$$ P(x_1,\cdots,x_m)=\sum_{i_1=0}^{n-1}\cdots\sum_{i_m=0}^{n-1}y_{i_1,\cdots,i_m}\prod_{j=1}^{m}L^{(x_j)}_{i_j}(x_j),\qquad L^{(x_j)}_{i_j}(x_j)=\prod_{\substack{k=0\\k\neq i_j}}^{n-1}\frac{x_j-x_{j,k}}{x_{j,i_j}-x_{j,k}} $$

which is a multi-variable polynomial of degree at most $n-1$ in each variable unique for each set of datapoints.

Similar to the one-dimensional Lagrange polynomials,

$$ L^{(x_j)}_{i_j}(x_j)= \begin{cases} 1, & x_j=x_{j,i_j},\\ 0, & x_j=x_{j,i_k},\, i_k \in [0,n-1],\, i_k \neq i_j,\, i_k \in \mathbb{N} \end{cases} $$

which guarantees that $P(x_1,\cdots,x_m)=y(x_1,\cdots,x_m)$ at all datapoints.[1]

$P(x_1,\cdots,x_m)$ is also known as the tensor product Lagrange interpolant.

Pointwise interpolation

Pointwise interpolation estimates the value of $y$ at one particular point, $y(x_1^*,\cdots,x_m^*)$, without constructing a general interpolating function.

via successive 1D interpolation in each variable

If a "grid" of $n^m$ datapoints, i.e., values of $y$ at all possible combinations of $n$ particular values in each of the $m$ variables

$$ \{(x_{1,i_1},\cdots,x_{m,i_m},y_{i_1,\cdots,i_m})\mid i_1,\cdots,i_m \in [0,n-1],\, i_1,\cdots,i_m \in \mathbb{N}\} $$

is known, then first perform $n^{m-1}$ 1D interpolations in $x_1$, fixing $x_2,\cdots,x_m$ at all known values, obtaining $n^{m-1}$ interpolating functions $f^{(x_1)}_{i_2,i_3,\cdots,i_m}(x_1)$

Now evaluate each $f^{(x_1)}$ at $x_1=x_1^*$ to obtain $n^{m-1}$ interpolated datapoints

$$ \{(x_1^*,x_{2,i_2},\cdots,x_{m,i_m},y_{x_1^*,i_2,\cdots,i_m})\mid i_2,\cdots,i_m \in [0,n-1],\, i_2,\cdots,i_m \in \mathbb{N}\} $$

Then perform $n^{m-2}$ 1D interpolations in $x_2$, fixing $x_3,\cdots,x_m$ at all known values, obtaining $n^{m-2}$ interpolating functions $f^{(x_2)}_{i_3,\cdots,i_m}(x_2)$

Now evaluate each $f^{(x_2)}$ at $x_2=x_2^*$ to obtain $n^{m-2}$ interpolated datapoints

$$ \{(x_1^*,x_2^*,x_{3,i_3}, \cdots,x_{m,i_m},y_{x_1^*,x_2^*,i_3,\cdots,i_m})\mid i_3,\cdots,i_m \in [0,n-1],\, i_3,\cdots,i_m \in \mathbb{N}\} $$

Then perform $n^{m-3}$ 1D interpolations in $x_3$, $n^{m-4}$ 1D interpolations in $x_4$, $\ldots$, $n$ 1D interpolations in $x_{m-1}$, and finally $1$ 1D interpolation in $x_m$ to obtain one interpolating function $f^{(x_m)}$.

Then the estimate of $y$ at $(x_1^*,\cdots,x_m^*)$ is $f^{(x_m)}(x_m^*)$.[2]

References