How to use 'for' loop for time loop?

39 vues (au cours des 30 derniers jours)
Nathi
Nathi le 8 Nov 2023
Commenté : Nathi le 8 Nov 2023
Here, 'for'loop in the index 'j' not satisfied the first three D equatiions. if I plot D, then I got first values only and all other values zero. but I want smooth curve. how to use For loop in the j th iteration to satisfy all six multiple eqaution?
clc
clear all
%======================SPACEGRID====================================%
ymax=14; m=56; dy=ymax/m; y=dy:dy:ymax; %'i'th row
xmax=1; n=20; dx=xmax/n; x=dx:dx:xmax; %'j'th column
x1=dx:xmax;
%=====================TIMEGRID======================================%
tmax=100; nt=500; dt=tmax/nt; t=dt:dt:tmax; % time at 'j'
UOLD=zeros(m,nt); VOLD=zeros(m,nt);
TNEW=0;TOLD=TNEW*ones(m,nt);TWALL=ones(1,length(t));
D=zeros([1,m]);
T=TOLD;
for j=1:nt
if j==1
for i=1:m
if i==1
D(i)=-dt*UOLD(i,j)*(-TNEW+TOLD(i,j)-TNEW)/(2*dx)-dt*VOLD(i,j)*(TOLD(i+1,j)-TWALL(j)+TOLD(i,j))/(4*dy)-(-dt)*VOLD(i,j)/(4*dy)+dt*(TWALL(j)-2*TOLD(i,j)+TOLD(i+1,j))/(2*dy^2)-dt*TWALL(j)/2*dy^2;
elseif i==m
D(i)=-dt*UOLD(i,j)*(-TNEW+TOLD(i,j)-TNEW)/(2*dx)-dt*VOLD(i,j)*(TNEW-TOLD(i-1,j)-TOLD(i,j))/(4*dy)-dt*VOLD(i,j)/(4*dy)+dt*(TOLD(i-1,j)-2*TOLD(i,j)+TNEW)/(2*dy^2)-dt*TNEW/2*dy^2;
else
D(i)=-dt*UOLD(i,j)*(-TNEW+TOLD(i,j)-TNEW)/(2*dx)-dt*VOLD(i,j)*(TOLD(i+1,j)-TOLD(i-1,j)+TOLD(i,j))/(4*dy)+dt*(TOLD(i-1,j)-2*TOLD(i,j)+TOLD(i+1,j))/(2*dy^2);
end
end
else
for i=1:m
if i==1
D(i)=-dt*UOLD(i,j)*(-T(i,j-1)+TOLD(i,j)-TOLD(i,j-1))/(2*dx)-dt*VOLD(i,j)*(TOLD(i+1,j)-TWALL(j)+TOLD(i,j))/(4*dy)-(-dt)*VOLD(i,j)/(4*dy)+dt*(TWALL(j)-2*TOLD(i,j)+TOLD(i+1,j))/(2*dy^2)-dt*TWALL(j)/2*dy^2;
elseif i==m
D(i)=-dt*UOLD(i,j)*(-T(i,j-1)+TOLD(i,j)-TOLD(i,j-1))/(2*dx)-dt*VOLD(i,j)*(TNEW-TOLD(i-1,j)+TOLD(i,j))/(4*dy)-dt*VOLD(i,j)/(4*dy)+dt*(TOLD(i-1,j)-2*TOLD(i,j)+TNEW)/(2*dy^2)-dt*TNEW/2*dy^2;
else
D(i)=-dt*UOLD(i,j)*(-T(i,j-1)+TOLD(i,j)-TOLD(i,j-1))/(2*dx)-dt*VOLD(i,j)*(TOLD(i+1,j)-TOLD(i-1,j)+TOLD(i,j))/(4*dy)+dt*(TOLD(i-1,j)-2*TOLD(i,j)+TOLD(i+1,j))/(2*dy^2);
end
end
end
end
plot(D)

Réponses (1)

Torsten
Torsten le 8 Nov 2023
Déplacé(e) : Torsten le 8 Nov 2023
Your code is equivalent with the code below because in D(i), you overwrite all the results you got for j < nt.
All arrays UOLD, VOLD, TNEW, TOLD consist of zeros except TWALL. TWALL only influences D(1). So you get D(1) different from 0 and all other array elements of D equal to 0.
clc
clear all
%======================SPACEGRID====================================%
ymax=14; m=56; dy=ymax/m; y=dy:dy:ymax; %'i'th row
xmax=1; n=20; dx=xmax/n; x=dx:dx:xmax; %'j'th column
x1=dx:xmax;
%=====================TIMEGRID======================================%
tmax=100; nt=500; dt=tmax/nt; t=dt:dt:tmax; % time at 'j'
UOLD=zeros(m,nt); VOLD=zeros(m,nt);
TNEW=0;TOLD=TNEW*ones(m,nt);TWALL=ones(1,length(t));
D=zeros([1,m]);
T=TOLD;
j = nt;
for i=1:m
if i==1
D(i)=-dt*UOLD(i,j)*(-T(i,j-1)+TOLD(i,j)-TOLD(i,j-1))/(2*dx)-dt*VOLD(i,j)*(TOLD(i+1,j)-TWALL(j)+TOLD(i,j))/(4*dy)-(-dt)*VOLD(i,j)/(4*dy)+dt*(TWALL(j)-2*TOLD(i,j)+TOLD(i+1,j))/(2*dy^2)-dt*TWALL(j)/2*dy^2;
elseif i==m
D(i)=-dt*UOLD(i,j)*(-T(i,j-1)+TOLD(i,j)-TOLD(i,j-1))/(2*dx)-dt*VOLD(i,j)*(TNEW-TOLD(i-1,j)+TOLD(i,j))/(4*dy)-dt*VOLD(i,j)/(4*dy)+dt*(TOLD(i-1,j)-2*TOLD(i,j)+TNEW)/(2*dy^2)-dt*TNEW/2*dy^2;
else
D(i)=-dt*UOLD(i,j)*(-T(i,j-1)+TOLD(i,j)-TOLD(i,j-1))/(2*dx)-dt*VOLD(i,j)*(TOLD(i+1,j)-TOLD(i-1,j)+TOLD(i,j))/(4*dy)+dt*(TOLD(i-1,j)-2*TOLD(i,j)+TOLD(i+1,j))/(2*dy^2);
end
end
D
D = 1×56
1.5938 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  1 commentaire
Nathi
Nathi le 8 Nov 2023
@Torsten then what I will do to get values in D. if i change 'j' index using for loop, then it will give values instead of zero. but its not the smooth.

Connectez-vous pour commenter.

Catégories

En savoir plus sur Creating and Concatenating Matrices dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by