Может ли кто-нибудь объяснить мне, почему мой код умножения Гаусса Джордана работает только для матриц 3×3, а не для матриц 4×4

#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 избыточным (и делает ваш код подверженным ошибкам).