why my loop doesn't run or produce an error at all?

2 ビュー (過去 30 日間)
Xi Luan
Xi Luan 2022 年 9 月 16 日
コメント済み: Xi Luan 2022 年 9 月 19 日
for NT = 2:60 %number of time steps
for n = 533:475 %number of grid points
maindiag = 2*(1+r)*ones(n+1,1);%n+1 because including one ghost point below i = 0
lowdiag = -r*ones(n+1,1);
updiag = -r*ones(n+1,1);
S = spdiags([lowdiag maindiag updiag],-1:1,n+1,n+1);
S = full(S);
S(1,1) = 1;
S(1,2) = -0.007875;
S(1,3) = -1;
S(n+1,n-1) = -1;
S(n+1,n) = 0;
S(n+1,n+1) = 1;
b(1,1) = 0;%first row of RHS vector b
%interior rows of RHS vector b
for i = 1:n-1
b(i+1,1) = r*C(i,1)+(2-2*r)*C(i+1,1)+r*C(i+2,1);
end
b(n+1,1) = 0;%final row of RHS vector b
C(1:n+1)= linsolve(S,b);
plot((0:dz:totz)',C)
end
end
I'm trying to use crank nicholson to solve for fick's 2nd law diffusion equation. The above code is a section of it. The fourth line from bottom is how I try to save the calculated result/concentration in each time step. r is a constant. Initial C matrix has been calculated and is a column vector at the first time step (NT = 1).
After run, n = [] and NT = 60. but C matrix didn't update at all and remained as initial at NT = 1.

採用された回答

Abolfazl Chaman Motlagh
Abolfazl Chaman Motlagh 2022 年 9 月 16 日
編集済み: Abolfazl Chaman Motlagh 2022 年 9 月 16 日
second line in image (line 31 in your attached code).
for n=533:475
this is an empty loop. here watch this, i run it here:
n=533:475
n = 1×0 empty double row vector
so what is in this loop doesn't run even one time for whatever value the NT has. hence there is no update for C.
  1 件のコメント
Xi Luan
Xi Luan 2022 年 9 月 19 日
thank you! I should write n = 533:-1:475. You pointed out the direction to me!

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeDigital Filtering についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by