%Lab 2
%Written by Jeannette Spuhler, 2011-09-08
%modified 2011-11-03

%Runges fenomen
%Pohl p. 139
clear all; clc;
x = -1:0.01:1;
f = exp(x)./(1+9*x.^2);


%------------------------------------------------------------------------------------------------------------
%interpolation of grad 6 in the points x = [-1 -2/3 -1/3 0 1/3 2/3 1]
xint = [-1 -2/3 -1/3 0 1/3 2/3 1];
fint = exp(xint)./(1+9*xint.^2);
pint = polyfit(xint,fint,8); 
yint = polyval(pint, x);

%plot
subplot(2, 1, 1);
plot(x, f, '--'); hold on; 
plot(x, yint, 'g', xint,fint, 'go');
title('Lab2 - Runges fenomen: interpolation of order 6'); 
ylabel('y');
xlabel('x');
legend('Function to be interpolated','Interpolation of order 6')

%-------------------------------------------------------------------------------------------------------------
%interpolation of grad 12 in the points x = -1+(1/6)(i-1) i=1, 2, ...13
xint = [];
for i = 1:13
    xint = [xint -1+(1/6)*(i-1)];
    
end
fint = exp(xint)./(1+9*xint.^2);
pint = polyfit(xint,fint,12); 
yint = polyval(pint, x);

%plot
subplot(2, 1, 2);
plot(x, f, '--'); hold on; 
plot(x, yint, 'g', xint,fint, 'go');
title('Lab2 -Runges fenomen: interpolation of order 12'); 
ylabel('y');
xlabel('x');
legend('Function to be interpolated','Interpolation of order 12')

hold off;


