Wednesday, 11 July 2012

Gauss Elimination (Solution System of Linear Equation)


Gaussian Elimination
We list the basic steps of Gaussian Elimination, a method to solve a system of linear equations. Except
for certain special cases, Gaussian Elimination is still \state of the art."" After outlining the method, we
will give some examples. Gaussian elimination is summarized by the following three steps:


  • Write the system of equations in matrix form. Form the augmented matrix. You omit the symbols
  • for the variables, the equal signs, and just write the coe_cients and the unknowns in a matrix. You
  • should consider the matrix as shorthand for the original set of equations.
  • Perform elementary row operations to get zeros below the diagonal.
    • An elementary row operation is one of the following:
    • multiply each element of the row by a non-zero constant
    • switch two rows
  • add (or subtract) a non-zero constant times a row to another row
  • Inspect the resulting matrix and re-interpret it as a system of equations.
  • If you get 0 = a non-zero quantity then there is no solution.
  • If you get less equations than unknowns after discarding equations of the form 0=0 and if there is
  • a solution then there is an in_nite number of solutions
  • If you get as many equations as unknowns after discarding equations of the form 0=0 and if there is a solution then there is exactly one solution
  • For two equations and two unknowns, the equations represent two lines. There are three possibilities:
  • There is a unique solution (the lines intersect at a unique point)
  • There is no solution (the two lines are parallel and don't intersect)
  • There is an in_nite number of solutions (the two equations represented the same line)
  • For three equations and three unknowns, each equations represents a plane and there are again three
  • possibilities:
    • There is a unique solution
    • There is no solution
    • There is an in_nite number of solutions.
Matlab Code (Without Pivoting)



clc
clear all
A = [4 6 7;0 2 3;2 -3 6];
B = [-3;8;5];
[m,n]=size(A);
if m~=n,error('Invalid Matrix');end
Aug=[A B];
nb = n+1;
for k=1:n-1
    for i=k+1:n
        Fac = Aug(i,k)/Aug(k,k);
        Aug(i,k:nb) =  Aug(i,k:nb) - Fac * Aug(k,k:nb);
    end
end
x = zeros(n,1);
x(n)=Aug(n,nb)/Aug(n,n);
for i=n-1:-1:1
    x(i) = ( Aug(i,nb) - Aug(i,i+1:n) * x(i+1:n) ) / Aug(i,i);
end
x

Matlab Code (With Pivoting)

clc
clear all
A = [0 2 3;4 6 7;2 -3 6];
B = [8;-3;5];
[m,n]=size(A);
if m~=n,error('Invalid Matrix');end
Aug=[A B];
nb = n+1;
for k=1:n-1
    %partialpivioting
    [big,i]=max(abs (Aug(k:n,k) ));
    ipr = i+k-1;
    if ipr~=k
        Aug([k,ipr],:)=Aug([ipr,k],:);
    end
    for i=k+1:n
        Fac = Aug(i,k)/Aug(k,k);
        Aug(i,k:nb) =  Aug(i,k:nb) - Fac * Aug(k,k:nb);
    end
end
x = zeros(n,1);
x(n)=Aug(n,nb)/Aug(n,n);
for i=n-1:-1:1
    x(i) = ( Aug(i,nb) - Aug(i,i+1:n) * x(i+1:n) ) / Aug(i,i);
end
x


No comments:

Post a Comment