Gauss Elimination

Gauss Elimination : means elimination of variable.
Let there are three equation and we have to find values of x, y, z
x+y+z=0
x-2y+2z=4
x+2y-z=2

How we can find values of x,y,z by using pen and paper?
x+y+z=0 -(I)
x-2y+2z=4 -(II)
x+2y-z=2 -(III)

substract II-I and III-I
we get : -

 x+y+z=0 -(IV)
-3y+2z=4 -(V)
    2y-z=2 -(VI)

Now we have to elimnate y
multiply 3 with III and 2 with II and subsutract III-II
we get : -
x+y+z=0 -(VII)
 -3y+z=4 -(VIII)
     -5z=10   -(IX)

from IX 
we get : -
z = -2

Put z=-2 in VII and VIII
we get : -
x+y-2=0 -(X)
-3y-2=4

from this we get : -
y=-2

and put y in X
We get : -
x=4



Now how can done this process using MATLAB: 
Here is code, paste it in Editor Window of MATLAB

function x = gauss(A,b)
[n,n]=size(A);
[n,k]=size(b);
x=zeros(n,k);
for i=1:n-1
    m=-A(i+1:n,i)/A(i,i);
    A(i+1:n,:)=A(i+1:n,:) + m*A(i,:);
    b(i+1:n,:)=b(i+1:n,:) + m*b(i,:);
end
x(n,:)=b(n,:)/A(n,n);
for i=n-1:-1:1
    x(i,:)=(b(i,:)-A(i,i+1:n)*x(i+1:n,:))/A(i,i);
end


Now Save this as gauss.m

Open Command Window of MATLAB
and write

>> A=[1 1 1;1 -2 2; 1 2 -1]

A =

     1     1     1
     1    -2     2
     1     2    -1

here A is matrix of coficent of [x1 y1 z1;x2 y2 z2;x3 y3 z3]
>> b=[0;4;2]

b =

     0
     4
     2
b is matrix of equalvence value of all equations 
>> gauss(A,b)

ans =

    4.0000
   -2.0000
   -2.0000

And to get values in integer

>> floor(ans)

ans =

     4
    -2
    -2

Here x=4, y= -2, z=-2.

Popular posts from this blog

Shutdown Pc

Ellipse using OpenGl

String Comparisons