Info

This question is closed. Reopen it to edit or answer.

Getting a Index exceeds matrix dimension Error. Can someone explain how to fix this?

1 view (last 30 days)
THE EEROR IS IN LINE 16, THE CODE IS AS FOLLOWS:
A=input('Enter Matrix A ');
b=input('Enter matrix b ');
tol=input('Enter tolerance ');
iter=0;
n=length(A);
x1=zeros(length(A),1)
x2=zeros(length(A),1)
err=.1
% for i=1:100
while err<tol
for i=1:n
for j=1:n
if i~=j
x2(i)=x2(i)+((-A(i,j)*x1(j)/A(i,i))+(b(i)/A(i,i)))
x2(i)=x2(i)+x1(i)
x2(i)=sqrt((x2(i)^(2)))
end
end
iter=iter+1
end
err=(norm(x2)-norm(x1))/norm(x1)
x1(i)=x2(i)
end
  1 Comment
John Chilleri
John Chilleri on 24 Apr 2017
Your code operates under the assumption that A is n x n (square). Also, I think you should change your input to request a vector b.
If matrix A is not square, then it may produce your error when you access A(i,j). Also, using
length(A)
is strange since A is a matrix.
Consider using
size(A,1) or size(A,2)
depending on which dimension size you desire.
Hope this helps!

Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!