Help ,urgent.

1 visualización (últimos 30 días)
Anshuman S
Anshuman S el 14 de Jun. de 2017
Respondida: Walter Roberson el 14 de Jun. de 2017
Hi this error is being generated:
In an assignment A(:) = B, the number of elements in A and B must be the same.
Error in q1_gs_150115 (line 42)
x(k) = num/At(k,k);
My code :
function [GS] = q1_gs_150115(~)
m=input('variables');
r=input('relaxation_number');
for i=1:m
for j=1:m
A(i,j)=input('element_i,j');
end
end
A=reshape(A,m,m);
for i = 1:m
j = 1:m;
j(i) = [];
B = abs(A(i,j));
c(i) = abs(A(i,i)) - sum(B); % Is the diagonal value greater than the remaining row values combined?
if c(i) > 0
fprintf('The matrix is diagonally dominant at row %2i\n\n',i)
end
end
for i=1:m
s(i,1)=input('constants_i');
end
p=input('number_of_iterations');
for i=1:m
x(i,1)=input('initial_values_i');
end
err = zeros(m,1);
At = [A,s];
for iter = 1:p
for k = 1:m
xold = x(k);
num = (1-r)*At(k,k)*x(k+1:m)+ r*At(k,end) - r*At(k,1:k-1)*x(1:k-1) - r*At(k,k+1:m)*x(k+1:m);
x(k) = r*num/At(k,k);
err(k) = abs(x(k)-xold);
end
disp(['Iter ',num2str(iter), '; Error =', num2str(max(err))]);
end
disp('The result is:')
disp(x)
  2 comentarios
Stephen23
Stephen23 el 14 de Jun. de 2017
The line shown in the error message does not exist in your code.
Guillaume
Guillaume el 14 de Jun. de 2017
Doubly ironical, since you haven't posted the relevant information for us to even give you an answer.

Iniciar sesión para comentar.

Respuesta aceptada

Walter Roberson
Walter Roberson el 14 de Jun. de 2017
num = (1-r)*At(k,k)*x(k+1:m)+ r*At(k,end) - r*At(k,1:k-1)*x(1:k-1) - r*At(k,k+1:m)*x(k+1:m);
r is a scaar. At(k,k) is a scalar. At(k,end) is a scalar. x(k+1:m) is a column vector. So we have to conclude that (1-r)*At(k,k)*x(k+1:m) is a column vector.
At(k,1:k-1) is a row vector but it is being used with "*" against a column vector, so the result of the "*" is a scalar, so r*At(k,1:k-1)*x(1:k-1) is a scalar.
At(k,k+1:m) is a row vector, but it is being used with "*" against a column vector, so the result of the "*" is a scalar, so r*At(k,k+1:m)*x(k+1:m) is a scalar.
num is therefore column vector minus scalar minus scalar. That is going to give a column vector result.
Then in the next line,
x(k) = r*num/At(k,k);
r is a scalar, At(k,k) is a scalar, and we found from above that num is a column vector. r*num/At(k,k) must therefore be a column vector. But you are trying to store the column vector into a scalar location, x(k)

Más respuestas (0)

Categorías

Más información sobre Creating and Concatenating Matrices en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by