Index in position 2 exceeds array bounds (must not exceed 1001)

1 回表示 (過去 30 日間)
Jamie Al
Jamie Al 2021 年 5 月 1 日
編集済み: DGM 2021 年 5 月 1 日
I have the following code with the error:
Index in position 2 exceeds array bounds (must not exceed 1001)
my code
dx = 0.001; %initially from Sod paper. grid/cell size
L = 1; %domain length
x = 0:dx:L;
N = length(x); % number of grids
NEQ = 8; % how many equ
U = zeros(NEQ,N);
Error in function:
function [ U_L, U_R] = MUSCL(U,N,NEQ )
size_U = size(U);
U_L = zeros(size_U);
U_R = zeros(size_U);
delta = 1e-6; % epsilon
for i= 1:NEQ
for j = 2:N-1
denom_L = (U(i,j) - U(i,j-1));
denom_R = (U(i,j+2) - U(i,j+1)); % error here
denom_L = sign(denom_L+1e-64)*max(delta, abs(denom_L)); % sign
denom_R = sign(denom_R+1e-64)*max(delta, abs(denom_R));
r_L = (U(i,j) - U(i,j-1))/denom_L;
r_R = (U(i,j+2) - U(i,j+1))/denom_R; % divide by 0 check ?
theta_L = Limiter(r_L);
theta_R = Limiter(r_R);
end
end
for j = 2:N-1
U_L(:,j) = U(:,j)+0.5*theta_R.*(U(:,j)-U(:, j-1)); % error
U_R(:,j) = U(:,j+1)-0.5*theta_L.*(U(:,j+2)-U(:,j+1));%
end
How do I fix this? How can I write the correct "domain" to loop over such setup.

採用された回答

DGM
DGM 2021 年 5 月 1 日
U is 8x1001.
i spans [1 8]
j spans [2 1000]
but you index ahead of j by 2.
U(i,j+2)
You'd either have to stop at 999 (i.e. 2:N-2) or pad the array. You'll have to decide what's contextually appropriate.
  4 件のコメント
Jamie Al
Jamie Al 2021 年 5 月 1 日
Do you mind showing a small example?
DGM
DGM 2021 年 5 月 1 日
編集済み: DGM 2021 年 5 月 1 日
Make U one column wider:
U = zeros(NEQ,N+1);
Trim one column off the end:
U_L = U_L(:,1:end-1);
U_R = U_R(:,1:end-1);
You'll have to make sure that's appropriate for your use.

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeMatrix Indexing についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by