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

24 ビュー (過去 30 日間)
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 件のコメント
Dyuman Joshi
Dyuman Joshi 2023 年 3 月 14 日
"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 2023 年 3 月 15 日
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.

サインインしてコメントする。

採用された回答

Dyuman Joshi
Dyuman Joshi 2023 年 3 月 15 日
編集済み: Dyuman Joshi 2023 年 3 月 15 日
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 0 0 0 0 0 0 0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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

その他の回答 (1 件)

Oguz Kaan Hancioglu
Oguz Kaan Hancioglu 2023 年 3 月 15 日
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
  1 件のコメント
Yanni
Yanni 2023 年 3 月 15 日
In particular, you got the main diagonal value is same. but I want DIFFERENT main diagonal values.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeOperating on Diagonal Matrices についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by