Error for C(i,:)

16 views (last 30 days)
Alexa Shumaker
Alexa Shumaker on 11 Feb 2019
Commented: Guillaume on 12 Feb 2019
I keep getting error in bolded line.
Using Gauss Siedel, code is from my textbook and its not working when I call it in the command window.
Error:
Unable to perform assignment because the left and right sides have a different number of
elements.
Error in GaussSeidel (line 38)
x(i) = d(i)-C(i,:)*x;
Command Window:
>>A = [0.8,-0.4,1;-0.4,0.8,-0.4;0,-0.4,0.8];
>> b = [41,25,105]';
>> x = GaussSeidel(A,b,5)
CODE:
function x = GaussSeidel(A,b,es)
% GaussSeidel: Gauss Seidel method
% A = matrix
% b = right hand side vector
% es = stop criterion (default = 0.00001%)
% x = solution vector
[m,n] = size(A);
if m~=n
error('Matrix A must be square');
end % end of if statement
C = A;
% setting matrix A equal to C
for i = 1:n
C(i,i) = 0;
% x(i) = 0;
end % end of for loop
x = x';
for i = 1:n
C(i,1:n) = C(i,1:n)/A(i,i);
end % end of for loop
for i = 1:n
d(i) = b(i)/A(i,i);
end % end of for loop
iter = 0;
while (1)
xold = x;
for i = 1:n
x(i) = d(i)-C(i,:)*x;
if x(i) ~= 0
ea(i) = abs((x(i) - xold(i))/x(i)) * 100;
end % end of if statement
end % end %for loop
iter = iter+1;
if ea==es, break, end
end % end of while loop
  6 Comments
Adam
Adam on 12 Feb 2019
The second error is exactly what it says it is. You are trying to transpose a variable you haven't created yet. The second is easiest found using the debugger, as per Guillaume's answer, where you can look at
size( d(i)-C(i,:)*x )
You are trying to assign it to a scalar, although how you reach that error at all given that x is undefined is a mystery. I assume you had the piece of code in that is commented out higher up although assign 0 to x(i) in a loop is not at all efficient.
doc zeros
will create an array of zeros.
Guillaume
Guillaume on 12 Feb 2019
x used to be declared in the version originally posted (as zeros(n)), so were d and ea which now simply grow in size at each iteration.
@Alexa, please post new versions of the code as new comments rather than editing the original code, so that people can understand how it evolved.
As I commented in Walter's answer, if that code came out of a text boox, then return the text book and ask for your money back. The original code is too broken to be worthy of publication.

Sign in to comment.

Answers (2)

Walter Roberson
Walter Roberson on 11 Feb 2019
your x is a 2d matrix. The result of the * operation is going to be a vector. You try to store the vector into the single location x(i)
  2 Comments
Alexa Shumaker
Alexa Shumaker on 11 Feb 2019
I don't understand what you mean. Can you be a little more specific? By the way this code isn't mine, is from my text book I was just calling it to see if it worked.
Guillaume
Guillaume on 11 Feb 2019
By the way this code isn't mine, is from my text book I was just calling it to see if it worked
Really? Then throw away that text book, it's not going to teach you anything useful. The code is full of bugs.

Sign in to comment.


Guillaume
Guillaume on 11 Feb 2019
As per my comment to your question, clearly your code is full of bugs. To find out how your code actually behaves as opposed to what you expect it to do, use the debugger to follow along what it's doing. After each step, look at the state of the variables you've just modified and see if they're what you wanted them to be. If not, fix the code, repeat.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!