% Interpolating a continuous function: the Runge function
% using monomial basis functions with 5 and 11 points
% RungeFcn.m
% Ming Leung, 12/30/02

clear all; hold off;
Runge=inline('1./(1+25*x.*x)');                 % Runge function
xplot=-1:0.01:1; plot(xplot,Runge(xplot),'k-'); % plot the function 
hold on; axis([-1.1 1.1 -0.5 2.1]);             % set plotting area

n=5; tV=(linspace(-1,1,n))'; AS=ones(n,n);      % initializing
yV=Runge(tV); plot(tV,yV,'bo');                 % plot data points
for i=2:n, AS(:,i)=tV.*AS(:,i-1); end,          % create matrix A
xV5=AS\yV;                                      % coefficients

n=11; tV=(linspace(-1,1,n))'; AS=ones(n,n);     % initializing
yV=Runge(tV); plot(tV,yV,'rx');                 % plot data points
for i=2:n, AS(:,i)=tV.*AS(:,i-1); end,          % create matrix A
xV11=AS\yV;                                     % coefficients

plot(xplot,polyval(xV5(5:-1:1),xplot),'b-', ... 
     xplot,polyval(xV11(11:-1:1),xplot),'r-');  % plot interpolant
legend('5 points','11 points');
xlabel('t'); ylabel('f(t)');                    % labels & title 
title('Interpolating a continuous function using 5 & 11 equally spaced points');