How to use 'for' loop for time loop?

31 visualizaciones (últimos 30 días)
Nathi
Nathi el 8 de Nov. de 2023
Comentada: Nathi el 8 de Nov. de 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)

Respuestas (1)

Torsten
Torsten el 8 de Nov. de 2023
Movida: Torsten el 8 de Nov. de 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 comentario
Nathi
Nathi el 8 de Nov. de 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.

Iniciar sesión para comentar.

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by