
%     adln1.m
%              March 3, 2005

echo on
clear
N = 20; NN = (0:N)/N ;  M = (N+1)^2  ;

x1 = 4*NN - 2 ;
[X1 X2] = meshgrid(x1, x1);
y = X1 .* exp(-X1.^2 - X2.^2);
figure(1), clf reset
%  myfigA(1,10,9.5), clf reset
surf(x1, x1, y), axis('ij'), hold on 

x1 = 0.6*NN+0.4;
x2 = 0.8*NN+0.6;
[X1 X2] = meshgrid(x1, x2);

d = X1 .* exp(-X1.^2 - X2.^2);
D = d(:)';
D(:, [1:3 438:441])

X = [ X1(:)'; X2(:)' ; ones(1, M)]; 
size(X)
X(:, [1:3 (M-3):M])

q = (D*X')/M  
R = (X*X')/M 
eig(R)'
w =  q/R

Y = w*X; YY = d ; YY(:) = Y;
Y(:, [1:3 (M-3):M])

err = sum(abs(D-Y))

surf(x1, x2, YY, ones(size(YY))), 
xlabel('x_1'), ylabel('x_2'), 
title('A non-linear function')
hold off
% print -f1 -deps cAdln1

 figure(2)
%  myfigA(2,10,9.5)
surf(x1, x2, d), axis('ij'), hold on 
surf(x1, x2, YY), 
xlabel('x_1'), ylabel('x_2'), 
title('Linear approximation')
hold off
% print -f2 -deps cAdln2


