% Interpolating a continuous function: the Runge function
% using monomial basis functions with 5 and 11 points
% Also interpolation using cubic spline.
% 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','FontSize',18); ylabel('f(t)','FontSize',18); % labels & title
title('Interpolating a continuous function using 5 & 11 equally spaced points','FontSize',18);

% Interpolating using cubic spline:
nPlotpts=10;
[xPlot,yPlot]=cubicSplineNaturalF(tV,yV,nPlotpts);
plot(xPlot,yPlot,'g-');