Laplace solved by Gauss-Seidel

The code delivers the same answer for any choice of NX or NY. There's a problem somewhere but I cannot find it..
NX=100; NY=100; Lx=5; Ly=5; x=linspace(0,Lx,NX); y=linspace(0,Ly,NY); u=zeros(NX,NY); u1=0; u2=900; u3=0; u4=0; err=1; k=0; tic; while max(err(:))>0 k=k+1;
uold=u;
for i=2:NX-1
for j=2:NY-1
u(i,j)=.25*(u(i+1,j)+u(i+1,j)+u(i,j+1)+u(i,j-1));
u(:,NX)=u1;
u(:,1)=u2;
u(1,:)=u3;
u(NY,:)=u4;
unew=u;
err=abs((uold-unew)./unew);
end
end
end
toc;
for i=1:NX
for j=1:NY
n=i+(j-1)*NX;
ui(i,j)=u(n);
end
end
surf(x,y,ui')
fprintf('number of iterations is :%f',k)
timetaken=toc

Answers (0)

Categories

Find more on Numerical Integration and Differential Equations in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!