Gauss-Jordan Elimination

89 ビュー (過去 30 日間)
Rachel McMurphy
Rachel McMurphy 2019 年 12 月 4 日
移動済み: John D'Errico 2024 年 10 月 19 日
So I have code for solving certain aspects of performing a Gauss-Jordan Elimination:
function s = search(M,i)
[x, y] = size(M); %x is rows, y is columns
submatrix = M([i:x],[1:y]); %output is rows below and equal to i
column_index = any(submatrix,1); %gives logical array
s = find(column_index,1); %gives column index
end
function m = move(M,i,j)
[x, y] = size(M); %x is rows and y is columns
submatrix = M([i:x],[1:y]); %creates submatrix of ith row down
column_j = submatrix(:,j); %gives jth column
row = find(column_j,1); %gives row number of first nonzero
i_row = M(i,:); %gives ith row of M
row_zero = M(row+1,:); %gives row that has nonzero in M
M(i,:) = row_zero;
M(row+1,:) = i_row
end
function n = normalize(M,i,j)
[x,y] = size(M); %x is rows and y is columns
element = M(i,j); %gives (i,j)th element
row_i = M(i,:); %gives ith row
div = row_i/element; %divides ith row by element
M(i,:) = div
end
function r = reduce(M,i,j,k)
[x,y] = size(M); %x is rows and y is columns
row_i = M(i,:); %gives ith row
row_k = M(k,:); %gives kth row
row_ik = row_i.*row_k; %multiplies rows i and k
new = row_k-row_ik; %makes jth position 0
M(k,:) = new %puts reduced row in kth position
end
Now I'm trying to use these four functions in order to create a step-by-step process of reducing a matrix (of any size) using for-loop from the 1st row to the last row of the matrix.
From what I understand, I have to use search(M,i) to find the first nonzero column, then if M(i,j) = 0 use move(M,i,j) to change the pivotal entry to a nonzero, if that pivotal entry is instead nonzero, use normalize(M,i,j) to make the initial element of that row 1, then use reduce(M,i,j,k) to make every other nonzero in that column 0. And repeat this process.
I have this code started:
function GJE = GJ(M)
[x, y] = size(M);
z = search(M,x); %finds first nonzero column
for i=z;j=1;k=1;
if M(i,j) == 0
move(M,i,j)
else
normalize(M,i,j)
end %of if
end %of for
end %of function
which seems to work for moving some rows around, but I'm lost after this, especially on how to make it for any sized matrix.
This is my example matrix:
M = [0 0 2 -4 1 7 -2;0 0 1 -2 0 4 0;1 3 2 -4 1 10 -3;2 6 1 -2 0 10 -2]
  3 件のコメント
Rachel McMurphy
Rachel McMurphy 2019 年 12 月 5 日
Yeah I know, but I have to build the code myself
vishwajeet
vishwajeet 2024 年 10 月 19 日
移動済み: John D'Errico 2024 年 10 月 19 日
A=input('Enter coefficient matrix');
b=input('Enter source vactor');
N=length(b);
X=zeros(N,1);
Aug=[A
A=input('Enter coefficient matrix');
Error using input
Support for user input is required, which is not available on this platform.
b=input('Enter source vactor');
N=length(b);
X=zeros(N,1);
Aug=[A b];
for j=1:N
Aug(j,:)=Aug(j,:)/Aug(j,i);
for i=1:N
if i~=j
m=Aug(i,j);
Aug(i,:)=Aug(i,:)-m*Aug(j,:);
end
end
end

サインインしてコメントする。

採用された回答

Ridwan Alam
Ridwan Alam 2019 年 12 月 5 日
編集済み: Ridwan Alam 2019 年 12 月 6 日
I didn't use the (sub-)functions, but tried to sketch out the algorithm following your logic in this file:
I have tested it and compared the results with the built-in function rref().
Hope this helps. Please let me know how it goes.
  3 件のコメント
Ridwan Alam
Ridwan Alam 2019 年 12 月 10 日
Agreed. :)
Jakub Matlacz
Jakub Matlacz 2021 年 4 月 3 日
Im afraid there is an error in this row:
rows = [1:j-1, j-1:a];
For j=1 (on the first loop) we have 0-index which is illegal in matlab and results in error
rows = [1:1-1, 1-1:3]
rows =
0 1 2 3

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeResizing and Reshaping Matrices についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by