MATLAB: Unable to perform assignment because the indices on the left side are not compatible with the size of the right side – Jacobi method inverse calculation

errorinversejacobi

Does anyone know why I'm getting this error: " Unable to perform assignment because the indices on the left side are not compatible with the size of the right side." I'm trying to perform a jacobi method inverse of my A matrix using identity matrix b. Thanks in advance!
A = [8 0 1 1 4; 2 9 1 4 0; 1 5 9 2 1; 1 1 4 12 1;2 3 1 4 16];
n = length(A);
b = eye(n);
%lambda = 1;
xinit = zeros(n,1);
norm = 2;
tol = 1.E-6;
maxiter = 100;
fprintf('\n-----Problem 1a------\n')
fprintf('The correct answer will be:')
[x,err,iter] = jacobi(A,b,xinit,tol,maxiter,norm);
x
err
iter
%% Function
% Jacobi
function [x,errst,iter] = jacobi(A,b,x,tol,maxiter,norm)
d = diag(A);
A = A - diag(d);
d = 1./d;
A = (A'*diag(d))';
b = b.*d;
err = 1.;
xold = x;
iter = 0;
while err>tol && iter<maxiter
iter = iter + 1;
x = b-A*x;
errst(iter,:) = errnorm(x-xold)./errnorm(x);
err = errst(iter,norm);
xold = x;
end
if iter>=maxiter
fprintf('\nLinear solver maxiter exceeded!!!\n')
end
end
function [abserr] =errnorm(vec)
vec = abs(vec);
abserr(1,1)= sum(vec);
abserr(1,2) = sqrt(vec'*vec);
abserr(1,3) = max(vec);
end

Best Answer

Function errnorm(vec) has inconsistent size. Try the following modifications:
function [abserr] =errnorm(vec)
vec = abs(vec);
abserr(1,1)= trace(vec);
abserr(1,2) = trace(sqrt(vec'*vec));
abserr(1,3) = max(max(vec));
end
Related Question