# 2D Interpolation Using MATLAB

This tutorial will discuss finding the interpolation of 2D data using the `interp2()`

function in MATLAB.

## Find the Interpolation of 2D Data Using the `interp2()`

Function in MATLAB

We can use MATLAB’s built-in function `interp2()`

function to find the interpolation of 2D gridded data in a mesh grid format. For example, let’s interpolate over a grid using the default method.

See the code below.

```
[x,y] = meshgrid(-3:3);
v = peaks(x,y);
figure
surf(x,y,v)
title('Original Sampling');
[xq,yq] = meshgrid(-3:0.25:3);
vq = interp2(x,y,v,xq,yq);
figure
surf(xq,yq,vq);
title('Linear Interpolation Using Finer Grid');
```

Output:

In the output, the image on the right is the result of interpolation using the default method.

As you can see, the output image has more data points as compared with the original one. The x and y vector is the input matrices, and they should have the same size.

The v vector contains the sample values. It can contain real or complex values if v contains complex numbers.

Then, the `interp2()`

function will interpolate the real and imaginary parts separately. The vector `xq`

and `yq`

contain the query points, which can be real scalars, vectors, matrices, or arrays.

We can also change the interpolation method by defining the new method as a string in the `interp2()`

function like we can set cubic, nearest, or spline method for interpolation. We can use interpolation to refine a grayscale image.

If an image has fewer pixels, the details inside the image will not be visible. In this case, we can use interpolation to increase the number of pixels to understand the image.

For example, let’s load a grayscale image into MATLAB and refine it using the `interp2()`

function. See the code below.

```
load flujet.mat
colormap gray
v = single(X(200:300,1:25));
figure
imagesc(v);
axis off
title('Original Image')
vq = interp2(v,5);
figure
imagesc(vq);
axis off
title('Linear Interpolation')
```

Output:

In the output, the right image is refined using linear interpolation. The function `single()`

is used to convert the values to single precision.

**Ammar Ali**