% One iteration of Jacobi.
% Inefficient.

function f = jacobi(f,mask,n,m)
% Arguments:
%   f = 2D-array with solution
%   mask = overriding mask for boundary conditions.


fnew = zeros(size(f));

for i = 2:n
    for j = 2:m
        if (mask(i,j) > 0)
        f(i,j) = mask(i,j)*0.25*(f(i-1,j) + f(i+1,j) ...
                     + f(i,j-1) + f(i,j+1));
        else
            f(i,j) = f(i,j);
        end
    end
end

%% Symmetry on left boundary (i-1 becomes i+1)
i = 1;
for j = 2:m
    if (mask(i,j) > 0)
        f(i,j) = 0.25*( f(i+1,j) + f(i+1,j) ...
                      + f(i,j-1) + f(i,j+1));
    else
        f(i,j) = f(i,j);
    end
end

%% Symmetry on lower boundary (j-1 becomes j+1)
j = 1;
for i = 2:n
    if (mask(i,j) > 0)
     f(i,j) = mask(i,j)*0.25*( f(i-1,j) + f(i+1,j) ...
                   + f(i,j+1) + f(i,j+1));
    else
        f(i,j) = f(i,j);
    end
end



end