hi am working on a code for gaussian elimination

14 Ansichten (letzte 30 Tage)
Ifechukwu
Ifechukwu am 22 Mai 2014
Bearbeitet: Walter Roberson am 17 Jul. 2016
hi am working on a code for gaussian elimination but I can't get the code to run for non square matrix please what should I do Here is the code and thanks in advance
function [x,U] = gausselim(A,b)
% function to perform gauss eliminination
%FORWARD ELIMINATION
n=length(b);
m=zeros(n,1);
x=zeros(n,1);
for k =1:n-1;
%compute the kth column of M
m(k+1:n) = A(k+1:n,k)/A(k,k);
%compute An=Mn*An-1, bn=Mn*bn-1
for i=k+1:n;
A(i, k+1:n) = A(i,k+1:n)-m(i)*A(k,k+1:n);
end;
b(k+1:n)=b(k+1:n)-b(k)*m(k+1:n);
end;
U= triu(A);
%BACKWARD ELIMINATION
x(n)=b(n)/A(n,n);
for k =n-1:-1:1;
b(1:k)=b(1:k)-x(k+1)* U(1:k,k+1);
x(k)=b(k)/U(k,k);
end;
end
  1 Kommentar
John D'Errico
John D'Errico am 22 Mai 2014
I fixed it for you, but you need to learn how to flag a block of code as such. Otherwise, it is unreadable.

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

George Papazafeiropoulos
George Papazafeiropoulos am 22 Mai 2014
Bearbeitet: Walter Roberson am 17 Jul. 2016
The correct function is:
function [x,U]=gausselim(A,b)
% function to perform gauss eliminination
%FORWARD ELIMINATION
n=length(b);
m=zeros(n,1);
x=zeros(n,1);
for k =1:n-1;
%compute the kth column of M
m(k+1:n) = A(k+1:n,k)/A(k,k);
%compute
%An=Mn*An-1;
%bn=Mn*bn-1;
for i=k+1:n
A(i, k+1:n) = A(i,k+1:n)-m(i)*A(k,k+1:n);
end;
b(k+1:n)=b(k+1:n)-b(k)*m(k+1:n);
end
U= triu(A);
%BACKWARD ELIMINATION
x(n)=b(n)/A(n,n);
for k =n-1:-1:1;
b(1:k)=b(1:k)-x(k+1)* U(1:k,k+1);
x(k)=b(k)/U(k,k);
end
end
After this try to run the code:
A=rand(5,4);
b=rand(4,1);
[x,U] = gausselim(A,b)
Is this what you want? I hope this helps...
George

Weitere Antworten (1)

Cory Cress
Cory Cress am 17 Jul. 2016
Bearbeitet: Walter Roberson am 17 Jul. 2016
I think the command you are looking for is rref :
R = rref(A)
[R,jb] = rref(A)
[R,jb] = rref(A,tol)
It produces a matrix in reduced row echelon form. Perhaps your exercise was to write it yourself, but for others that want to use a built-in function use rref.

Kategorien

Mehr zu Linear Algebra finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by