Open In App

Linear Interpolation in MATLAB

Improve
Improve
Like Article
Like
Save
Share
Report

Interpolation is a numerical method of finding new data points by finding a pattern in a given set of discrete data points. There are various types and methods of interpolation in the field of Numerical Analysis such as linear interpolation, cubic interpolation, spline interpolation, etc. 

The key point to be noted when interpolating is that this method only works when the queried data point lies between the range of discrete data points provided. Consider the example that three data points are given, (1,1), (2,4), (3,9) so, interpolation can only find the value of the unknown function(the second data in brackets) when the queried data point say q lies between 1 and 3. If we want to find a value out of the range (1,3) then, they would need to use the method of extrapolation.

Now, MATLAB offers various functions to perform different types of interpolation. However, we would see how linear interpolation works in MATLAB.

Linear Interpolation in MATLAB:

The function to perform linear interpolation, MATLAB provides the interp1() function. 

Syntax:

interp1(<sample points>, <value of unknown function on sample points>, 
<query points>, <method of interpolation>)

Here, a sample point is a set of data points, which could be an array or a vector. The value of the unknown function on sample points is also a set that has the same size/length as the sample points. The third argument could be a set or a single value that has the data at which interpolation is to be performed. The fourth argument is optional, and it tells MATLAB which method of interpolation is to be used. By default, it is set to linear thus, we would not change it.

Let us see some examples to understand this better.

Example 1:

Matlab




% Matlab code for Linear Interpolation
% sample data points
x = linspace(1,23,1000);    
  
% Value of unknown function 
% at sample data
y = cos(x);            
  
% Queried value
vq = pi;                    
  
% linear interpolation 
interp1(x,y,vq)


Output:

 

Here, we generate a linearly spaced vector of 1000 points in range (1,23) and pass it as sample points. Then, we take the values of the unknown function on these points as y=cos(x), which is a vector of the same length as x. The queried value vq=pi. 

Let’s see how the interpolation looks graphically on the same example with fewer sample data points.

Example 2:

Matlab




% Matlab code for Linear Interpolation
  x = linspace(1,23,13);
  y = cos(x);
  vq = pi;
  xq = interp1(x,y,vq);
  plot(x,y,'o-',vq,xq,'black*')


Output:

 

We are plotting the same data just with lesser sample points and the black * at the left bottom of the plot represents the interpolated query point.

Note: Here the interpolated value (-0.800332) is different from the previous example (-1.0) because the number of the sample data points is 13 which is much lesser than 1000. This tells us that the higher the number of sample data, the more accurate result is interpolated. This is a key basis in Machine Learning.

Conclusion:

This article showed how to perform linear interpolation in MATLAB, and also explained the same graphically.



Last Updated : 26 Sep, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads