Академический Документы
Профессиональный Документы
Культура Документы
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.
Contents
Example with 4 points
Approximation of a Function
Compute points
Polyfit degree 3 function
Polyfit degree 7 function
1D Polygonal approximation
Spline
Subplot images
Exercise:
x=[0,1,2,3];
y=[1,3,5,2];
plot(xx,yy)
hold on;
plot(x,y,'ro')
hold off;
p=
Approximation of a Function
We will use different strategies of approximating a function from only few points.
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,'-.') %original function
hold on;
plot(x,y,'o','Marker','o','MarkerFaceColor','red'); %approximation points
figure()
plot(xOrig,yOrig,'-.') %original function
hold on;
plot(x,y,'o','Marker','o','MarkerFaceColor','red'); %approximation points
p=polyfit(x,y,3);
yyy=polyval(p,xOrig); %values of the polynomial in all the original points
plot(xOrig,yyy,'-.b')
title('Degree 3 approximation');
hold off
Polyfit degree 7 function
same as before but using degree 7 polynomial.
figure()
plot(xOrig,yOrig,'-.')
hold on;
plot(x,y,'o','Marker','o','MarkerFaceColor','red');
p=polyfit(x,y,7);
yyy=polyval(p,xOrig); %values of the polynomial in all the original points
plot(xOrig,yyy,'-.b')
title('Degree 7 approximation');
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,'-.')
yyp=interp1(xp,yp,xOrig); %only substitution is possible
plot(xOrig,yyp,'b')
title('1D polygonal approximation');
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,'-.')
yys=spline(xs,ys,xOrig); %only substitution is possible
plot(xOrig,yys,'b')
title('1D spline approximation');
hold off
Subplot images
This can help to present results together
figure()
degree=[3,5,7,10];
for i=1:size(degree,2)
subplot(3,2,i)
plot(x,y,'o','Marker','o','MarkerFaceColor','red');
hold on;
plot(xOrig,yOrig,'-.')
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])
hold off
end
% Polygonal
subplot(3,2,5)
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');
hold off
%Spline
subplot(3,2,6)
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');
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).