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!