%  Script for Homework 5
%  Optimal delta function approximations
nx=151;  x = [0 : nx-1]'/(nx-1);

nn = [3 5 7 9 11 21]';
for n =  1 : length(nn)
 N = nn(n);
 j = [ 1 : N]';
 [J K] = meshgrid(j, j);
 G = 12 ./(J+K+1) - 12 ./(J+K) + 3 ./(J+K-1);
 u = 1 ./j;

 v = G\u;
 a = v / (u'*v);

 [X J] = meshgrid(x, j);
 delta = a'* (X.^(J-1));
 w = a'*G*a;
 disp([N w 1/N N*w])

 plot(x, delta, [.5-w/2, .5+w/2], [.5,.5]/w)
 pause(1)
end

