No, your code is incorrect. You forgot to use '*' signs when doing matrix multiplication. Here is how I would do it:
function x=JacobiSolve(A,b,n)
D=diag(A);
D_inv=diag(1./D);
R=A-diag(D);
b=b(:);
x=zeros(size(b));
for i=1:n, x=D_inv*(b-R*x); end
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!