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)
3 次查看(过去 30 天)
显示 更早的评论
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;
采纳的回答
Star Strider
2021-10-22
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.
.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!