I need it for my programming project and I dont know how to solve it. Thank you!
INDEX IN POSITION 1 EXCEEDS ARRAY BOUNDS (MUST NOT EXCEED 3)
5 views (last 30 days)
Show older comments
Laura Ashley Signo
on 22 Oct 2021
Commented: Star Strider
on 22 Oct 2021
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
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.
.
2 Comments
More Answers (0)
See Also
Categories
Find more on Matrix Indexing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!