Laplace solved by Gauss-Seidel

조회 수: 2 (최근 30일)
Kornelija Paulauskaite
Kornelija Paulauskaite 2017년 12월 6일
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

답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by