% Exercise 3 of Midterm for Math 496 (text Exercise 3.2, p. 53)
clear
% get the data
load 'ifk.mat'
m = length(d);
n = m; % for full matrix
s = linspace(0.025,0.975, m)';
% generate the full Gramian matrix
G = zeros(m,n);
for ii = 1:m
  for jj = ii:n
    a = s(ii)+s(jj);
    G(ii,jj) = 3*exp(-a)*(exp(a)-1-a-a^2/2)/a^3;
    G(jj,ii) = G(ii,jj);
  end
end
% for plotting solutions
xnodes = (0:.01:1)';
[X,S] = meshgrid(xnodes,s);
Gvalues = (X.*exp(-S.*X))';
ndx = [5,10,20];
for kk = 1:3
  Gamma = G(:,1:ndx(kk));
  fprintf('For n = %d, condition number is %g.\n',ndx(kk),cond(Gamma));
  msoln = Gamma\d
  figure
  plot(xnodes,Gvalues(:,1:ndx(kk))*msoln);
  grid
  title(['Text Exercise 3.2, solution plot with n = ',int2str(ndx(kk))])
end