% Antti Hannukainen 
% 
% Test the tht2.m - function and the obtained result !
% 
% Use sin(x*y) - as a test-function 
% 
% Data points are selected randomly from eps-NBHD of point x0
%

x0 = [0.5,0.5];
eps = 0.1;

X = eps*rand(100,2) - eps/2+repmat(x0,100,1);

f = @(x,y)( sin(x.*y) );

F = f( X(:,1), X(:,2));
G = tht2(X, F , 3);


figure;
hold on;
plot3(X(:,1),X(:,2),F,'r*');

[X,Y]=meshgrid(0.4:0.01:0.6,0.4:0.01:0.6);

Z = f(X,Y);

mesh(X,Y,Z);


% Evaluate polynomial!
ZZ = mpolyeval(G,[X(:),Y(:)],3);

for i=1:size(X,1)
  for j=1:size(X,2)
    ZG(i,j) = ZZ( i + size(X,1)*(j-1) );
  end
end


figure;
surf(X,Y,abs(Z-ZG));
title('Error');
