Index in position 2 exceeds array bounds (must not exceed 1001)
조회 수: 1 (최근 30일)
이전 댓글 표시
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.
댓글 수: 0
채택된 답변
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Matrix Indexing에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!