Project - Data Handling and Interpolation
Project:
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.
Key Highlights:
- Graphical solutions are more visual and appealing
- Interpolation of data
- Read excel file
- Write to text file
Deliverables:
- Find missing data and save it in text
- Interpolate the data set
- Save your program and attach the same.
- Save txt file and upload in attachment
AIM:
- In this project, we are reading the excel file into the MATLAB program.
- 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.
What is interpolation?
Interpolation is a method of fitting the data points to represent the value of a function. It has various applications in engineering and science, that are used to construct new data points within the range of a discrete data set of known data points or can be used for determining a formula of the function that will pass from the given set of points (x, y).
Types of interpolation methods are:
- Linear interpolation
- Nearest neighbor interpolation or Piecewise constant interpolation
- Polynomial interpolation
- Spline interpolation
- Mimetic interpolation
- Cubic Spline Interpolation
- Linear interpolation:

A linear interpolation is a form of interpolation, which involves the generation of new values based on an existing set of values. Linear interpolation is achieved by geometrically rendering a straight line between two adjacent points on a graph or plane. All points on the line other than the original two can be considered interpolated values.
Linear Interpolation Formula
The formula to calculate linear interpolation is:
Linear Interpolation(y) = y1+(x−x1) (y2−y1) (x2−x1) y1+(x−x1) (y2−y1) (x2−x1)
were,
- x1x1 and y1y1 are the first coordinates
- x2x2 and y2y2 are the second coordinates
- x is the point to perform the interpolation
- y is the interpolated value
- Nearest neighbor interpolation or Piecewise constant interpolation

Interpolation is the problem of approximating the value of a function for a non-given point in some space when given the value of that function in points around (neighboring) that point. The nearest neighbor algorithm selects the value of the nearest point and does not consider the values of neighboring points at all, yielding a piecewise-constant interpolant.
- Polynomial interpolation

Polynomial interpolation is a method of estimating values between known data points. When graphical data contains a gap, but data is available on either side of the gap or at a few specific points within the gap, an estimate of values within the gap can be made by interpolation.
- Spline interpolation

Spline interpolation similar to the Polynomial interpolation x’ uses low-degree polynomials in each of the intervals and chooses the polynomial pieces such that they fit smoothly together. The resulting function is called a spline.
- Mimetic interpolation
mimetic interpolation is a method for interpolating differential forms. In contrast to other interpolation methods, which estimate a field at a location given its values on neighboring points, mimetic interpolation estimates the field's k{\displaystyle k} Kk-form given the field's projection on neighboring grid elements. The grid elements can be grid points as well as cell edges or faces, depending on k=0,1,2, 3…{\displaystyle k=0,1,2,\cdots }.
Mimetic interpolation is particularly relevant in the context of vector and pseudo-vector fields as the method conserves line integrals and fluxes, respectively
- Cubic Spline Interpolation:

Cubic spline interpolation is a special case for Spline interpolation that is used very often to avoid the problem of Runge's phenomenon. This method gives an interpolating polynomial that is smoother and has smaller errors than some other interpolating polynomials such as Lagrange polynomial and Newton polynomial.
M-Script:
clear;
clc;
% interpolation of voltage and current data
% reading excel file data
D=xlsread('VI');
v=D(:,1); % voltage data seperating
i=D(:,2); % current data seperating
xq=[-17.3:0.1:0.9]; % Given interpolation range
yq1=interp1(v,i,xq); % linear interpolation
yq2=interp1(v,i,xq,'spline'); % spline interpolation
% Linear interpolation plotting
figure(1)
plot(v,i,'s',xq,yq1,': .');
title('Linear Interpolation');
xlabel('Voltage (V)');
ylabel('Current (I)');
legend('Actual Value', 'Interpolated value', 'location','Northeast');
grid on;
% spline interpolation plotting
figure(2)
plot(v,i,'s',xq,yq2,'b: .');
title('SPLINE INTERPOLATION');
xlabel('Voltage (V)');
ylabel('Current (I)');
legend('Actual Value', 'Interpolated value', 'location','Northeast');
grid on;
% saving the interpolation data into text file
%opening text file
vol_cu=fopen('VI_output.txt','w');
fprintf(vol_cu,' Voltage Current\n');
%writing voltage & current value
fprintf(vol_cu,'%4.3f %5.3f\n',xq,yq2);
%saving linear interpolation graph
savefig(1,'vilinear.fig');
%saving spline interpolation graph
savefig(2,'vispline.fig');
fclose(vol_cu);
M-script is shown below figure:

Workspace is shown below figure:

text file is shown below:

M-script explanation:
Firstly, clearing the ‘workspace’ memory by giving the ‘clear’ command.
To clear the command window, we used the ‘clc’ command. Then given input excel data file is read by using the ‘xlsread’ function command. The input values of Voltage and current are separated by using this syntax (:,1) and (:,2). The interpolation range is defined as per the given in the project description.
The linear interpolation is calculated by using this syntax – Interp1(); in this calculation, we used the voltage and current data.
For spline interpolation, we use the syntax – Interp1(); and mention the spline. To calculate the spline interpolation. After performing the interpolation, the plot is plotted. To view the plot we use the figure (1) command.
The linear interpolation is plotted using the figure (1) command and plot command by providing the actual values of voltage v and current i and the 's' specified square shape of the marker. And the interpolation of the voltage and current is also drawn in the same plot by using xq and yq1 with an indication of ‘.’
X and Y labels are used to label the x and y-axis. After that for the title of the graph ‘title ()’ command is used. Then for the legend, we used legend () syntax. In legend syntax, we are using actual values, interpolated values, markers, and line styles. The legend is placed at the northeast of the graph. For the shape of the marker, we are using ‘s’ as squares. Then for the grid lines in the graph, we used the “grid” command.
For the spline interpolation, I am using the same commands and changing the line color to Blue.
Then the plotted plots and data are required to save into the text file.
For saving the output data and plots, a text file is created with the name ‘VI_output.txt’ for this we used the command 'fopen ()’. Then the interpolated and spline values are saved by using the ‘fprintf ()’ command. Finally, the plots are saved using the ‘savefig ()’ command.
interpolation data, plots, and text files are created. these are shown below figure:

The Linear interpolation graph is shown below:

The spline interpolation graph is shown below:
