Error - Index in position 1 exceeds array bounds (must not exceed 10)
조회 수: 1 (최근 30일)
이전 댓글 표시
I am trying to run this code and I keep getting this error:
"Index in position 1 exceeds array bounds (must not exceed 10).
Error in WaveEquation (line 21)
x1(row,col) = 2*(1-r)*x1(row, col)+r*(x1(row+1,col)+x1(row-1,col))-x1(row,col-1); "
I tried to debug it and I am getting nowhere. I think I know what is happening; I believe that the error occurs as it is TRYING to continue to row 10. Attached is the debugging for code/table.
clear all; format compact; tic
delx = 0.1;
r = 1;
u = 1;
delt = r.^2*delx/u; % Delta Time Step Size
Tsteps = round(1/delt); % Number of Steps
x1 = zeros(Tsteps, 1/(2*delx)+2);
% Initial Conditions
x = 0:delx:.5+delx;
x1(1,:) = sin(pi*x);
% Boundary Conditions
x1(2,2:end-1) = .5*(x1(1,1:end-2)+x1(1,3:end));
x1(2,end) = x1(2,end-2); % Reflection Line
for row = 3:size(x1,1)
for col = 2:size(x1,2)-1
x1(row,col) = 2*(1-r)*x1(row, col)+r*(x1(row+1,col)+x1(row-1,col))-x1(row,col-1);
end
x1(row,end) = x1(row,end-2); % Reflection Line
end
x2 = [x1,fliplr(x1(:,1:end-3))];
figure(1),imagesc(0:delx:1,(0:delt:Tsteps*delt),x2),colorbar
ylabel('\leftarrow time (sec)')
xlabel('x')
title('Hyperbolic PDE')
if (delx==.1)
dispmat = [x1(1:8,1:7)];
fprintf('\nCompare to table 3.5, Solution of wave equation\n')
disp(num2str(dispmat))
end
댓글 수: 0
채택된 답변
the cyclist
2021년 2월 10일
I think you want
for row = 3:size(x1,1)-1
end
instead of
for row = 3:size(x1,1)
end
so that you don't try to access an element beyond the end of the array, when you use row+1.
You seem to have done it correctly for the loop over columns.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Eigenvalue Problems에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!