필터 지우기
필터 지우기

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 CenterFile Exchange에서 Matrices and Arrays에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by