Docstoc

C++ programs for numerical techniques in computer programming

Document Sample
C++   programs for numerical techniques in computer programming Powered By Docstoc
					                             Gauss elimination method

//***********************************************************************
//Program Name       : Gauss elimination.cpp
//Author        :
//Date written    : 17-02-2010
//Date Compiled     : 17-02-2010
//Aim of the program : To solve the equation by Gauss Elimination method
//***********************************************************************
#include<iostream>
using namespace std;
#include<conio.h>

int main()
{
 float a[20][20],ratio,x[20];
 int i,j,k,n;
 cout<<"\nEnter the order of the matrix";
 cin>>n;
 cout<<"\nEnter the coefficients and RHS";
 for(i=0;i<n;i++)
  {
   for(j=0;j<n+1;j++)
    {
     cin>>a[i][j];
     cout<<"\n";
    }
  }
 for(j=0;j<n;j++)
  {
   for(i=0;i<n;i++)
    {
     if(i!=j)
             {
                      ratio=a[i][j]/a[j][j];
     for(k=0;k<n+1;k++)
       a[i][k]=a[i][k]-ratio*a[j][k];
             }
    }
   }

for(i=n-1;i>=0;i--)
{
        float s=0;
        for(j=i+1;j<n;j++)
                 s+=a[i][j]*x[j];
           x[i]=(a[i][n]-s)/a[i][i];
}
       for(i=0;i<n;i++)
    {
     for(j=0;j<n+1;j++)
      {
       cout<<a[i][j];
       cout<<"\t";
      }
      cout<<endl;
    }

cout<<"\nThe solution is";
for(i=0;i<n;i++)
 cout<<"\n"<<"x("<<i+1<<") ="<<x[i]<<endl;
getch();
return 0;

}



/*          OUTPUT

Enter the order of the matrix 2 2

Enter the coefficients and RHS
235
346


The solution is
x(1) =-0.25

x(2) =1.75
*/
                                        Gaussjordan method



//**************************************************************************
// Program Name        :Gaussjordan.cpp
// Author         :
// Date Written      :17-02-2010
// Date Compiled      :17-02-2010
// Aim of the Program :Solution of linear eqs(Gauss-Jordan method)
//**************************************************************************
#include<iostream>
using namespace std;

 main()
{
 float a[20][20],ratio;
 int i,j,k,n;
 cout<<"\nEnter the order of the matrix";
 cin>>n;
 cout<<"\nEnter the coefficients and RHS";
 for(i=0;i<n;i++)
  {
   for(j=0;j<n+1;j++)
    {
     cin>>a[i][j];
     cout<<"\n";
    }
  }
 for(j=0;j<n;j++)
  {
   for(i=0;i<n;i++)
    {
     if(i!=j)
             {
                      ratio=a[i][j]/a[j][j];
     for(k=0;k<n+1;k++)
       a[i][k]=a[i][k]-a[j][k]*ratio;
             }
    }
   }

cout<<"\nThe solution is";
for(i=0;i<n;i++)
 cout<<"\n"<<"x("<<i+1<<") ="<<a[i][n]/a[i][i]<<endl;
}


    OUTPUT


Enter the order of the matrix 3 3

Enter the coefficients and RHS
2345
5675
1379

The solution is
x(1) =-5.5

x(2) =6

x(3) =-0.5
                                       Gaussiedal method



//***************************************************************
//Program Name :Gaussiedal.cpp
//Author     :
//Date written :27/03/10
//Date compiled :27/03/10
//Aim of Program:simulataneous eq soln using gauss siedal method
//***************************************************************

#include<iostream>
#include<iomanip.h>
#include<math.h>
#define n 2

