Why while end error does not work in my code??
조회 수: 2 (최근 30일)
이전 댓글 표시
clear clc
L=0.02;H=40;dx=0.01;dy=dx;dt=15
nx=uint32(L/dx+1);ny=uint32(H/dy+1);rx=dt/dx^2;ry=dt/dy^2;
k=28;alfa=12.5*10^-6;g=5*10^6;Ts=30;h=45
[X Y]=meshgrid(linspace(0,L,nx),linspace(0,H,ny));Tint=200;T=Tint*ones(ny,nx);
Tleft=0;Tright=Tint;Ttop=Tint;Tbottom=Tint;
T(:,1)=Tleft;T(:,end)=Tright;T(1,:)=Ttop;T(end,:)=Tbottom;Fo=alfa*dt*(dx^2);
time=0;n=time/dt;
err=1
tol=10^-1
while err>tol
n=n+1
Tn=T;
for i=2:nx-1
for j=2:ny-1
T(j,i)=Tn(j,i)+((Tn(j,i+1)+Tn(j,i-1)+Tn(j-1,i)+Tn(j+1,i)-4*Tn(j,i))+g*dx^2/k)*Fo
T(j,end)=Fo*((g*dx^2)/k - (2*h*(Tn(j,end) - Ts)*dx)/k - 2*Tn(j,end) + 2*Tn(j,end-1) + Tn(j,end)/Fo)
end
end
err = max(max(abs(Tn - T)))
end
İt is weird situation. My code is working but it does not stop. My error is smaller than 10^-2 but it does not stop.
댓글 수: 1
Rik
2020년 8월 26일
On my copy of Matlab it does stop. To massively increase the speed of the code I did put semicolons in the inner loop.
채택된 답변
Rik
2020년 8월 26일
Based on your now deleted comment ("Yeah, when i put semicolons it worked."):
The cause of the loop seeming to be stuck is that you printed the entire T array to your command window. This is fast, but it does take time. By putting semicolons in your code to suppress the output you avoid this, so the for loops finish in a reasonable time.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!