Wednesday, November 12, 2014

Matlab Program for finding positive root of a Non-Linear Function using Regula-Falsi Method

k=input('Enter the function =  ', 's');
f= inline(k);
a=0; b=1;
for i=1:inf
    if f(a)*f(b)<0
        break
    else
        a=a+1;
        b=b+1;
    end
end
for i=2:inf
   x(i)= (a*f(b)-b*f(a))/(f(b)-f(a));
   if f(x(i))*f(a)<0
       b=x(i);
   else
       a=x(i);
   end
   if abs(x(i)-x(i-1))<=0
      Iteration=i-1
      Root=x(i)
      break
   end
end

Tuesday, November 4, 2014

Matlab Program for finding positive root of a Non-Linear Function using Newton Rapson Method

a=input('Enter the function =  ', 's');
f= inline(a);
h= diff(a);
d= inline(h);
x(1)=0;
for k=1:inf
    if f(x(1))>0
      for i=2:inf
      x(i)= x(i-1)-((f(x(i-1)))/(d(x(i-1))));
       if abs(x(i)-x(i-1))<=0.001 && x(i)>0  
       Root=x(i)
       break 
       end
       end
       break
     else
      x(1)=x(1)+1;
    end
end

Monday, November 3, 2014

Matlab Program for finding positive root of a Non-Linear Funtion using Secant Method

a=input('Enter the function =  ', 's');
f= inline(a);
x(1)=0; x(2)=1;iteration=0;
for k=1:inf
 if f(x(1))*f(x(2))<0
  for i=3:inf
  x(i)=(x(i-2)*(f(x(i-1)))-x(i-1)*(f(x(i-2))))/(f(x(i-1))-f(x(i-2)));
  iteration=iteration+1;
    if abs(x(i)-x(i-1))<=0.001 && x(i)>0
    root=x(i)
    iteration=iteration
    break
    end
  end
  break
 else
 x(1)=x(1)+1;
 x(2)=x(2)+1;
 end
end


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)

Wednesday, September 17, 2014

Cramer's Rule for solving simultaneous equations

Cramer's Rule
Cramer's rule  is used to find the solution of a system of linear equations with as many equations as unknowns, valid whenever the system has a unique solution. It expresses the solution in terms of the determinants of the (square) coefficient matrix and of matrices obtained from it by replacing one column by the vector of right hand sides of the equations.
Consider the set of linear equations of 3 variable


The above equation can be written in matrix format as  


Then the values of x, y and z can be found with Cramer’s Rule as follows:




Matlab Program for n Variables simultaneous equations using Cramer's Rule

d = input('Enter the determinant matrix = ');
[m,n]=size(d);
while m~=n || det(d)==0
    if m~=n
        disp('Matrix is not Square')
        d = input('Enter the determinant matrix = ');
        [m,n]=size(d);
    else
        disp('Solution do not exist')
        d = input('Enter the determinant matrix = ');
        [m,n]=size(d);
    end
end
v = input('Enter the column vector = ');
[s,t]=size(v);
while (s~=n) || (t~=1)
    fprintf('Matrix is not a column vector of order %f\n',n);
    v = input('Enter the column vector = ') ;
    [s,t]=size(v);
end
delta = det(d);
delta1 = [ v d(:,2:n) ];
x = det(delta1)/delta;
fprintf('\nx1 = %f\n',x);
for c=2:(n-1)
    delta2 = [ d(:,1:c-1) v  d(:,c+1:n)];
    y = det(delta2)/delta;
    fprintf('x%f = %f\n',c,y);
end
delta3 = [ d(:,1:n-1) v];
z = det(delta3)/delta;
fprintf('x%f = %f\n',n,z);