% P2
rand('seed',0);
randn('seed',0);
n = 100;
m = 10;
% Generate n random points in a m-by-m square...

x = sort(m*rand(n,1));
y = sort(m*rand(n,1));

% Generate some exact "heights"

a1 = .3*m; b1=.2*m; a2 = .6*m; b2=.8*m;
for i=1:n
    r1 = (x(i)-a1)^2 + (y(i)-b1)^2;
    r2 = (x(i)-a2)^2 + (y(i)-b2)^2;
    h(i) = 10*exp(-.2*r1) + 100*exp(-3*r2);
end

% Noisy measurements
 E = zeros(n,n);
 for i=1:n
     for j =1:n
         % Assume that we have 90% chance of getting a measurement
         if rand<.1
             E(i,j) = inf;
         else
             dij = sqrt((x(i)-x(j))^2 + (y(i) - y(j))^2);
             mu = .05*dij*randn;
             E(i,j) = h(j) - h(i) + mu;
         end
     end
 end
 
 clc
 hAppx = Leveling(E,x,y,h(1));
 format short
 hAppx= reshape(hAppx,25,4)