How to change diagonal, subdiagonal and superdiagonal values with respect time while using loop and conditional statement?
Ältere Kommentare anzeigen
xmax=1; ymax=7; m=20; n=29;
dx=xmax/m; dy=ymax/n; dt=0.2;
UOLD=zeros(m,n);VOLD=zeros(m,n);
A=zeros([1,m]);
B=A;
C=A;
while dt<tmax
for j=1:n
for i=1:m
if j>i
C(i)=((dt*VOLD(i,j))/(4*dy))-(dt/(2*(dy^2))); %superdiagonal
elseif i>j
A(i)=((-dt*VOLD(i,j))/(4*dy))-(dt/(2*(dy^2))); %sub diagonal
elseif i==j
B(i)=1+((dt*UOLD(i,j))/(2*dx))+(dt/(dy^2)); %main diagonal
end
end
end
dt=0.2+dt;
end
4 Kommentare
Is this what you are looking for?
xmax=1; ymax=7; m=20; n=29; tmax=100; nt=500;
dx=xmax/m; dy=ymax/n; dt=tmax/nt;
UOLD=zeros(m,n);VOLD=zeros(m,n);
A=zeros([m,m]);
B=A;
C=A;
while dt<tmax
for j=1:n
for i=1:m
if j>i
C(j-1,j)=((dt*VOLD(i,j))/(4*dy))-(dt/(2*(dy^2))); %superdiagonal
elseif i>j
A(i,i-1)=((-dt*VOLD(i,j))/(4*dy))-(dt/(2*(dy^2))); %sub diagonal
elseif i==j
B(i,j)=1+((dt*UOLD(i,j))/(2*dx))+(dt/(dy^2)); %main diagonal
end
end
end
dt=0.2+dt;
end
A
B
C
Yanni
am 14 Mär. 2023
Dyuman Joshi
am 14 Mär. 2023
"You changing that matrix size only."
How else are you going to get the values on diagonals?
What are the expected outputs?
Dyuman Joshi
am 15 Mär. 2023
I didn't understand what you meant by your comment below my response.
Also, you did not answer my questions - How else are you going to get the values on "diagonals"?
What are the expected outputs? Please provide exactly what the values of A, B and C you want to get.
Akzeptierte Antwort
Weitere Antworten (1)
As @Dyuman Joshi mentioned, the problem statement is not clear. However vectors don't have diagonal. Please change the A,B, and C as a matrix and use correct indeces. I think it solves your problem.
xmax=1; ymax=7; m=20; n=29; tmax=100; nt=500;
dx=xmax/m; dy=ymax/n; dt=tmax/nt;
UOLD=zeros(m,n);VOLD=zeros(m,n);
A=zeros([m,n]);
B=A;
C=A;
while dt<tmax
for j=1:n
for i=1:m
if j>i
C(i,j)=((dt*VOLD(i,j))/(4*dy))-(dt/(2*(dy^2))); %superdiagonal
elseif i>j
A(i,j)=((-dt*VOLD(i,j))/(4*dy))-(dt/(2*(dy^2))); %sub diagonal
elseif i==j
B(i,j)=1+((dt*UOLD(i,j))/(2*dx))+(dt/(dy^2)); %main diagonal
end
end
end
dt=0.2+dt;
end
A
B
C
1 Kommentar
Yanni
am 15 Mär. 2023
Kategorien
Mehr zu Operating on Diagonal Matrices finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!