Showing posts with label Numerical Methods. Show all posts
Showing posts with label Numerical Methods. Show all posts

Thursday, 29 May 2014

Power Method to Find the Largest Eigenvalue


   Power method is used to find the numerically largest eigenvalue. The method sometimes converge very slowly. In this post I am providing the C and C++ programs to implement power method.

Figure Below Shows a Sample Output




Wednesday, 30 April 2014

Fitting a Parabola Using Method of Moments : C/C++ Program


   In this post I have shared the C and C++ programs to fit a Parabola using the method of moments.

Figure Below Shows a Sample Output




Monday, 31 March 2014

Straight Line Fitting By Using the Method of Least Squares : C/C++ Program


   In this post I have shared the C and C++ Programs to perform straight line fitting by using the method of least squares. 

Figure Below Shows a Sample Output




Straight Line Fitting Using the Method of Group Averages : C/C++ Program


   In this post I have shared the C and C++ programs to perform straight line fitting by using the method of Group Averages.

Figure Below Shows a Sample Output



Friday, 28 February 2014

Gauss Seidel Method: C/C++ Program


   With this post I have provided the C and C++ programs implementing the Gauss Seidel Method.

Figure Below Shows a Sample Output

Gauss Jacobi Method: C/C++ Program


   With this post I have provided the C and C++ programs implementing the Gauss Jacobi Method.

Figure Below Shows a Sample Output


Click here to download the C Program.
 


Thursday, 10 January 2013

