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

9 views (last 30 days)
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 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 on 19 Nov 2019
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

Kiana Bahrami on 19 Nov 2019
So now it's outputitng a 5x1 vector for 'x' but I still need a matrix like I had for my Jacobi method that you also helped me with before this. Do you know how I could fix the code to give me a matrix instead of a vector for vec?
Erivelton Gualter on 19 Nov 2019
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 on 19 Nov 2019
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?