function runge_example
% Runge's Counter Example Against High Degree Polynomials
% R(x) = 1/(1+25*x^2); on [-1,+1]
clc;
% Exact Discretization:
m = 100; r(1) = -1; r(m+1) = +1; dr = 2/m; z(1) = R(r(1)); z(m+1) = R(r(m+1));
for i = 2:m; r(i) = -1+(i-1)*dr; z(i) = R(r(i)); end
% Low degree case:
n = 6; x(1) = -1; x(n+1) = +1; dx = 2/n; y(1) = R(x(1)); y(n+1) = R(x(n+1));
for i = 2:n; x(i) = -1+(i-1)*dx; y(i) = R(x(i)); end
pl = polyfit(x,y,n);
yl = polyval(pl,r);
figure(1);
plot(r,yl,'r--',r,z)
grid on
htitle=title('Low Degree (6) Polynomial Fit for Runge''s Function');
hxlabel=xlabel('x');
hylabel=ylabel('y');
set(htitle,'Fontsize',16,'FontName','Times New Roman','FontWeight','Bold')
set(hxlabel,'Fontsize',16,'FontName','Times New Roman','FontWeight','Bold')
set(hylabel,'Fontsize',16,'FontName','Times New Roman','FontWeight','Bold')
% high degree case:
n = 16; x(1) = -1; x(n+1) = +1; dx = 2/n; y(1) = R(x(1)); y(n+1) = R(x(n+1));
for i = 2:n; x(i) = -1+(i-1)*dx; y(i) = R(x(i)); end
ph = polyfit(x,y,n);
yh = polyval(ph,r);
figure(2);
plot(r,yh,'r--',r,z)
grid on
htitle=title('High Degree (16) Polynomial Fit for Runge''s Function');
hxlabel=xlabel('x');
hylabel=ylabel('y');
set(htitle,'Fontsize',16,'FontName','Times New Roman','FontWeight','Bold')
set(hxlabel,'Fontsize',16,'FontName','Times New Roman','FontWeight','Bold')
set(hylabel,'Fontsize',16,'FontName','Times New Roman','FontWeight','Bold')
% Runge's Function
function rv = R(x)
rv = 1/(1+25*x^2);
%
