function T = jacobi(s)
% JACOBI : Demonstrate Jacobi iteration on 2D temperature problem
% 
% With no arguments, jacobi pauses after each iteration
% T = jacobi(s) does s iterations

n = 100;   % size of interior of grid
if nargin == 0
    pausing = true;
    s = 1000;
else
    pausing = false;
end;

% Initialize 2d temp array with boundary conditions
T = [100*ones(n+2,1)  zeros(n+2,n+1)];
if pausing
    surfc(T), shg;
    pause;
end;

% Indices for interior points and neighbor points
this = 2:n+1;
prev = this-1;
next = this+1;

% Now the main loop
for step = 1:s
    
    T(this,this) = (T(this,prev) + T(prev,this) + T(this,next) + T(next,this))/4;
    if pausing
        step
        surfc(T),shg;
        pause;
    end;
end;

surfc(T),shg