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

1 Ansicht (letzte 30 Tage)
Jamie Al
Jamie Al am 1 Mai 2021
Bearbeitet: DGM am 1 Mai 2021
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.

Akzeptierte Antwort

DGM
DGM am 1 Mai 2021
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 Kommentare
Jamie Al
Jamie Al am 1 Mai 2021
Do you mind showing a small example?
DGM
DGM am 1 Mai 2021
Bearbeitet: DGM am 1 Mai 2021
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Matrices and Arrays finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by