INDEX IN POSITION 1 EXCEEDS ARRAY BOUNDS (MUST NOT EXCEED 3)

5 views (last 30 days)
Index in position 1 exceeds array bounds. Index must not exceed 3.
Error in untitled3 (line 17)
Sum = Sum + a(i,j)*x(j);
------
a = [1.44,-0.36,5.52,0.00;-0.36,10.33,-7.78,0.00;5.52,-7.78,28.40,9.00];
b = [0.04;-2.15;0;0.88];
n = length(b);
x = zeros(n,1);
xnew = zeros(n,1);
x(:) = 0;
iterlimit = 100;
tol = 1e-6;
for iteration = 1 : iterlimit
convergence = true;
for i = 1 : n
Sum = 0;
for j = 1 : n
if j ~= i
Sum = Sum + a(i,j)*x(j);
end
end
xnew(i) = -1/a(i,i) * (Sum -b(i));
if abs(xnew(i) - x(i)) > tol
convergence = false;
end
end
if convergence
break
end
x = xnew;
end
disp('iterations')
iter
disp('solution')
xnew;

Accepted Answer

Star Strider
Star Strider on 22 Oct 2021
These —
for i = 1 : n
Sum = 0;
for j = 1 : n
should instead be —
for i = 1 : size(a,1)
Sum = 0;
for j = 1 : size(a,2)
Try that.
.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!