% Least squares results:

x = [0:.01:5]';
a = [ ...
  -5.6534e-01
   7.5154e-02];
n = length(a);
f2 = ( (x*ones(1,n)).^(ones(length(x),1)*[1:n]) )*a+1;

a = [ ...
  -8.1824e-01
   2.2807e-01
  -2.0876e-02];
n = length(a);
f3 = ( (x*ones(1,n)).^(ones(length(x),1)*[1:n]) )*a+1;

a = [ ...
  -9.4296e-01
   3.7189e-01
  -6.8459e-02
   4.7715e-03];
n = length(a);
f4 = ( (x*ones(1,n)).^(ones(length(x),1)*[1:n]) )*a+1;

plot(x,exp(-x),x,f2,'--',x,f3,':',x,f4,'-.')
%legend(' Exact',' n = 2',' n = 3',' n = 4')
title('Figure 1. Least squares approximations to first-order PDE')
xlabel('x')
ylabel('f(x)')
axis([0,5,-.2,1])
print -deps fig1




a=[ ...
  -1.0000e+00
   1.4286e-01];
n = length(a);
f2 = ( (x*ones(1,n)).^(ones(length(x),1)*[1:n]) )*a+1;

a=[ ...
  -1.0000e+00
   3.1343e-01
  -2.9851e-02];
n = length(a);
f3 = ( (x*ones(1,n)).^(ones(length(x),1)*[1:n]) )*a+1;

a=[ ...
  -1.0000e+00
   4.1967e-01
  -8.0977e-02
   5.7841e-03];
n = length(a);
f4 = ( (x*ones(1,n)).^(ones(length(x),1)*[1:n]) )*a+1;

figure(2)
plot(x,exp(-x),x,f2,'--',x,f3,':',x,f4,'-.')
%legend(' Exact',' n = 2',' n = 3',' n = 4')
title('Figure 2. Collocation approximations to first-order PDE')
xlabel('x')
ylabel('f(x)')
print -deps fig2

a=[ ...
  -5.1613e-01
   6.4516e-02];
n = length(a);
f2 = ( (x*ones(1,n)).^(ones(length(x),1)*[1:n]) )*a+1;

a = [ ...
  -7.5728e-01
   1.9660e-01
  -1.6990e-02];
n = length(a);
f3 = ( (x*ones(1,n)).^(ones(length(x),1)*[1:n]) )*a+1;

a = [ ...
  -9.0399e-01
   3.3678e-01
  -5.8496e-02
   3.8711e-03];
n = length(a);
f4 = ( (x*ones(1,n)).^(ones(length(x),1)*[1:n]) )*a+1;

figure(3)
plot(x,exp(-x),x,f2,'--',x,f3,':',x,f4,'-.')
%legend(' Exact',' n = 2',' n = 3',' n = 4')
title('Figure 3. Galerkin approximations to first-order PDE')
xlabel('x')
ylabel('f(x)')
axis([0,5,-.2,1])
print -deps fig3

figure(4)
l  = 5/2;
K1 = [l/3-1/2,l/6+1/2;
      l/6-1/2,l/3+1/2];
K  = zeros(3);
K(1:2,1:2) = K1;
K(2:3,2:3) = K(2:3,2:3)+K1;
n  = 2;
f2 = -K(2:n+1,2:n+1)\K(2:n+1,1);

l  = 5/3;
K1 = [l/3-1/2,l/6+1/2;
      l/6-1/2,l/3+1/2];
K  = zeros(4);
K(1:2,1:2) = K1;
K(2:3,2:3) = K(2:3,2:3)+K1;
K(3:4,3:4) = K(3:4,3:4)+K1;
n  = 3;
f3 = -K(2:n+1,2:n+1)\K(2:n+1,1);

l  = 5/4;
K1 = [l/3-1/2,l/6+1/2;
      l/6-1/2,l/3+1/2];
K  = zeros(5);
K(1:2,1:2) = K1;
K(2:3,2:3) = K(2:3,2:3)+K1;
K(3:4,3:4) = K(3:4,3:4)+K1;
K(4:5,4:5) = K(4:5,4:5)+K1;
n  = 4;
f4 = -K(2:n+1,2:n+1)\K(2:n+1,1);

plot(x,exp(-x),linspace(0,5,3),[1;f2],'--', ...
               linspace(0,5,4),[1;f3],':', ...
               linspace(0,5,5),[1;f4],'-.')
%legend(' Exact',' n = 2',' n = 3',' n = 4')
title('Figure 4. Finite element approximations to first-order PDE (equal-sized elements)')
xlabel('x')
ylabel('f(x)')
axis([0,5,-.2,1])
print -deps fig4

figure(5)
l1 = 5/3;
l2 = 10/3;
K1 = [l1/3-1/2,l1/6+1/2;
      l1/6-1/2,l1/3+1/2];
K2 = [l2/3-1/2,l2/6+1/2;
      l2/6-1/2,l2/3+1/2];
K  = zeros(3);
K(1:2,1:2) = K1;
K(2:3,2:3) = K(2:3,2:3)+K2;
n  = 2;
f2 = -K(2:n+1,2:n+1)\K(2:n+1,1);

l1 = 1*5/7;
l2 = 2*5/7;
l3 = 4*5/7;
K1 = [l1/3-1/2,l1/6+1/2;
      l1/6-1/2,l1/3+1/2];
K2 = [l2/3-1/2,l2/6+1/2;
      l2/6-1/2,l2/3+1/2];
K3 = [l3/3-1/2,l3/6+1/2;
      l3/6-1/2,l3/3+1/2];
K  = zeros(4);
K(1:2,1:2) = K1;
K(2:3,2:3) = K(2:3,2:3)+K2;
K(3:4,3:4) = K(3:4,3:4)+K3;
n  = 3;
f3 = -K(2:n+1,2:n+1)\K(2:n+1,1);

l1 = 1/3;
l2 = 2/3;
l3 = 4/3;
l4 = 8/3;
K1 = [l1/3-1/2,l1/6+1/2;
      l1/6-1/2,l1/3+1/2];
K2 = [l2/3-1/2,l2/6+1/2;
      l2/6-1/2,l2/3+1/2];
K3 = [l3/3-1/2,l3/6+1/2;
      l3/6-1/2,l3/3+1/2];
K4 = [l4/3-1/2,l4/6+1/2;
      l4/6-1/2,l4/3+1/2];
K  = zeros(5);
K(1:2,1:2) = K1;
K(2:3,2:3) = K(2:3,2:3)+K2;
K(3:4,3:4) = K(3:4,3:4)+K3;
K(4:5,4:5) = K(4:5,4:5)+K4;
n  = 4;
f4 = -K(2:n+1,2:n+1)\K(2:n+1,1);

plot(x,exp(-x),[0,5/3,5],[1;f2],'--', ...
               [0,5/7,15/7,5],[1;f3],':', ...
               [0,1/3,1,7/3,5],[1;f4],'-.')
%legend(' Exact',' n = 2',' n = 3',' n = 4')
title('Figure 5. Finite element approximations to first-order PDE (unequal elements)')
xlabel('x')
ylabel('f(x)')
axis([0,5,-.2,1])
print -deps fig5

