1D-Interpolation of a set of measures

When we collect data from an experiment, we get a set of mesured values . Interpolate these values means to compute a Polynomial (of degree one less than the number of measures) which goes through all the points.
Matlab uses the polyfit and polyval functions for doing that.

Example with 4 points

x=[0,1,2,3];
y=[1,3,5,2];
 
p=polyfit(x,y,3) % polynomial as a vector from max power to less one.
p = 1×4
-0.8333 2.5000 0.3333 1.0000
% p=p(1)x^3+p(2)x^2+p(3)x+p(4)
 
xx=0:0.1:3; %points to plot the polynomial
yy=polyval(p,xx);
 
plot(xx,yy)
hold on;
plot(x,y,'ro')
hold off;

Approximation of a Function

We will use different strategies of approximating a function from only few points.
Let's consider the function:
in the domain [-1,1].
Take some points x=-1:0.2:1 used for approximating this function with a polynomial of different degree.

Compute points

close all
a=-1;
b=1;
% Define an Inline function
f=@(x) 1./(1+25*x.^2);
% Approximation points
x=a:0.2:b;
y=f(x);
% More points to represent the original function
xOrig=a:0.01:b;
yOrig=f(xOrig);
figure()
plot(xOrig,yOrig,'-.g') %original function
hold on;
plot(x,y,'o','Marker','o','MarkerFaceColor','red'); %approximation points
grid

Polyfit degree 2 function

We plot together the original function, approximation points and the approximation polynomial.
figure()
plot(xOrig,yOrig,'-.g') %original function
hold on;
plot(x,y,'o','Marker','o','MarkerFaceColor','red'); %approximation points
p=polyfit(x,y,2);
yyy=polyval(p,xOrig); %values of the polynomial in all the original points
plot(xOrig,yyy,'-.b')
title('Degree 2 approximation');
grid
hold off

Polyfit degree 6 function

same as before but using degree 6 polynomial.
figure()
plot(xOrig,yOrig,'-.g')
hold on;
plot(x,y,'o','Marker','o','MarkerFaceColor','red');
p=polyfit(x,y,6);
yyy=polyval(p,xOrig); %values of the polynomial in all the original points
plot(xOrig,yyy,'-.b')
title('Degree 6 approximation');
grid
hold off

Interpolation Polynomial: Polyfit degree 10 function

same as before but using degree 10 (= npoints -1) polynomial.
One can see the Runge Phenomenon at the beginning and final of the interval
figure()
plot(xOrig,yOrig,'-.g')
hold on;
plot(x,y,'o','Marker','o','MarkerFaceColor','red');
p=polyfit(x,y,10);
yyy=polyval(p,xOrig); %values of the polynomial in all the original points
plot(xOrig,yyy,'-.b')
title('Degree 10 approximation');
grid
hold off

1D Polygonal approximation

Use a polygonal approximation function when using more points.
figure()
xp=a:0.1:b;
yp=f(xp);
plot(xp,yp,'o','Marker','o','MarkerFaceColor','red');
hold on;
plot(xOrig,yOrig,'-.g')
yyp=interp1(xp,yp,xOrig); %only substitution is possible
plot(xOrig,yyp,'b')
title('1D polygonal approximation');
grid
hold off

Spline

Use a spline approximation function when using more points.
figure()
xs=a:0.1:b;
ys=f(xs);
plot(xs,ys,'o','Marker','o','MarkerFaceColor','red');
hold on;
plot(xOrig,yOrig,'-.g')
yys=spline(xs,ys,xOrig); %only substitution is possible
plot(xOrig,yys,'b')
title('1D spline approximation');
grid
hold off

Subplot images

This can help to present results together
a=-1;
b=1;
% Define an Inline function
f=@(x) 1./(1+25*x.^2);
% Approximation points
x=a:0.2:b;
y=f(x);
% More points to represent the original function
xOrig=a:0.01:b;
yOrig=f(xOrig);
figure()
degree=[4,6,8,10];
for i=1:size(degree,2)
subplot(3,2,i)
plot(x,y,'o','Marker','o','MarkerFaceColor','red');
hold on;
plot(xOrig,yOrig,'-.g')
p=polyfit(x,y,degree(i));
yyy=polyval(p,xOrig);
plot(xOrig,yyy,'-.b')
if (degree(i) < 10)
title(['Degree ', num2str(degree(i)),' approximation']);
else
title(['Degree ', num2str(degree(i)),' interpolation']);
end
axis([-1,1,-0.2,1])
grid
hold off
end
% Polygonal
subplot(3,2,5)
xp=a:0.1:b;
yp=f(xp);
plot(xp,yp,'o','Marker','o','MarkerFaceColor','red');
hold on;
plot(xOrig,yOrig,'-.')
yyp=interp1(xp,yp,xOrig); %only substitution is possible
plot(xOrig,yyp,'b')
title('1D polygonal approximation');
grid
hold off
%Spline
subplot(3,2,6)
xs=a:0.1:b;
ys=f(xs);
plot(xs,ys,'o','Marker','o','MarkerFaceColor','red');
hold on;
plot(xOrig,yOrig,'-.')
yys=spline(xs,ys,xOrig); %only substitution is possible
plot(xOrig,yys,'b')
title('1D spline approximation');
grid
hold off

Exercise:

Compute the mean and maximum error between the original curve and their approximation according to the polynomial degree for the measure points x=a:0.2:b and y=f(x). (Use all plotted points, xx=a:0.01:b to compute errors).
Degree meanError maxError
3.0000 0.1583 0.5159
5.0000 0.1119 0.3448
7.0000 0.0797 0.2154
10.000 0.2899 1.9156
(c)Numerical Factory 2022