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

2 次查看(过去 30 天)
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

采纳的回答

Erivelton Gualter
Erivelton Gualter 2019-11-19
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

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Characters and Strings 的更多信息

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by