Printing out all Row Operations for Reduced Row Echelon Form and Elementary Matrices

36 Ansichten (letzte 30 Tage)
The code below is designed to take in an Augmented matrix (Concatenating A and B) and produce the row operations needed to reduced row echelon form. The row operations start from R1(first row operation) to Rn (nth row operation). The code should be modified to print out all the elementary matrices along with the corresponding row operations. Elementary matrices are the identity matrices multiplied by the row operation. However, the code is not working properly. The code needs to be fixed so that it matches the input of the expected outputs below. The code should be able to output the row operations and elementary matrices regardless of the size of the matrix, for example, a 1 x 10, 5 x 5, or 7 x 3. At the end, the code tests if all row operations are valid by multiplying all the elementary matrices together and then multiplying by A, and it should equal to the reduced row echelon form of A. I get the rror for the {j-i} column
Example Error:
Array indices must be positive integers or logical values.
Error in augmented_row_operations>row_operations (line 67)
row_ops{j-1} = sprintf('R%d -> R%d - (%d) R%d', j, j, factor, i);
Error in augmented_row_operations (line 7)
[rref_mat, row_ops, elem_matrices] = row_operations(A_aug)
% Example input
A = [2 3 4; 5 5 2; 2 3 3];
B = [0; 0; 0];
A_aug = [A B];
[rref_mat, row_ops, elem_matrices] = row_operations(A_aug)
function [rref_mat, row_ops, elem_matrices] = row_operations(A)
% Input: A is the augmented matrix
% Output: rref_mat is the RREF of A
% row_ops is a cell array containing the row operations performed to obtain the RREF form
% elem_matrices is a cell array containing the corresponding elementary matrices for each row operation
% Initialization
[r, c] = size(A);
rref_mat = A;
row_ops = cell(r-1, 1);
elem_matrices = cell(r-1, 1);
% Perform Gaussian elimination to obtain upper triangular matrix
for i = 1:r-1
% Find the first non-zero entry in the ith column
pivot_row = i;
while (pivot_row <= r) && (rref_mat(pivot_row, i) == 0)
pivot_row = pivot_row + 1;
end
% If there is no non-zero entry in the ith column, move to the next column
if pivot_row > r
continue;
end
% Swap rows if necessary to bring pivot to the ith row
if pivot_row > i
rref_mat([i pivot_row], :) = rref_mat([pivot_row i], :);
row_ops{i} = sprintf('R%d <-> R%d', i, pivot_row);
elem_matrices{i} = eye(r);
elem_matrices{i}([i pivot_row], [i pivot_row]) = elem_matrices{i}([pivot_row i], [pivot_row i]);
end
% Scale the ith row to make the pivot equal to 1
pivot = rref_mat(i, i);
rref_mat(i, :) = rref_mat(i, :) / pivot;
row_ops{i} = sprintf('R%d -> (1/%d) R%d', i, pivot, i);
elem_matrices{i} = eye(r);
elem_matrices{i}(i, i) = 1/pivot;
% Eliminate the entries below the pivot in the ith column
for j = i+1:r
if rref_mat(j, i) ~= 0
factor = rref_mat(j, i);
rref_mat(j, :) = rref_mat(j, :) - factor*rref_mat(i, :);
row_ops{j-1} = sprintf('R%d -> R%d - (%d) R%d', j, j, factor, i);
elem_matrices{j-1} = eye(r);
elem_matrices{j-1}(j, i) = -factor;
end
end
end
% Perform back-substitution to obtain reduced row echelon form
for i = r:-1:2
for j = i-1:-1:1
if rref_mat(j, i) ~= 0
factor = rref_mat(j, i);
rref_mat(j, :) = rref_mat(j, :) - factor*rref_mat(i, :);
row_ops{j-1} = sprintf('R%d -> R%d - (%d) R%d', j, j, factor, i);
elem_matrices{j-1} = eye(r);
elem_matrices{j-1}(j, i) = -factor;
end
end
end
% Round the entries to 6 decimal places to avoid rounding errors
rref_mat = round(rref_mat, 6);
% Print the row operations and corresponding elementary matrices
for i = 1:length(row_ops)
fprintf('R%d: %s\n',i,row_ops(i));
end
end
% Compute RREF and row operations
Test Cases:
Input:
[3 4 5 1;1.5 2 2.5 1.5]
Output:
R1 -> (1/3)R1: [ 0.33 0 0 0; 0 1 0 0; 0 0 1 0]
R2 -> R2 - (1.5)R1: corresponding elementary matrix
Input:
[2 3 4; 5 5 2; 2 3 3]
output:
R1 -> (1/2) R1: corresponding elementary matrix
R2 -> R2 - (5/2) R1: corresponding elementary matrix
R3 -> R3 - (1) R1: corresponding elementary matrix
R2 <-> R3: corresponding elementary matrix
R2 -> (1/5) R2: corresponding elementary matrix
R1 -> R1 - (3/5) R2: corresponding elementary matrix
R3 -> R3 + (2/5) R2: corresponding elementary matrix
R1 -> R1 - (8/3) R3: corresponding elementary matrix
R2 -> R2 + (11/2) R3: corresponding elementary matrix
  2 Kommentare
Voss
Voss am 6 Mär. 2023
Please double-check that the code you've posted here is the code you are actually using. The code here has the row_operations function defined two times and is missing a "[" on the line where row_operations is called.
Also, please double-check that your test case inputs and outputs are accurate. The first two test cases have the same input but different outputs.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Sarvesh Kale
Sarvesh Kale am 7 Mär. 2023
Hi Teoman,
Your code written was slightly modified by me to avoid the indexing error, while coding in MATLAB, you should keep in mind that the array indexing starts from 1 and not 0, the fixed code hopefully does what you want, I have attached the modified code.
Thank you
example code use
row_operations([1 2 ;3 4])
R1: R1 -> R1 - (2) R2
ans = 2×2
1 6 0 -2
I hope this helps !

Weitere Antworten (0)

Community Treasure Hunt

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

Start Hunting!

Translated by