How to use 'for' loop for time loop?

조회 수: 39 (최근 30일)
Nathi
Nathi 2023년 11월 8일
댓글: Nathi 2023년 11월 8일
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)

답변 (1개)

Torsten
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
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
Nathi
Nathi 2023년 11월 8일
@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.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by