% Example 7.2
% Interpolating Polynomial for Runge Function

clear all
close all
clc

n=10;

xp = linspace(-1,1,n+1);
yp = [0.038 0.058 0.100 0.200 0.500 1.000 0.500 0.200 0.100 0.0580 0.038];
a = polyfit(xp,yp,n);

xplot = linspace(-1,1);
yplot = 1./(1+25.*xplot.^2);
polyplot = 0;
for i=1:n+1    
    polyplot = polyplot + a(n+2-i).*xplot.^(i-1); %polyfit orders coefficients from highest
end                                                 % power of x

figure
hold on
plot(xplot,polyplot,'LineWidth',2)
plot(xplot,yplot,'LineWidth',2)
plot(xp,yp,'ok','MarkerSize',7,'MarkerFaceColor','k')
legend('Interpolating Polynomial','1/(1+25x^2)','Data Points','Location','southoutside')
xlim([-1.1,1.1])
ylim([-0.5,2])
xlabel('x')
ylabel('y')
fplot(0,'-k',xlim)