it keeps saying dimensions must agree..what am i doing wrong
조회 수: 2 (최근 30일)
이전 댓글 표시
i solved this linear equation using jacobi method but it keeps saying dimensions must agree. please what am i doing wrong?
A=[4,-2,1;2,5,-2;1,-1,-3]; % matrix A
b=[3;2;0.1];
n=3;
% Set initial value of x to zero column vector
x0=zeros(1,3);
% Set Maximum iteration number k_max
k_max=1000;
% Set the convergence control parameter erp
erp=0.0001;
% Show the q matrix
q=diag(diag(A))
% loop for iterations
for k=1:k_max
for i=1:n
s=0.0;
for j=1:n
if j==i
continue
else
s=s+A(i,j)*x0(j);
end
end
x1(i)=(b(i)-s)/A(i,i);
end
if norm(x1-x0)<erp
break
else
x0=x1;
end
end
% show the final solution
x=x1
% show the total iteration number
n_iteration=k
댓글 수: 0
답변 (1개)
Image Analyst
2015년 3월 14일
I copied and pasted and ran your code and it ran with no errors. Please attach the actual code that generates the error along with ALL THE RED TEXT that is in the error message. Don't paraphrase or snip out a small part like you did because that won't help us solve your problem especially when your posted code runs fine.
댓글 수: 3
Image Analyst
2015년 3월 14일
Well, what is the size of x1 and x0? You can find out by using the size() function or hovering your cursor over them, or the best way is for you to look at this link
Again, attach the non-working code. Use copy and paste, because the code you gave above works fine. There is no such error.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!