How to use 'for' loop for time loop?
31 次查看(过去 30 天)
显示 更早的评论
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)
0 个评论
回答(1 个)
Torsten
2023-11-8
移动:Torsten
2023-11-8
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
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Creating and Concatenating Matrices 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!