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

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

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
A = 20×20
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 -856.4469 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 -856.4469 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 -856.4469 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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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 -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 -856.4469 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
You changing that matrix size only. but i need distinct main daigonal values and other two as well. here, main diagonal values are same. so, how to get distinct values with respect to time?
"You changing that matrix size only."
How else are you going to get the values on diagonals?
What are the expected outputs?
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.

댓글을 달려면 로그인하십시오.

 채택된 답변

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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -851.2980 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -853.0143 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 -856.4469 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 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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 -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개)

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

In particular, you got the main diagonal value is same. but I want DIFFERENT main diagonal values.

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Operating on Diagonal Matrices에 대해 자세히 알아보기

질문:

2023년 3월 14일

편집:

2023년 4월 29일

Community Treasure Hunt

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

Start Hunting!

Translated by