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 (최근 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에서 Mathematics에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by