Linear Interpolation in MATLAB
Last Updated :
26 Sep, 2022
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.
Similar Reads
Interpolation in MATLAB
Upsampling can be defined as a process that comprises adding zero-valued samples in between existing original samples in order to increase the sampling rate. Upsampling is also called zero-stuffing. Interpolation:Upsampling with Filtering is called Interpolation. Upsampling adds zero-valued samples
3 min read
Integration in MATLAB
Integration is defined as the process of finding the anti derivative of a function. It is used to calculate area, volume, displacement, and many more. In this article, we will see how to perform integration on expressions in MATLAB. There are two types of Integration: Indefinite integral: Let f(x) b
3 min read
Interpolation in Machine Learning
In machine learning, interpolation refers to the process of estimating unknown values that fall between known data points. This can be useful in various scenarios, such as filling in missing values in a dataset or generating new data points to smooth out a curve. In this article, we are going to exp
7 min read
2D Array Interpolation in MATLAB
In this article, we are going to discuss "2D Array Interpolation" in MATLAB with the help of two linspace() and interp2() functions. Functions Usedlinspace( ) The linspace() function is used for the generation linearly spaced vector. Syntax: linspace(a, b) linspace(a, b, n) Here, linspace(a, b) is
4 min read
Linear Interpolation Formula
Linear Interpolation Formula is a method that constructs the new data points from the given set of data points. Linear interpolation is used for fitting curves using linear polynomials. It finds the unknown values in the table. What is the Linear Interpolation Formula?The formula of linear interpola
4 min read
3D Array Interpolation MATLAB
Interpolation is a method of finding the value of queried data points based on the trend created by existing data points. MATLAB provides many options to perform interpolation on data of N-dimensions. In this article, we shall discuss how to interpolate data in a 3D array with the help of some exam
3 min read
What is Bilinear Interpolation?
Bilinear interpolation is an extension of linear interpolation to a two-dimensional space. It approximates the value at a certain point within a grid by sampling the coordinates with values of four other grid points. The outstanding thing about the interpolation is that this is done in one direction
5 min read
Interpolation in Python
Interpolation in Python refers to the process of estimating unknown values that fall between known values. This concept is commonly used in data analysis, mathematical modeling, and graphical representations. Python provides several ways to perform interpolation, including the use of libraries like
2 min read
Quadratic Interpolation
A quadratic polynomial is used in the mathematical process of quadratic interpolation to estimate values between data points. When you have a set of three data points and wish to estimate the behaviour of a smooth curve passing through these points, you frequently use this formula. To try to predict
12 min read
Find() function in MATLAB
The find() function in MATLAB is used to find the indices and values of non-zero elements or the elements which satisfy a given condition. The relational expression can be used in conjunction with find to find the indices of elements that meet the given condition. It returns a vector that contains t
3 min read