#matlab #gaussian
Вопрос:
% Ask user for the number of rows and columns of the matrix
m= input('Please input number of rows in matrix: ');
n= input ('Please input number of column in matrix: ');
% Set up dimensions of Matrix and get the elements of the matrix
for i= 1:m
for j=1:n
N(i,j)=input('Please input the elements of the matrix (left to right): ')
end
end
% Set up dimensions of vector
s= input('Please input number of elements in the vector: ');
% Set up dimensions and get the elements of the vector
for i= 1:s
for j=1
v(i,j)=input('Please input the elements of the matrix: ')
end
end
%Format the augmented matrix A for Gauss Jordan Elimination
A = [N v];
%Perform Gauss Jordan Elimination
for j=1:n
for i=1:m
if i==j
A(i,:)=A(i,:)/A(i,j) %Convert the elements on the diagonal when i = j aka
diagonal
else
A(i,:)=A(i,:)-A(j,:)*(A(i,j)/A(j,j)) % Goes down each row and converts each
element to zero
end
end
end
disp('Gauss Jordan Elimination Result')
A
Приведенный выше код работает для матриц 3×3, но когда я пытаюсь использовать его для матриц 4×4, он не работает, и я не уверен, почему. Примечание: дополненная матрица [N v] — это то, что на самом деле подвергается устранению гаусса Джордана
Комментарии:
1. Почему вы собираете
s
данные в качестве входных данных, когдаm
они уже доступны? Для целей исключения Гаусса-Джордана не следуетs
=m
? Таким образом, сборm
делает сборs
избыточным (и делает ваш код подверженным ошибкам).