int main()
{
  float a[n][n+1],x[n],aerr,maxerr,t,s,err;
  int i,j,itr,maxitr;

 //initializing array
 for(i=0;i<n;i++)
  x[i]=0;

 cout<<"enter elements of aug mat";
 for(i=0;i<n;i++)
  for(j=0;j<n+1;j++)
   cin>>a[i][j];

 cout<<"enter allowed error, max iterartons"<<endl;
 cin>>aerr>>maxitr;

 cout<<"iteration "<<"\t"<<"x[1]"<<"\t"<<"x[2]"<<"\t"<<"x[3]"<<endl;

 for(itr=1;itr<=maxitr;itr++)
  {
    maxerr=0;
    for(i=0;i<n;i++)
         {
           s=0;
           for(j=0;j<n;j++)
            if(j!=i)
              s+=a[i][j]*x[j];
           t=(a[i][n]-s)/a[i][i];
           err=fabs(x[i]-t);
           if(err>maxerr)
             maxerr=err;
           x[i]=t;
           }
      cout<<"\t"<<itr;
      for(i=0;i<n;i++)
           cout<<"\t"<<setprecision(4)<<x[i];
      cout<<endl;
      if(maxerr<aerr)
           {
             cout<<"converges in "<<setw(3)<<itr<<"iterartions"<<endl;
             for(i=0;i<n;i++)
              cout<<"x["<<setw(3)<<(i+1)<<"]"<<"\t"<<setprecision(4)<<x[i]<<endl;
             return 0;
           }
     }
    cout<<"soln does not converge,"<<"iterations not sufficient"<<endl;
    return 1;
}




        OUTPUT




  enter elements of aug mat 2 3 5
                 147
enter allowed error, max iterartons
0.0001 15
iteration     x[1] x[2] x[3]
     1     2.5 1.125
     2     0.8125 1.547
     3     0.1797 1.705
     4 -0.05762 1.764
     5 -0.1466 1.787
     6 -0.18 1.795
     7 -0.1925 1.798
     8 -0.1972 1.799
     9 -0.1989 1.8
     10 -0.1996 1.8
     11 -0.1999 1.8
    12 -0.1999 1.8
converges in 12iterartions
x[ 1] -0.1999
x[ 2] 1.8
                                   Simpson method



//***********************************************************************************
***
//Program Name          : Simpson.cpp
//Author           :
//Date written       : 27-03-2010
//Date Compiled        : 27-03-2010
// Aim of the program : To find integral of the equation x*x
//***********************************************************************************
***
#include<iostream>
using namespace std;
#include<math.h>
float y(float x)
{
         return (x*x);
}
 main()
{
         float x0,xn,h,s;
         int i,n;
         cout<<"\nEnter the values of x0,xn,no of sub-intervals"<<endl;
         cin>>x0>>xn>>n;
         h=(xn-x0)/n;
         s=y(x0)+y(xn)+4*y(x0+h);
         for(i=3;i<=n-1;i+=2)
                  s+=4*y(x0+i*h)+2*y(x0+(i-1)*h);
         cout<<"\nValue of integral is "<<(h/3)*s<<endl;
}




          OUTPUT


Enter the values of x0,xn,no of sub-intervals
173

Value of integral is 57.3333
                                   Trapezoidal method


//********************************************************************************
//Program Name       : Trapezoidal.cpp
//Author        :
//Date written    : 17-03-2010
//Date Compiled     : 17-03-2010
//Aim of the program : to find integral of the equation
//********************************************************************************

#include<iostream>
using namespace std;
#include<math.h>
float y(float x)
{
         return (x*x);
}
 main()
{
         float x0,xn,h,s;
         int i,n;
         cout<<"\nEnter the values of x0,xn,no of sub-intervals"<<endl;
         cin>>x0>>xn>>n;
         h=(xn-x0)/n;
         s=y(x0)+y(xn);
         for(i=1;i<=n-1;i++)
                  s+=2*y(x0+i*h);
         cout<<"\nValue of integral is "<<(h/2)*s<<endl;
}




         OUTPUT


Enter the values of x0,xn,no of sub-intervals
173

Value of integral is 118
                                      Rungakutta method

