% Demo for heat.m from
%   Regularization Tools, Per Christian Hansen
%   http://www2.imm.dtu.dk/~pch/Regutools/
% this file models the notoriously ill-posed inverse heat equation
% as a linear least squares problem.

% use these parameters for the homework (or higher ones)
n = 100; m=50; kappa=1;
[A,b,xtrue] = heat(n,kappa); A = full(A);
% cut A and x in order to get an n x m rectangular system
A = A(:,1:m); xtrue=xtrue(1:m);

% put some noise in the data
bnoisy = b + 1e-4*randn(n,1);



figure(1); clf;
title('true solution');
plot(xtrue);
 
figure(2); clf;
title('noiseless data');
plot(b);

figure(3); clf;
title('noisy data');
plot(bnoisy)

figure(4); clf;
title('comparison of solutions');
% in matlab x=A\b <=> x = pinv(A)*b
%  solve the problem with the noiseless data`
x1=A\b;
%  solve the problem with the noisy data
x2=A\bnoisy;
plot(1:m,x1,1:m,x2);

% compute the SVD of A
figure(5); clf;
[U,S,V] = svd(A);
plot(diag(S));
title('svd(A)');


