Error - Index in position 1 exceeds array bounds (must not exceed 10)

3 views (last 30 days)
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

Accepted Answer

the cyclist
the cyclist on 10 Feb 2021
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.

More Answers (0)

Categories

Find more on Matrices and Arrays in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!