# Bilinear Interpolation in Python

Vaibhhav Khetarpal Jan 30, 2023 Feb 22, 2022

A Linear Interpolation comes into use for curve fitting with the help of linear polynomials.

The Bilinear Interpolation is an extension of Linear Interpolation that is utilized to interpolate functions of any two given variables with the help of linear interpolation.

Let us demonstrate the different ways available to implement Bilinear Interpolation in Python.

## Create a `User-Defined` Function to Implement Bilinear Interpolation in Python

Here we create a `user-defined` function associated with four points and utilize Bilinear Interpolation in Python.

``````def bilinterpol(a, b, pts):
i = sorted(pts)
(a1, b1, x11), (_a1, b2, x12), (a2, _b1, x21), (_a2, _b2, x22) = i
if a1 != _a1 or a2 != _a2 or b1 != _b1 or b2 != _b2:
print('The given points do not form a rectangle')
if not a1 <= a <= a2 or not b1 <= b <= b2:
print('The (a, b) coordinates are not within the rectangle')
Y = (x11 * (a2 - a) * (b2 - b) +
x21 * (a - a1) * (b2 - b) +
x12 * (a2 - a) * (b - b1) +
x22 * (a - a1) * (b - b1)
) / ((a2 - a1) * (b2 - b1) + 0.0)
return Y
pts = [(0, 1, 12),
(4, 1, 0),
(0, 3, -4),
(4, 3, 8),
]
print(bilinterpol(2,3, pts))
``````

Output:

``````2.0
``````

## Use the `scipy.interpolate.interp2d()` to Implement Bilinear Interpolation in Python

The `SciPy` library, an abbreviation for `Scientific Python`, is open-source.

Composed of a large scope of utility functions that help with Data Science, optimization, interpolation, linear algebra, signal processing, etc. The `SciPy` library uses and depends on the `NumPy` library.

This method can handle significantly complex problems that deal with `NumPy` arrays. The `scipy.interpolate.interp2d()` function in our case implements bilinear interpolation over a 2d grid.

Syntax:

``````scipy.interpolate.interp2d(x, y, z, kind='linear', copy=True, bounds_error=False, fill_value=None)
``````

The function contains three prominent parameters that need to be understood to properly utilize it.

• The `x, y` are both contains array-like values that depict the data points of the given coordinates. `x` represents the column coordinates. In contrast, `y` represents the `row` coordinates, considering that the data points lie on the grid.
• The `z` contains array-like values, and it specifies the value of the function that is to be interpolated with the given set of data points.
• The `kind` specifies kinds of interpolation to be used. It could be `linear`, `cubic`, or `quintic`. The value defaults `linear` if no argument is passed.

The following code uses the `scipy.interpolate.interp2d()` to implement Bilinear Interpolation in Python.

``````from scipy import interpolate
import numpy as np
import matplotlib.pyplot as plt
x = np.arange(-15.01, 15.01, 1.00)
y = np.arange(-15.01, 15.01, 1.00)
xx, yy = np.meshgrid(x, y)
z = np.cos(xx**2+yy**2)
f = interpolate.interp2d(x, y, z, kind='quintic')
xnew = np.arange(-15.01, 15.01, 1e-2)
ynew = np.arange(-15.01, 15.01, 1e-2)
znew = f(xnew, ynew)
plt.plot(x, z[0, :], 'ro-', xnew, znew[0, :], 'b-')
plt.show()
``````

Output:

Code Explanation:

• All the three essential libraries, namely `SciPy`, `NumPyc`, and `MatPlotLib`, are imported to the code.
• The `numpy.arrange()` function is then utilized to insert values into the variables `x and y` in the form of arrays.
• Moving on to the `meshgrid()` function that generates a `1d` array with `x and y` as cartesian indexes.
• Then, the `cos()` function is utilized to find the cosine value, which determines the value of `z`, the main function in the code.
• Lastly, the result is depicted with the help of the `matplotlib` library functions.

Vaibhhav is an IT professional who has a strong-hold in Python programming and various projects under his belt. He has an eagerness to discover new things and is a quick learner.