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

10 次查看(过去 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 0 0 0 0 0 0 0 0 -849.5816 0 0 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 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
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

类别

Help CenterFile Exchange 中查找有关 Operating on Diagonal Matrices 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by