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!