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
1 view (last 30 days)
Show older comments
Kiana Bahrami
on 19 Nov 2019
Commented: Kiana Bahrami
on 19 Nov 2019
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
0 Comments
Accepted Answer
Erivelton Gualter
on 19 Nov 2019
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
More Answers (0)
See Also
Categories
Find more on Characters and Strings in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!