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
p=polyfit(x,y,3) % polynomial as a vector from max power to less one.
-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
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
% Define an Inline function
% More points to represent the original function
plot(xOrig,yOrig,'-.g') %original function
plot(x,y,'o','Marker','o','MarkerFaceColor','red'); %approximation points
Polyfit degree 2 function
We plot together the original function, approximation points and the approximation polynomial.
plot(xOrig,yOrig,'-.g') %original function
plot(x,y,'o','Marker','o','MarkerFaceColor','red'); %approximation points
yyy=polyval(p,xOrig); %values of the polynomial in all the original points
title('Degree 2 approximation');
Polyfit degree 6 function
same as before but using degree 6 polynomial.
plot(x,y,'o','Marker','o','MarkerFaceColor','red');
yyy=polyval(p,xOrig); %values of the polynomial in all the original points
title('Degree 6 approximation');
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
plot(x,y,'o','Marker','o','MarkerFaceColor','red');
yyy=polyval(p,xOrig); %values of the polynomial in all the original points
title('Degree 10 approximation');
1D Polygonal approximation
Use a polygonal approximation function when using more points.
plot(xp,yp,'o','Marker','o','MarkerFaceColor','red');
yyp=interp1(xp,yp,xOrig); %only substitution is possible
title('1D polygonal approximation');
Spline
Use a spline approximation function when using more points.
plot(xs,ys,'o','Marker','o','MarkerFaceColor','red');
yys=spline(xs,ys,xOrig); %only substitution is possible
title('1D spline approximation');
Subplot images
This can help to present results together
% Define an Inline function
% More points to represent the original function
plot(x,y,'o','Marker','o','MarkerFaceColor','red');
p=polyfit(x,y,degree(i));
title(['Degree ', num2str(degree(i)),' approximation']);
title(['Degree ', num2str(degree(i)),' interpolation']);
plot(xp,yp,'o','Marker','o','MarkerFaceColor','red');
yyp=interp1(xp,yp,xOrig); %only substitution is possible
title('1D polygonal approximation');
plot(xs,ys,'o','Marker','o','MarkerFaceColor','red');
yys=spline(xs,ys,xOrig); %only substitution is possible
title('1D spline approximation');
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).
(c)Numerical Factory 2022