Menu Close

Bilinear Interpolation Method | Python

Bilinear interpolation is a method of estimating a function at a point within a grid of data points.

It involves “constructing a piecewise linear surface” that passes through the four nearest data points to the desired point.

Bilinear Interpolation Method in Python:

				
					import numpy as np

# Define the data points on a grid
x = np.array([0, 1, 2])
y = np.array([0, 1, 2])
z = np.array([[1, 2, 3],
              [4, 5, 6],
              [7, 8, 9]])

# Program By - Bottom Science

# Define the point where we want to estimate the function
x_new = 1.5
y_new = 0.5

# Find the indices of the nearest data points
i = np.searchsorted(x, x_new) - 1
j = np.searchsorted(y, y_new) - 1

# Compute the weights for each of the four data points
wx = (x[i+1] - x_new) / (x[i+1] - x[i])
wy = (y[j+1] - y_new) / (y[j+1] - y[j])

# Compute the estimated value of the function at the desired point
Pn = (1 - wx)*(1 - wy)*z[i,j] + wx*(1 - wy)*z[i+1,j] + (1 - wx)*wy*z[i,j+1] + wx*wy*z[i+1,j+1]

# Print the estimated value of the function at the desired point
print(f"Pn({x_new}, {y_new}) = {Pn}")

#Output - Pn(1.5, 0.5) = 2.5


				
			

Explanation 

This means that the estimated value of the function at (x=1.5, y=0.5) using bilinear interpolation is approximately 2.5.

Note that in this example, we assume that the data points are evenly spaced on a rectangular grid, which makes it easy to compute the weights for each of the four nearest data points.

If the data points are not evenly spaced, or if they are not on a rectangular grid, then we would need to use a different method to compute the weights.

More Related Stuff