Challenge

Project - Data Handling and Interpolation

AIM

  1. Read the excel file in the Matlab program and perform the interpolation and to generate plot for the interpolated data and store them in a text file.

THEORY

Interpolation

Interpolation is a method of deriving a simple function from the given discrete data set such that the function passes through the provided data points. This helps to determine the data points in between the given data ones. This method is always needed to compute the value of a function for an intermediate value of the independent function. In short, interpolation is a process of determining the unknown values that lie in between the known data points. It is mostly used to predict the unknown values for any geographical related data points such as noise level, rainfall, elevation, and so on.

A closely related problem is the approximation of a complicated function by a simple function. Suppose the formula for some given function is known, but too complicated to evaluate efficiently. A few data points from the original function can be interpolated to produce a simpler function which is still fairly close to the original. The resulting gain in simplicity may outweigh the loss from interpolation error and give better performance in calculation process.

Types of interpolation method are

  1. Linear interpolation
  2. Nearest neighbour interpolation or Piecewise constant interpolation
  3. Polynomial interpolation
  4. Spline interpolation
  5. Mimetic interpolation

1. Linear interpolation

The Linear Interpolation Method applies a distinct linear polynomial between each pair of the given data points for the curves, or within the sets of three points

The unknown value on the data points can be found using the linear interpolation formula.

2. Nearest Nehibour Method

In this method the value of an interpolated point is inserted to the value of the most adjacent data point.Therefore the nearest neighbour method does not produce any new data points.

3. Polynomial interpolation

Polynomial interpolation is a generalization of linear interpolation. Note that the linear interpolant is a linear function. We now replace this interpolant with a polynomial of higher degree.

if we have n data points, there is exactly one polynomial of degree at most n−1 going through all the data points. The interpolation error is proportional to the distance between the data points to the power n. Furthermore, the interpolant is a polynomial and thus infinitely differentiable. So, we see that polynomial interpolation overcomes most of the problems of linear interpolation.

4. Spline Interpolation

The linear interpolation uses a linear function for each of intervals [xk,xk+1]. Spline interpolation 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.

For instance, the natural cubic spline is piecewise cubic and twice continuously differentiable. Furthermore, its second derivative is zero at the end points.

5. Mimetic interpolation

A key feature of mimetic interpolation is that vector calculus identites are satisfied, including Stoke's theorem and the divergence theorem. As a result, mimetic interpolation conserves line, area and volume integrals.Conservation of line integrals might be desirable when interpolating the electric field, for instance, since the line integral gives the electric potential difference at the endpoints of the integration path.Mimetic interpolation ensures that the error of estimating the line integral of an electric field is the same as the error obtained by interpolating the potential at the end points of the integration path, regardless of the length of the integration path.

Linear, bilinear and trilinear interpolation are also considered mimetic, even if it is the field values that are conserved (not the integral of the field). Apart from linear interpolation, area weighted interpolation can be considered as one of the first mimetic interpolation method to have been developed

interp1

It is used in 1-D data interpolation.

syntax

vq = interp1(x,v,xq) returns interpolated values of a 1-D function at specific query points using linear interpolation. Vector x contains the sample points, and v contains the corresponding values, v(x). Vector xq contains the coordinates of the query points.

If you have multiple sets of data that are sampled at the same point coordinates, then you can pass v as an array. Each column of array v contains a different set of 1-D sample values.

vq = interp1(x,v,xq,method) specifies an alternative interpolation method: 'linear', 'nearest', 'next', 'previous', 'pchip', 'cubic', 'v5cubic', 'makima', or 'spline'. The default method is 'linear'.

vq = interp1(x,v,xq,method,extrapolation) specifies a strategy for evaluating points that lie outside the domain of x. Set extrapolation to 'extrap' when you want to use the method algorithm for extrapolation. Alternatively, you can specify a scalar value, in which case, interp1 returns that value for all points outside the domain of x.

vq = interp1(v,xq) returns interpolated values and assumes a default set of sample point coordinates. The default points are the sequence of numbers from 1 to n, where n depends on the shape of v:

  • When v is a vector, the default points are 1:length(v).

  • When v is an array, the default points are 1:size(v,1).

vq = interp1(v,xq,method) specifies any of the alternative interpolation methods and uses the default sample points.

