Laplace solved by Gauss-Seidel

2 views (last 30 days)
Kornelija Paulauskaite
Kornelija Paulauskaite on 6 Dec 2017
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 Loops and Conditional Statements 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!