How do I fix index, I keep getting an error
1 visualización (últimos 30 días)
Mostrar comentarios más antiguos
David Velo
el 25 de Abr. de 2019
Comentada: Walter Roberson
el 25 de Abr. de 2019
function x = Gauss2(A,b);
% Solve linear system Ax = b
% using Gaussian elimination without pivoting
% A is an n by n matrix
% b is an n by k matrix (k copies of n-vectors)
% x is an n by k matrix (k copies of solution vectors)
A=[1 0 0 0 0 0 -1 0, 0 -1 0 -1 0 0 0 0, 0 1 -1 0 0 0 0 0, 1 0 0 1 -1 0 0 0, 0 0 0 0 1 1 0 0, 0 0 1 0 0 1 0 0, 0 0 0 0 0 0 -1 0, 0 0 0 0 0 0 1 -1]
b= [35 -50 30 40 20 60 -90 25]'
[n, n] = size(A); % Find size of matrix A
[n, k] = size(b); % Find size of matrix b
x = zeros(n,k); % Initialize x
for i = 1:length(n)
m = -A(i+1:n,i)/A(i,i);
A(i+1:n,:) = A(i+1:n,:) + m*A(i,:);
b(i+1:n,:) = b(i+1:n,:) + m*b(i,:);
end;
% Use back substitution to find unknowns
x(n,:) = b(n,:)/A(n,n);
for i = n-1:-1:1
x(i,:) = (b(i,:) - A(i,i+1:n)*x(i+1:n,:))/A(i,i);
end
0 comentarios
Respuesta aceptada
Walter Roberson
el 25 de Abr. de 2019
When you created A, you used commas. Change those commas to semi-colons. Your current A is a vector instead of a 2D array.
2 comentarios
Walter Roberson
el 25 de Abr. de 2019
Your line
A=[1 0 0 0 0 0 -1 0, 0 -1 0 -1 0 0 0 0, 0 1 -1 0 0 0 0 0, 1 0 0 1 -1 0 0 0, 0 0 0 0 1 1 0 0, 0 0 1 0 0 1 0 0, 0 0 0 0 0 0 -1 0, 0 0 0 0 0 0 1 -1]
creates a row vector. The line
A=[1 0 0 0 0 0 -1 0; 0 -1 0 -1 0 0 0 0; 0 1 -1 0 0 0 0 0; 1 0 0 1 -1 0 0 0; 0 0 0 0 1 1 0 0; 0 0 1 0 0 1 0 0; 0 0 0 0 0 0 -1 0; 0 0 0 0 0 0 1 -1]
would create an 8 x 8 array, as would
A= [1 0 0 0 0 0 -1 0
0 -1 0 -1 0 0 0 0
0 1 -1 0 0 0 0 0
1 0 0 1 -1 0 0 0
0 0 0 0 1 1 0 0
0 0 1 0 0 1 0 0
0 0 0 0 0 0 -1 0
0 0 0 0 0 0 1 -1]
Space between items on the same line is the same as comma between items on the same line: it is for creating columns in the same row. Semicolon between parts breaks up into rows. Newline without ... operator also breaks into rows.
Más respuestas (0)
Ver también
Categorías
Más información sobre Matrices and Arrays 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!