How to change diagonal, subdiagonal and superdiagonal values with respect time while using loop and conditional statement?

10 Ansichten (letzte 30 Tage)
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
Dyuman Joshi
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
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.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Dyuman Joshi
Dyuman Joshi am 15 Mär. 2023
Bearbeitet: Dyuman Joshi am 15 Mär. 2023
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
dt=0.2+dt;
end
end
A
A = 20×20
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -847.8653 0 0 0 0 0 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 0 0 0 -854.7306 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 -856.4469 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 -858.1633 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 -859.8796 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 -861.5959 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 -863.3122 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 -865.0286 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Weitere Antworten (1)

Oguz Kaan Hancioglu
Oguz Kaan Hancioglu am 15 Mär. 2023
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
A = 20×29
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 -856.4469 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 -856.4469 -856.4469 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 -856.4469 -856.4469 -856.4469 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 -856.4469 -856.4469 -856.4469 -856.4469 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 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
B
B = 20×29
1.0e+03 * 1.7139 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.7139 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.7139 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.7139 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.7139 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.7139 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.7139 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.7139 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.7139 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.7139 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
C
C = 20×29
0 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 0 0 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 0 0 0 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 0 0 0 0 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 0 0 0 0 0 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 0 0 0 0 0 0 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 0 0 0 0 0 0 0 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 0 0 0 0 0 0 0 0 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 0 0 0 0 0 0 0 0 0 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 0 0 0 0 0 0 0 0 0 0 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469 -856.4469

Kategorien

Mehr zu Operating on Diagonal Matrices finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by