Error for C(i,:)

조회 수: 5 (최근 30일)
Alexa Shumaker
Alexa Shumaker 2019년 2월 11일
댓글: Guillaume 2019년 2월 12일
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
Adam
Adam 2019년 2월 12일
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 2019년 2월 12일
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.

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

답변 (2개)

Walter Roberson
Walter Roberson 2019년 2월 11일
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
Alexa Shumaker
Alexa Shumaker 2019년 2월 11일
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 2019년 2월 11일
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.

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


Guillaume
Guillaume 2019년 2월 11일
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.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by