Program to Find the Square Root of a Number

  Finding the square root of a number is a very trivial operation in Mathematics. But how to find the square root of a number using some programming language like C or C++? Almost every programming language has a built in function to perform this operation. For example C and C++ has sqrt( ) defined in the header file math.h. But how to find the square root without using any standard library function? There are many different solutions available to this problem. Here I have posted programs to find the square root based on two different techniques. Babylonian Method (Heron's Method) and High/Low Method.

Babylonian Method (Heron's Method)
  
  This method is credited to the Babylonians and sometimes to Hero of Alexandria, a first-century Greek Mathematician. The technique is described below.

            To find the root of a number X, start with an initial approximation R0.
            R0 ≈ √X. 
          Rn+1=( Rn + ( X/Rn ) )/2.
            Repeat the process until you get a sufficiently accurate answer. 
High/Low Method
  
  This method is similar to the bisection method. This method involves guessing a approximate value for the square root of a number based on known squares. The guessed value is checked if it is too high or too low and adjusting accordingly until a sufficiently accurate answer is obtained.

Click Here to Download the C Program to find the Square Root of a number using Heron's Method. 
Click Here to Download the C++ Program to find the Square Root of a number using Heron's Method.

 

Saturday, 22 December 2012

Inverse of a Matrix


  The Inverse of a matrix exists only if the determinant is non zero. For every non singular matrix there exists a unique Inverse. If A is a matrix, then the inverse of the matrix is denoted by A-1. The product of a matrix and its Inverse equals the identity matrix I. There for we have the relation AA-1 = A-1A = I. Here inverse is obtained by applying elementary row transformations on the given matrix and similar changes are also made on an identity matrix of the same order. When the given matrix is transformed into an Identity Matrix the Identity Matrix becomes the Inverse Matrix. 

Figure Given Below Shows a 3 X 3 Matrix.


 Figure Given Below Shows the Inverse of the Given Matrix.


Figure Given Below Shows the Product of the Matrix and its Inverse.


Click here to download the C Program to find the Inverse of a Matrix.
Click here to download the C++ Program to find the Inverse of a Matrix.

         

Thursday, 20 December 2012

Solving Linear Equations using Gauss Jordan Elimination


  Gauss Jordan elimination is performed on the coefficient matrix to make it a diagonal matrix. Corresponding changes are also made on the product matrix. The method is often used to solve system of linear equations. Elementary row operations are used to obtain the diagonal matrix in Gauss elimination method. The program may fail to produce a result if one of the diagonal elements is reduced to zero, even when there is a solution. In such a case please run the program again after changing the order of the equations entered into the coefficient matrix.

The Figure Given Below Shows the Output for Three Equations in Three Unknown Variables.


Click here to download the C Program to solve Linear Equations by applying Gauss Jordan Elimination.
Click here to download the C++ Program to solve Linear Equations by applying Gauss Jordan Elimination.

Gauss Jordan Elimination: C/C++ Program


  Gauss Jordan elimination is one of the many operations we can perform on matrices. Here I have provided a program which finds the diagonal matrix corresponding to the given matrix. It is often used to solve system of linear equations. Elementary row operations are used to obtain the diagonal matrix in Gauss Jordan elimination method.

A 3 X 3 Matrix



 The Figure Below Shows the Diagonal Matrix Obtained by Gauss Jordan Elimination.


Click here to download the C Program to perform Gauss Jordan Elimination.

Click here to download the C++ Program to perform Gauss Jordan Elimination.

Tuesday, 18 December 2012

Solving Linear Equations using Gauss Elimination


  Gauss elimination is performed on the coefficient matrix to make it an upper triangular matrix. Corresponding changes are also made on the product matrix.  Then back substitution is used to obtain the solutions. The method is often used to solve system of linear equations. Elementary row operations are used to obtain the upper triangular matrix in Gauss elimination method. The program may fail to produce a result if one of the diagonal elements is reduced to zero, even when there is a solution. In such a case please run the program again after changing the order of the equations entered into the coefficient matrix.

The Figure Below Shows the Output For Four Equations in Four Unknown Variables. 

Click here to download the C Program to solve Linear Equations by applying Gauss Elimination.
Click here to download the C++ Program to solve Linear Equations by applying Gauss Elimination.

Monday, 17 December 2012

Gauss Elimination: C/C++ Program


  Gauss elimination is one of the many operations we can perform on matrices. Here I have provided a program which finds the upper triangular matrix corresponding to the given matrix. It is often used to solve system of linear equations. Elementary row operations are used to obtain the upper triangular matrix in Gauss elimination method.

The C++ Program is given below.
#include<iostream>
#define MAX 10
using namespace std;
int lcm(int x,int y);
int main()
{
    int i,j,k,r,c,a[MAX][MAX],b[MAX][MAX],det=1,l,d1,d2;
    cout<<"\nEnter the number of Rows of the Matrix: ";
    cin>>r;
    cout<<"\nEnter the number of Columns of the Matrix: ";
    cin>>c;
    if(r==c)
    {
       cout<<"\nEnter the Elements of the Matrix:\n";
       for(i=0;i<r;i++)
       {
          for(j=0;j<c;j++)
          {
             cin>>a[i][j];
          }
       }
       for(i=0;i<r-1;i++)
       {
           for(j=i+1;j<r;j++)
           {
             l=lcm(a[i][i],a[j][i]);
             if(l!=0&&(a[i][i]!=0&&a[j][i]!=0))
             {
               l=(a[i][i]*a[j][i])/l;
               d1=l/a[i][i];
               d2=l/a[j][i];
               a[j][i]=0;
               for(k=i+1;k<r;k++)
               {
                 a[j][k]=(d2*a[j][k])-(d1*a[i][k]);
               }
             }
           }
        }
        cout<<"\nThe given Matrix after Gauss Elimination is:\n";
        for(i=0;i<r;i++)
        {
          for(j=0;j<c;j++)
          {
             cout<<a[i][j]<<"\t";
          }
          cout<<"\n";
        }
     }
     else
     {
        cout<<"\nThis is not a Square Matrix!!!\n";
     }
     return 0;
}
int lcm(int x,int y)
{
    int t;
    while (y != 0)
    {
      t=y;
      y=x%y;
      x=t;
    }
    return x;
}

Figure Given Below Shows a 3 X 3 Matrix.

After Performing Gauss Elimination the Matrix is Shown Below.

Click here to download the C Program to perform Gauss Elimination.

Click here to download the C++ Program to perform Gauss Elimination.