Project - Data Handling and Interpolation
In a laboratory experiment, data of voltage and current has been recorded and the same is attached in an excel file ( Click on this link VI.xlsx ). Read the excel file in the Matlab program and perform the interpolation in the range of -17.3:0.1:0.9. Generate the interactive plot for the interpolated data and store them in a text file.
Interpolation is mainly used in mathematics, scale the images and digital signal processing methods. It is a procedure to estimate the points that lie within a defined range. Interpolation methods can be used in creating various models in statistics. In this topic, we are going to learn about MATLAB Interpolation.
Working of Interpolation in Matlab with Syntax and Examples:
In Matlab, interpolation is the procedure of including new points within a defined range or a given set of points. It is used to find the missing data in the data set, smoothen the given data set or predict the outcome of the given data set. Various functions are associated with interpolation techniques. Here, we will mainly discuss one-dimensional interpolation or linear interpolation syntax:
aq=interp1(x, a, xq): This returns the interpolated values of the function (one-dimensional) with the help of the linear interpolation method. The input ‘x’ is a vector that contains every sample point, a has the defined values and xq contains the coordinates. If there are many values, then a can be declared in an array.
aq=interp1(x, a, xq, method): Here we can change the interpolation method, which we will discuss later. There are many interpolation methods like nearest, linear, next, previous, cubic, v5cubic, pchip, spline or makima. The default method used is always linear.
aq=interp1(x, a, xq, method, extrapolation method): Extrapolation can be defined in the syntax if we want to check the points that are declared outside the defined value of x. We can mention extrapolation to ‘extrap’ if we want to apply the extrapolation algorithm to the points.
aq=interp1(a, xq): This returns the interpolated values and a set of coordinates are assumed. The default set of numbers falls under a specific range of 1 to n, where n is decided according to the shape of a. If a is a vector, then the default set of points lies within a range of 1 to the length of a. If a is an array, then the default set of points lies within a range of 1 to size(a,1).
There are various types of interpolation methods in Matlab.
Linear Interpolation Method: This is the default interpolation method used. It helps find the interpolated values at the query point which is based on the values of grid points in each dimension defined. There are certain limitations of this method like 2 points are at least required to use Linear Interpolation. Computation time and memory allocation is more as compared to the nearest algorithm method.

Nearest Interpolation method: This method is used to find the interpolated values at the query point with the help of the nearest element at the sample grid point. It also requires at least 2 points to find the interpolated values. It has the fastest computation time.
Next Interpolation Method: This method is used to find the interpolated values at the query point with the help of the next element at the sample grid point. It also requires at least 2 points to find the interpolated values. It has the fastest computation time.
Previous Interpolation Method: This method is used to find the interpolated values at the query point with the help of the previous element at the sample grid point. It also requires at least 2 points to find the interpolated values. It has the fastest computation time.
Pchip Interpolation Method:

It is known as Shape-preserving piecewise cubic interpolation method, where the interpolated values are defined by the shape-preserving piecewise method. It requires at least four points to find the interpolated values. Memory allocation and computation time is more than the Linear Interpolation Method.
Cubic Interpolation Method:
This functions in the same way as defined in the above pchip Interpolation Method. Memory allocation and computation time is also the same as the pchip Interpolation method.
Spline Interpolation Method:

This method is used to find the interpolated values using the cubic interpolation of the values. It requires at least four points to find the interpolated values. Memory allocation and computation time is more than the pchip Interpolation Method.
The m-script code is shown below
clear all
clc
A=xlsread('VI'); %reading excel file
v=A(:,1); %separating voltage value
i=A(:,2); %separating current value
xq=[-17.3:0.1:0.9]; %setting interpolation range
yq1=interp1(v,i,xq); %linear interpolated value
yq2=interp1(v,i,xq,'spline'); %spline interpolated value
figure(1)
plot(v,i,'s',xq,yq1,': .'); %ploting linear interpolation
title('Linear interpolation');
xlabel('Voltage [V]');
ylabel('Current [I]');
legend('Actual value','Interpolated value','Location','southeast');
grid on;
figure(2)
plot(v,i,'s',xq,yq2,'m: .');
title('Spline interpolation'); %ploting spline interpolation
xlabel('Voltage [V]');
ylabel('Current [I]');
legend('Actual value','Interpolated value','Location','southeast');
grid on;
val=fopen('vi.txt','w'); %opening text file
fprintf(val,' Voltage Current\n');
fprintf(val,'%4.3f %5.3f\n',xq,yq2); %writing voltage & current value
savefig(1,'vilinear.fig'); %saving linear interpolation graph
savefig(2,'vispline.fig'); %saving spline interpolation graph
fclose(val);
Code explanation:
The memory and storage are cleared using, ‘clear all’ and ‘clc’ function command
The excel file is read using ‘xlsread ()’ function command
After reading the values of current and voltages are seperated.by using’(:,1)’ and ‘(:,2)’ commands
Then the range of interpolation which has to be done is made as per the question.
The linear interpolation is done using the function ‘Interp1()’ by providing the v, i & interpolation range. Similarly spline interpolation is carried out using the ‘interp1()’ function specifying the method of interpolation as 'spline'.
After performing the interpolation, the plotting is done.
The figure is brought up using the ‘figure ()’ command and plot is done using the ‘plot ()’ command.
The linear interpolation is ploted using the figure(1) command and plot command by providing the actual values of voltage v and current i and the 's' specified the square shape of marker. And the interpolation of the voltage and current also drawn in the same plot by using xq and yq1 with indication of ‘.’
The ‘xlabel ()’ & ‘ylabel ()’ are used for labelling x-axis and y-axis,
Title () command is used for giving the title for the plot
legend () command is used for distinguish the curves, and the it is displayed in the bottom right side by using ‘location’ and then ‘southeast’.
grid is set to on by grid on command.
The spline interpolation curve is ploted with figure(2) and plot command by providing the actual values of voltage v and current i and the 's’ specified the square shape of marker.
Next is the interpolated values of voltage and current which will be shown by small and big dot of magenta color, the m specified indicates the magenta in the plot.
The xlabe() & ylabel() are used for labelling x-axis and y-axis.
title() command is used for giving the title for plot,
legend() l command is used for distinguish the curves, and the it is displayed in the bottom right side by using ‘location’ and then ‘southeast’.
grid is set to on by grid on command.
Now the saving of data and plot created is done, for that a text file called 'vi.txt' is opened in write mode using ‘fopen ()’ command.
By using fprintf() command the interpolated values of voltage and current after performing spline interpolation is saved as text file.
Also the plot of linear and spline interpolation were saved in figure format using the savefig() command.
Workspace:

After running the polts are getting is shown below for linear interpolation

In the plot i observed that the square indicates the acctual values of voltage and current and dot '.' indicates the interpolation of voltage and current.
Spline interpolation plot is shown below

In the plot i observed that the square indicates the acctual values of voltage and current and dot '.' indicates the spline interpolation of voltage and current.