Borrar filtros
Borrar filtros

Error for C(i,:)

2 visualizaciones (últimos 30 días)
Alexa Shumaker
Alexa Shumaker el 11 de Feb. de 2019
Comentada: Guillaume el 12 de Feb. de 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 comentarios
Adam
Adam el 12 de Feb. de 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 el 12 de Feb. de 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.

Iniciar sesión para comentar.

Respuestas (2)

Walter Roberson
Walter Roberson el 11 de Feb. de 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 comentarios
Alexa Shumaker
Alexa Shumaker el 11 de Feb. de 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 el 11 de Feb. de 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.

Iniciar sesión para comentar.


Guillaume
Guillaume el 11 de Feb. de 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.

Categorías

Más información sobre Loops and Conditional Statements en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by