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 个评论

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.
I added the error
Guillaume
Guillaume 2019-2-11
编辑:Guillaume 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.
Hi. I updated the code. Again this is not my code, teacher wanted us to run the code from our text book. But errors keep popping up.
ERROR 1: 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;
ERROR 2: Undefined function or variable 'x'.
Error in GaussSeidel (line 22)
x = x';
If I change anything different errors occur.
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.
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

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 个评论

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.
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

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!

Translated by