tic
format long

%discretization and initialization%%%%%%%%%%
N=101; %number of nodes in x- direction
M=101;%%number of nodes in y- direction
h=1/(N-1);
k=1/(M-1);
ur=1;
T=zeros(N,M);
Tnew=T;

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

nr_it=0;    
stop=0;
while (stop~=1)
    nr_it=nr_it+1;
for i=2:N-1
    for j=2:M-1
    Tnew(i,j)=0.25*(T(i+1,j)+T(i-1,j)+T(i,j+1)+T(i,j-1)+h*h);
    end
end;

    if norm(Tnew-T)/norm(Tnew)<1e-6
            stop=1;
    end     
   if mod(nr_it,250)==0
    fprintf('nr_it=%g err=%g\n', nr_it,norm(Tnew-T)/norm(Tnew));
 end
    T=(1-ur)*T+ur*Tnew;
 
end

x=0:h:(N-1)*h;y=0:k:(M-1)*k;

 contour(x,y,T',20)
 axis equal 
 axis([0,1,0,1])
T_max=max(max(T))
nr_it
