## Matlab code: solve Helmholtz equation with SOR method

Hi, I try to solve Helmholtz equation with finite difference method and SOR method. But if I choose optimal value (B) not equal 1, I can't find that solution. (the answer is Inf)

matlab code:

nx = 6;
ny = 6;
dx=1/(nx-1);
dy=1/(ny-1);
dh=dx;
dh=dy;
x=-1:dh:1;
y=-1:dh:1;
n=size(x,2);

B= 1.0658; %nilai parameter relaksasi
koef=sqrt(2)*pi/4;

%%
% Nlai Awal
u = zeros(n,n);
% Syarat batas
u(1:n,1)= -sqrt(2)/2*cos(pi*y/4); %batas kiri
u(1:n,n)= sqrt(2)/2*cos(pi*y/4);%batas kanan
u(1,1:n)= sqrt(2)/2*sin(pi*x/4);%batas atas
u(n,1:n)=sqrt(2)/2*sin(pi*x/4); %batas bawah

tic;
flag = 1;
k=0;
while flag == 1
flag = 0;
uold = u;
k=k+1;
for i = 2:n-1
for j = 2:n-1
u(i,j) = (1-B)*uold(i,j)+ (B/(4-dh^2*koef^2))*(uold(i+1,j)+ uold(i-1,j)+ uold(i,j+1)+ uold(i,j-1));
error = abs((u(i,j)- uold(i,j))/uold(i,j));
if error > 10^(-10)
flag = 1;
end

end
end
end

% Plot
toc;
%solusi eksak
disp(k)
disp(u)
figure
surf (x,y,u)
[X,Y] = meshgrid(-1:1/5:1);
Z = sin(X*pi/4) .* cos(Y*pi/4)
norm(u-Z,inf)
figure
surf(X,Y,Z)