it keeps saying dimensions must agree..what am i doing wrong
1 view (last 30 days)
Show older comments
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 Comments
Answers (1)
Image Analyst
on 14 Mar 2015
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 Comments
Image Analyst
on 14 Mar 2015
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.
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!