Attempted to access x(402); index out of bounds because numel(x)=401.

1 回表示 (過去 30 日間)
Hassan
Hassan 2015 年 1 月 10 日
コメント済み: Stephen23 2015 年 1 月 10 日
I want to apply forward downstream scheme on a linear advection equation; this code was used in forward upstream scheme and I could not modify it to fit my application.
The error is caused by the "k+1" in line 18 of the code
dx = 0.05; dt = 0.01; C = 1; A1 = 1-C*dt/dx; A2 = C*dt/dx;
x = [0:dx:20];
N = length(x);
for k = 2:N
if (x(k) >= 4) && (x(k) <= 6)
u(k) = cos(0.5*pi*(x(k)-5));
else
u(k) = 0;
end
end
u(1) = u(N);
for k = 1:N
uplot(1,k) = u(k);
end
for iout = 1:5
for n = 1:5
for k = 2:N
u1(k) = A1*u(k)+A2*u(k+1);
end
u1(1) = u1(N);
for k = 1:N
u(k) = u1(k);
end
end
for k = 1:N
uplot(iout+1,k) = u(k);
end
end

採用された回答

Stalin Samuel
Stalin Samuel 2015 年 1 月 10 日
instead of N use N-1
  2 件のコメント
Hassan
Hassan 2015 年 1 月 10 日
In which line?
Stephen23
Stephen23 2015 年 1 月 10 日
Line 17: for k = 2:N-1.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by