Unable to perform assignment because the left and right sides have a different number of elements - Gauss-Seidel Method to solve for inverse matrix

조회 수: 3 (최근 30일)
Does anyone know why I'm getting this error: "Unable to perform assignment because the left and right sides have a different number of elements." when I try to solve for the inverse of A using the Gauss-Seidel method and 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 1b------\n')
fprintf('The correct answer using the Gauss Seidel method will be the inverse matrix, x:')
[x,errst,iter] = seidel(A,b,x,lambda,tol,maxiter,norm);
x
iter
%% Functions
% Gauss Seidel
function [x,errst,iter] = seidel(A,b,x,lambda,tol,maxiter,norm)
n = length(A);
d = diag(A);
d = 1./d;
A = (A'*diag(d))';
b = b.*d;
A = A - eye(n);
err = 1.;
xold = x;
iter = 0;
while err>tol && iter<maxiter
iter = iter + 1;
for i = 1:n
x(i) = b(i)-A(i,:)*x;
end
x = lambda*x+(1-lambda)*xold;
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
% error
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

채택된 답변

Erivelton Gualter
Erivelton Gualter 2019년 11월 19일
For this case, it seems vec has 5x1 size. So you should you sum instead of trace.
function [abserr] =errnorm(vec)
vec = abs(vec);
abserr(1,1)= sum(vec);
abserr(1,2) = trace(sqrt(vec'*vec));
abserr(1,3) = max(max(vec));
end
  댓글 수: 5
Erivelton Gualter
Erivelton Gualter 2019년 11월 19일
I realized the problem.
You dont need the loop to update x:
for i = 1:n
x(i) = b(i)-A(i,:)*x;
end
SO here is the final code again:
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);
norm = 2;
tol = 1.E-6;
maxiter = 100;
fprintf('\n-----Problem 1b------\n')
fprintf('The correct answer using the Gauss Seidel method will be the inverse matrix, x:')
[x,errst,iter] = seidel(A,b,xinit,lambda,tol,maxiter,norm);
x
iter
%% Functions
% Gauss Seidel
function [x,errst,iter] = seidel(A,b,x,lambda,tol,maxiter,norm)
n = length(A);
d = diag(A);
d = 1./d;
A = (A'*diag(d))';
b = b.*d;
A = A - eye(n);
err = 1.;
xold = x;
iter = 0;
while err>tol && iter<maxiter
iter = iter + 1;
x = b-A*x;
x = lambda*x+(1-lambda)*xold;
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
% error
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
Kiana Bahrami
Kiana Bahrami 2019년 11월 19일
Wonderful! Thank you! It now prints out the correct 'x' matrix. However, it performs the same # of iterations as the jacobi method. From my understanding, it should be more efficient and perform with a lower number of iterations that the iterations, no?

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Calculus에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by