MATLAB Answers

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

31 views (last 30 days)
WARRIOR24 on 10 Feb 2021
Answered: the cyclist on 10 Feb 2021
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);
x1(row,end) = x1(row,end-2); % Reflection Line
x2 = [x1,fliplr(x1(:,1:end-3))];
ylabel('\leftarrow time (sec)')
title('Hyperbolic PDE')
if (delx==.1)
dispmat = [x1(1:8,1:7)];
fprintf('\nCompare to table 3.5, Solution of wave equation\n')

Accepted Answer

the cyclist
the cyclist on 10 Feb 2021
I think you want
for row = 3:size(x1,1)-1
instead of
for row = 3:size(x1,1)
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)

Community Treasure Hunt

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

Start Hunting!