//**********************************************
//Program Name :rungakutta.cpp
//Author     :
//Date written :27-03-2010
//Date compiled :27-03-2010
//**********************************************

#include<iostream>
#include<math.h>
using namespace std;
float f( float x, float y)
{
  return ((1+x)*y*y)/2;
}
int main()
{
  float x, x0, y0, h, xn, y, k1,k2,k3,k4,k;
  cout<<"\nEnter the values of x0, y0, h, xn :\n";
  cin>>x0>>y0>>h>>xn;
  x=x0;
  y=y0;
  while(1)
    {
      if(x==xn)
           break;
      k1=h*f(x,y);
      k2=h*f(x+h/2,y+k1/2);
      k3=h*f(x+h/2,y+k2/2);
      k4=h*f(x+h,y+k3);
      k=(k1+(k2+k3)*2+k4)/6;
      x+=h;
      y+=k;

   }
 cout<<"\nWhen x="<<x<<" y="<<y<<"\n";
  return 0;
}

     OUTPUT

Enter the values of x0, y0, h, xn :
0 1 0.2 0.2

When x=0.2 y=1.1236
                             Newtonraphson method


//********************************************************************************
//Program name             :Newtonraphson.cpp
//Author              :
//Date Written           :17-03-2010
//Date Compiled           :17-03-2010
//Aim of program           :To find root of an equation using newton-raphson method
//********************************************************************************
#include<iostream>
using namespace std;
#include<iomanip.h>
#include<math.h>
float f(float x)
{
          return (x*x+3*x+1);
}
float df(float x)
{
          return (2*x+3);
}
int main()
{
          int p,maxt;
          float h,x0,x1,aerr;
          cout<<"\nEnter the values of x0,allowed error,maximum iterations"<<endl;
          cin>>x0>>aerr>>maxt;
          for(p=1;p<=maxt;p++)
            {
              h=f(x0)/df(x0);
              x1=x0-h;
              cout<<"Iteration no."<<p<<"x="<<setw(9)<<setprecision(6)<<x1<<endl;
                    if(fabs(h)<aerr)
                    {
                              cout<<"\nAfter"<<p<<"iterations,root"<<"="<<x1<<endl;
                              return 0;
                    }
                    x0=x1;
            }
          cout<<"\nSolutions does not converge"<<",iterations not sufficient"<<endl;
          return 1;
}
        OUTPUT



Enter the values of x0,allowed error,maximum iterations
1 0.0001 15
Iteration no.1x=     0
Iteration no.2x=-0.333333
Iteration no.3x=-0.380952
Iteration no.4x=-0.381966
Iteration no.5x=-0.381966

After 5 iterations,root=-0.381966
                                Euler method


//*********************************************************************************
//Program Name            : Euler.cpp
//Author             :
//Date written         : 10-02-2010
//Date Compiled          : 10-02-2010
//Aim of the program : To solve the differential equation using euler's method
//*********************************************************************************
#include<iostream>
using namespace std;
#include<math.h>
float df(float x,float y)
{
         return ((1+x)*y*y)/2;
}
int main()
{
         float x0,y0,h,x,x1,y1;
         cout<<"\nEnter the values of x0,y0,h,x"<<endl;
         cin>>x0>>y0>>h>>x;
         x1=x0;
         y1=y0;
         while(1)
         {
                   if(x1>x)
                   return 0;
                   y1+=h*df(x1,y1);
                   x1+=h;
                   cout<<"\nWhen x = "<<x1<<"\ty = "<<y1<<endl;
         }
}




       OUTPUT
Enter the values of x0,y0,h,x
0 1 .1 .02

When x = 0.1    y = 1.05

				
DOCUMENT INFO
Description: My C++ programs for numerical techniques in computer programming,Gauss elimination method, Gaussjordan method, Gaussiedal method, Simpson method, Trapezoidal method, Rungakutta method, Newtonraphson method, Euler method