Error for C(i,:)
이전 댓글 표시
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
2019년 2월 11일
Please give the actual error message. Just telling us there is an error is not very useful for us to help you fix it. Also using a 'Code' block to format your code would make it a lot more readable.
Alexa Shumaker
2019년 2월 11일
I've fixed the formatting for you. In the future, use the buttons on the toolbar.
None of the comments in the code explain anything about what the intent of the code is.
A couple of obvious things that don't look right:
- d and ea are both created as nxn matrices. Only the first column of each is ever used.
- It looks like you're passing a scalar as es (with value 5), yet we have if ea == es. That if statement compares a matrix to a scalar so the result of the comparison is going to be a matrix. I'm fairly certain you don't know what happens when you pass a logical matrix to if, so it's most likely a bug. In this particular case, the if will only break the loop when all values of ea are equal to es. As per the first point, only the first column of ea is ever used, so columns 2:n will always be 0 and never equal to 5. The loop will never end.
So we don't have to run your code, please give us the complete error message, everything in red.
edit: and another thing that doesn't look right. x is created as a nxn matrix of 0 that is then transposed. Tranposing a square matrix full of zeros is not going to do much.
Alexa Shumaker
2019년 2월 11일
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
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
2019년 2월 11일
1 개 추천
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
2019년 2월 11일
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
2019년 2월 11일
0 개 추천
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.
카테고리
도움말 센터 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!