How to stop time loop when steady state is reached?
Show older comments
I'm using unsteady case. so, it will reach a steady state at a certain time level. I fixed time at 'j'th column wise and it ran at maximum time level, but I want to stop the 'time loop' when steady state is reached. Even though I'm using a steady criteria i.e error >tolerance along dt>0 using 'while' loop, it didn't work and using dt>0, my code didn't stop.
clc; close all; clear all;
ymax=20; n=80; dy=ymax/n;
xmax=1; m=20; dx=xmax/m;
tmax=100; nt=500; dt=tmax/nt; t=0:dt:tmax;
%tol=1e-5; max_difference=1.0;
UOLD=zeros(n,nt); VOLD=zeros(n,nt);
TNEW=0; TOLD=ones(n,nt);
A=zeros([1,m]);
B=A;
C=A;
D=A;
T=TOLD;
tic
%while dt>0 && max_difference>tol
for j=1:m
for i=1:n
if j>i
C(i)=(dt*VOLD(i,j)/4*dy)-((dt)/(2*dy^2));
elseif i>j
A(i)=(-dt*VOLD(i,j)/4*dy)-((dt)/(2*dy^2));
elseif i==j
B(i)=1+(dt*UOLD(i,j)/2*dx)+((dt)/(dy^2));
end
end
end
for j=2:m
for i=1:n
D(i)=(-dt*UOLD(i,j)*((-TNEW+TOLD(i,j)-TNEW)/(2*dx)))+(((dt)/(2*dy^2))*(TOLD(i+1,j)-(2*TOLD(i,j))+TNEW))-(((dt*VOLD(i,j))/(4*dy))*(TNEW-TOLD(i+1,j)-TOLD(i,j)));
end
end
T(:,j)=TriDiag(A,B,C,D); %call tridiagonal
dt=0.2+dt;
Thanks for any advice.
3 Comments
Mathieu NOE
on 20 Apr 2023
where does the TriDiag function comes from ?
Mathieu NOE
on 20 Apr 2023
ok
I am not sure to understand all the details of your code , but something disturbs me
if we consider that the metric (error) that defines how your computation converge to the supposed steady state values
difference = (abs(T(:,j)-T(:,j-1))./max(1,abs(T(:,j-1))));
then if I plot it for the 80 iterations , we see the difference increasing and not decreasing .... so I wonder if you have 100% confidence in your code
ploted with y log scale to make it clear :

Image Analyst
on 20 Apr 2023
Which loop, the one over i or the one over j, is the "time loop"?
I don't see any line like "error >tolerance". Where exactly is that line?
Accepted Answer
More Answers (0)
Categories
Find more on Numerical Integration and Differentiation 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!