vq = interp1(v,xq,method,extrapolation) specifies an extrapolation strategy and uses the default sample points.

pp = interp1(x,v,method,'pp') returns the piecewise polynomial form of v(x) using the method algorithm.

interp2

It is used in interpolation for 2-D gridded data in meshgrid format.

Syntax

Vq = interp2(X,Y,V,Xq,Yq) returns interpolated values of a function of two variables at specific query points using linear interpolation. The results always pass through the original sampling of the function. X and Y contain the coordinates of the sample points. V contains the corresponding function values at each sample point. Xq and Yq contain the coordinates of the query points.
Vq = interp2(V,Xq,Yq) assumes a default grid of sample points. The default grid points cover the rectangular region, X=1:n and Y=1:m, where [m,n] = size(V). Use this syntax when you want to conserve memory and are not concerned about the absolute distances between points.
Vq = interp2(V) returns the interpolated values on a refined grid formed by dividing the interval between sample values once in each dimension.
Vq = interp2(V,k) returns the interpolated values on a refined grid formed by repeatedly halving the intervals k times in each dimension. This results in 2^k-1 interpolated points between sample values.
Vq = interp2(___,method) specifies an alternative interpolation method: 'linear', 'nearest', 'cubic', 'makima', or 'spline'. The default method is 'linear'.
Vq = interp2(___,method,extrapval) also specifies extrapval, a scalar value that is assigned to all queries that lie outside the domain of the sample points.

If you omit the extrapval argument for queries outside the domain of the sample points, then based on the method argument interp2 returns one of the following:

  • Extrapolated values for the 'spline' and 'makima' methods

  • NaN values for other interpolation methods

PROGRAM CODE

close all
clear all
clc

vi=xlsread('VI');               %reading excel file
v=vi(:,1);                      %separating voltage value
i=vi(:,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,'d',xq,yq1,':.');      %ploting linear interpolation
title('Linear interpolation');
xlabel('Voltage [V]');
ylabel('Current [I]');
legend('Actual value','Interpolated value','Location','NorthWest');
grid on;

figure(2)
plot(v,i,'d',xq,yq2,'m:.');
title('Spline interpolation');  %ploting spline interpolation 
xlabel('Voltage [V]');
ylabel('Current [I]');
legend('Actual value','Interpolated value','Location','NorthWest');
grid on;

val=fopen('vi.txt','w');                 %opening text file
fprintf(val,' Voltage    Current\n');
fprintf(val,'%3.3f    %3.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 EXPLAINATION

  • The memory and storage are cleared using close all, clear all and clc function
  • The excel file is read using xlsread() function.
  • After reading the values of current and voltages were seperated.
  • 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 x,y & 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 'd' specified the shape of marker.Next is the interpolated values of voltage and current which will be shown by small and big dot in the plot.
  • The xlabel() & ylabel() are used for labeling x-axis and y-axis,title() command is used for giving the title for plot,legend() command is used for distinguish the curves, 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 'd' specified the shape of marker.Next is the interpolated values of voltage and current which will be shown by small and big dotn of magantha color,the m specified indicates the magantha in the plot.
  • The xlabel() & ylabel() are used for labeling x-axis and y-axis,title() command is used for giving the title for plot,legend() command is used for distinguish the curves, 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.
  • In the linear plot it is obeserved that the last 3 interpolation results were not a number or other words they were unreadable
  • Whereas in spline interpolation all the values of interpolation results were obtained.

WORKSPACE

OUTPUT

Linear interpolation

Spline interpolation

RESULT

  1. Excel file was read in matlab and performed linear and spline interpolation.
  2. After comparision of the two interpolation method it was found that the spline interpolation is more accurate.
  3. Generated the plot of the interpolated data and saved the data in text file.

REFERENCE

https://in.mathworks.com/help/matlab/interpolation.html#:~:text=Interpolation%20is%20a%20technique%20for,grid%20and%20scattered%20data%20points.

https://en.wikipedia.org/wiki/Interpolation

https://www.vedantu.com/maths/interpolation

Your portfolio, next

Ship work like this
in your own course.

Talk to a career counsellor

Free 20-minute call. We'll map the right course to where you want to work.

By submitting you agree to our Terms and Privacy Policy.