MATLAB Answers

Error Index in position 1 exceeds array bounds (must not exceed 4).

2 views (last 30 days)
I am trying to run a "for loop" for a list of numbers. I am getting the correct answers except for my last number "1.5". Here is what my error is saying:
Index in position 1 exceeds array bounds (must not exceed 4).
Error in WaveEquation (line 30)
Here is my Code:
clear all; format compact; tic
delx = 0.1;
u = 1;
r= [0.2, 0.5, 0.8, 1, 1.25, 1.5];
% r=1;
j=1;
for i = r
delt = i.^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-i)*x1(row, col)+i*(x1(row-1,col-1)+x1(row-1,col+1))-x1(row-2,col);
end
x1(row,end) = x1(row,end-2); % Reflection Line
end
x2 = [x1,fliplr(x1(:,1:end-3))];
fprintf('\nr =%f', i);
if (delx==.1)
dispmat = x1(1:6,1:7);
fprintf('\nExplicit Method\n')
disp(num2str(dispmat))
end
j=j+1;
end
And here is my Command Window. I am getting what I need up until r = 1.5.

Accepted Answer

KALYAN ACHARJYA
KALYAN ACHARJYA on 11 Feb 2021
Edited: KALYAN ACHARJYA on 11 Feb 2021
Here, x1 if you check carefully
>> whos x1
Name Size Bytes Class Attributes
x1 4x7 224 double
x1 size is 4x7 (It has 4 rows, 7 columns), but you are trying to access rows from 1 to 6
dispmat = x1(1:6,1:7);
%.............^row,^columns range
Modify the code accordingly

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by