Wednesday, October 22, 2014

Matlab Program for Solving Non-Linear Equation using Bisection Method

a=input('Enter the function =  ', 's');
f= inline(a);
a=0; b=1;x=0;
for i=1:inf
    if f(a)*f(b)<0
        break
    else
        a=a-i; % replace - by + to get positive root
        b=b+i;
    end
end
for i=2:inf
   x(i)= (a+b)/2;
   if f(x(i))*f(a)<0
       b=x(i);
   else
       a=x(i);
   end
   if abs(x(i)-x(i-1))<=0.0001
       break;
   end
end
Iteration=i
Root=x(i)

Monday, October 20, 2014

Matlab Program for 3 Variables simultaneous equations using Gauss Seidel Method

a = input('Enter the Augmented Matrix >  ')
x(1)=0; y(1)=0; z(1)=0;
for i=2:100
    x(i)=( a(10) - a(4)*y(i-1) - a(7)*z(i-1) )/a(1);
    y(i)=( a(11) - a(2)*x(i-1) - a(8)*z(i-1) )/a(5);
    z(i)=( a(12) - a(3)*x(i-1) - a(6)*y(i-1) )/a(9);
    e1=abs(x(i)-x(i-1));
    e2=abs(y(i)-y(i-1));
    e3=abs(z(i)-z(i-1));
    if e1<=0.001 && e2<=0.001 && e3<=0.001
        Iteration=i
        x = x(i)
        y = y(i)
        z = z(i)
        break
    end
end

Matlab Program for 3 Variables simultaneous equations using Jacobi Iteration Method

a = input('Enter the Augmented Matrix >  ')
x(1)=0; y(1)=0; z(1)=0;
for i=2:100
    x(i)=( a(10) - a(4)*y(i-1) - a(7)*z(i-1) )/a(1);
    y(i)=( a(11) - a(2)*x(i-1) - a(8)*z(i-1) )/a(5);
    z(i)=( a(12) - a(3)*x(i-1) - a(6)*y(i-1) )/a(9);
    e1=abs(x(i)-x(i-1));
    e2=abs(y(i)-y(i-1));
    e3=abs(z(i)-z(i-1));
    if e1<=0.001 && e2<=0.001 && e3<=0.001
        Iteration=i
        x = x(i)
        y = y(i)
        z = z(i)
        break
    end
end

Friday, October 17, 2014

Gauss-Jordan Elimination Method

Gauss-Jordan Elimination Method
Gauss Jordan Elimination Method is used for solving system of linear equation and also used to find the inverse of an invertible square matrix. For finding the solution of the given set of linear equation a sequence of operations performed on the augmented matrix obtained from the set of linear equations until the coefficient part of the augmented matrix becomes identity matrix. To modify the augmented matrix we can swap the two rows or multiplying a row by a non-zero number or adding a multiple of one row to another row.

Consider the set of linear equations of 3 variable

The augmented matrix for the given set of equation is 


  Using the below operation to the augmented matrix
R1 = R1 /a1
R2 = R2a2*R1
R3 = R3a3*R1
The augmented matrix will become
Now apply these operations
R2 = R2 /D
R1 = R1B*R2
R3 = R3F*R2
The augmented matrix will become


Finally, apply these operations
R3 = R3/R
R1 = R1N*R3
R2 = R2P*R3
The augmented matrix will become

 
Thus, the values of  x, y and z can be obtained as
x = T
y = U
z = V

Note: If we apply all the above operations to an identity matrix it will produce the inverse of the coefficient matrix

Matlab Program for 3 Variables simultaneous equations using Gauss-Jordan Elimination Method

a=input('Enter the Augmented Matrix  >  ')
i=eye(3);
a(1,:)=a(1,:)/a(1);
i(1,:)=i(1,:)/a(1);
i(3,:)=i(3,:)-a(3)/a(1)*i(1,:);
a(3,:)=a(3,:)-a(3)/a(1)*a(1,:);
i(3,:)=i(2,:)-a(2)/a(1)*i(1,:);
a(2,:)=a(2,:)-a(2)/a(1)*a(1,:);
i(2,:)=i(2,:)/a(5);
a(2,:)=a(2,:)/a(5);
i(1,:)=i(1,:)-a(4)*i(2,:);
a(1,:)=a(1,:)-a(4)*a(2,:);
i(3,:)=i(3,:)-a(6)*i(2,:);
a(3,:)=a(3,:)-a(6)*a(2,:);
i(3,:)=i(3,:)/a(9);
a(3,:)=a(3,:)/a(9);
i(1,:)=i(1,:)-a(7)*i(3,:);
a(1,:)=a(1,:)-a(7)*a(3,:);
i(2,:)=i(2,:)-a(8)*i(3,:);
a(2,:)=a(2,:)-a(8)*a(3,:);
i  % it will produce inverse of the coefficient matrix
x=a(10)
y=a(11)
z=a(12)

Monday, October 13, 2014

Gaussian Elimination Method for solving simultaneous equations

Gaussian Elimination Method
Gaussian elimination method is used for solving systems of linear equations, it is also known as row reduction method. Gaussian elimination method can also be used to find the rank of a matrix, to calculate the determinant of a matrix, and to calculate the inverse of an invertible square matrix. A sequence of operations performed on the augmented matrix obtained from the set of linear equations until the lower left-hand corner of the matrix is filled with zeros. To modify the coefficient matrix we can swap the two rows or multiplying a row by a non-zero number or adding a multiple of one row to another row. Using these operations, a matrix can always be transformed into an upper triangular matrix.
Consider the set of linear equations of 3 variable


The augmented matrix for the given set of equation is 


Using the below operation to the augmented matrix 


R2 = R2 – (a2/a1)*R1
R3 = R3 – (a3/a1)*R1


The augmented matrix will become

Now apply this operation

R3 = R3 – (F/D)*R2

After applying the above rules the augmented matrix will transferred into upper triangular matrix



and the value of  x, y and z can be obtained as


z  =  Q/P
y  =  (L – E*z)/D
x  =  (K – C*z – B*y)/A


Matlab Program for 3 Variables simultaneous equations using Gaussian Elimination Method

g=input('Enter the Augmented Matrix >  ');
g(2,:)=g(2,:)- g(2)/g(1)*g(1,:);
g(3,:)=g(3,:)- g(3)/g(1)*g(1,:);
g(3,:)=g(3,:)- g(6)/g(5)*g(2,:);
z=g(12)/g(9)
y=(g(11)-z*g(8))/g(5)
x=(g(10)-z*g(7)-y*g(4))/g(1)