% Generates a Series of Lines:  Function, Spline, and Error of Spline Fits to m Points of 1/(1+25x^2)
% David Bloore, July 2011

% user-defined parameters

m=input('Lower bound of m?');
r=input('Length of interval of m?');
d=input('Number of discrete points along x axis?');

% blank matrices for plot3

M=linspace(m,m+r,r+1);
X1=zeros(d,r+1);
Y1=zeros(d,r+1);
Z1=zeros(d,r+1);
X2=zeros(d,r+1);
Y2=zeros(d,r+1);
Z2=zeros(d,r+1);
X3=zeros(d,r+1);
Y3=zeros(d,r+1);
Z3=zeros(d,r+1);

% domain and range of interest

x=linspace(-1,1,d);
f=(1+25*x.^2).^-1;

% instead of just assigning errors to Z, we assign f(x) to Z1, the spline to Z2, and the error to Z3

counter=1;

while counter<r+2;
    
    mtemp=m+counter-1;
    Mtemp=mtemp*ones(1,d);
    
    xin=linspace(-1+2*(mtemp+1)^-1,1-2*(mtemp+1)^-1,mtemp);
    zin=(1+25*xin.^2).^-1;
    
    pp=spline(xin,[0 zin 0]);
    zBe=ppval(pp,x);
    err=zBe-f;
    
    X1(1:d,counter)=x(1:d);
    Y1(1:d,counter)=Mtemp;
    Z1(1:d,counter)=f(1:d);
    X2(1:d,counter)=x(1:d);
    Y2(1:d,counter)=Mtemp;
    Z2(1:d,counter)=zBe(1:d);
    X3(1:d,counter)=x(1:d);
    Y3(1:d,counter)=Mtemp;
    Z3(1:d,counter)=err(1:d);
    
    counter=counter+1;
end

% each column of Z corresponds to a line, same for X,Y--so the first column of each matrix produces the first line, and so on
% plot3 takes multiple arguments, just like plot!

plot3(X1,Y1,Z1,'-',X2,Y2,Z2,'--',X3,Y3,Z3,':');
xlabel('-1 \leq x \leq 1');
ylabel('Number of Points Used for Spline Fit');
zlabel('Error = Interpolant fit - 1/(1+25x^2)');
title('Series of Lines:  Function, Spline, and Error of Snterpolant Fits to m Points of 1/(1+25x^2)');
set(gca,'XTick',-1:1/(m+1):1);
set(gca,'YTick',m:m+r);
set(gca,'YDir','reverse');
view(-60,35);
% zlim([-2 2]);
grid on;
hidden off;