Tuesday 20 September 2011

Trapezoidal Rule


/* This program finds area of the function by Trapezoidal Rule*/

#include<stdio.h>
#include<math.h>

float area(float a)
{
 float f;
 f=1/(sqrt(1+a*a));
 return (f);
 }

 void main()
 {
 int i,n;
 float a,b,h, sum1,sum2,sum;
 clrscr();
 printf(" Type the lower limit a and upper limit b\n");
 scanf("%f%f", &a,&b);
 printf(" Type the number of interval n\n");
 scanf("%d", &n);
 sum1=area(a);
 sum1+=area(b);
 sum2=0;
 h=(b-a)/n;
 for(i=1;i<n;i++)
 sum2+=area(a+i*h);
 sum=h*(sum1+2*sum2)/2;
 printf(" The required area = %8.4f\n", sum);
 getch();
 }

Simpson's 1.3 Rule



/* This Program finds area by Simpson's 1/3 rule*/

#include<stdio.h>
#include<math.h>

float area(float x)
{
float f;
f=log(1+x*x)/(1+x*x);
return(f);
}

void main()
{
int i,n;
float a,b,h,sum1,sum2,sum3,sum;
clrscr();
printf("Type the lower limit a and upper limit b\n");
scanf("%f  %f",&a,&b);
printf("Type number of interval n\n");
scanf("%d",&n);
sum1=area(a);
sum1+=area(b);
sum2=0;
sum3=0;
h=(b-a)/n;
for(i=1;i<n;i+=2)
sum2+=area(a+i*h);
for(i=2;i<n-1;i+=2)
sum3+=area(a+i*h);
sum=h*(sum1+4*sum2+2*sum3)/3;
printf("The required area = %8.4f\n",sum);
getch();
}

Runge-Kutta Method


/* Numerical Solution of Ordinary Differential equations of First order by Runge-Kutta  method*/

#include<stdio.h>
#include<math.h>

float math(float x, float y)
{
float f;
f=sqrt(x+y);
return(f);
}

void main()
{
int i,n;
float x0,y0,x,h,k1,k2,k3,k4;
clrscr();
printf("Type the value of the point x where value of y to be found\n");
scanf("%f",&x);
printf("Type the initial value of x and y\n");
scanf("%f%f",&x0,&y0);
printf("Type the number of step length n\n");
scanf("%d",&n);
printf("x0=%f\t  y0=%f\t  n=%d \n",x0,y0,n);
h=(x-x0)/n;
for(i=1;i<=n;i++)
{
k1=h*math(x0,y0);
k2=h*math(x0+h/2,y0+k1/2);
k3=h*math(x0+h/2,y0+k2/2);
k4=h*math(x0+h,y0+k3);
y0+=(k1+2*k2+2*k3+k4)/6;
x0+=h;
}
printf("at x=%f\t,y=%f\n",x,y0);
getch();
}

Regula-Falsi Method


/*This program finds roots by Regula-Falsi method OR Method of False position */

#include<stdio.h>
#include<math.h>
#include<conio.h>

float root(float x)
{
float f;
f=x*x*x-x-1;
return(f);
}

void main()
{
int i,n;
float a,b,x,e,t,p;
clrscr();
printf("Type a and b\n");
scanf("%f %f",&a,&b);
printf("Type maximum number of interval n\n");
scanf("%d",&n);
printf("Type the value of e\n");
scanf("%f",&e);
p=root(a)*root(b);
if(p>0)
printf("Root does not lies between a and b");
else
{
i=1;
t=1;
while(t>e && i<=n)
{
x=(a*root(b)-b*root(a))/(root(b)-root(a));
if(root(x)*root(a)>0)
a=x;
else
b=x;
t=fabs(a-b);
if(t>fabs(root(x)))
t=fabs(root(x));
i++;
}
if(i>n)
printf("Iteration does not converge");
else
{
 printf("Approximate root=%8.4f\n",x);
 printf("Number of Iteration=%d\n",i);
}
}
getch();
}

Newton Forward and Backward Method


/* Interpolation by Newton Forward and Backward formulae */
#include<stdio.h>
#include<conio.h>

void main()
{
int n,i,j,op;
float x[10],y[10],xx,yy,d[3][10],u,t;
clrscr();
printf("\nNo. of pairs of points");
scanf("%d",&n);
for(i=0;i<n;i++)
{
 printf("%d th value of x and  wanted\t",i+1);
 scanf("%f%f",&x[i],&y[i]);
}
 for(i=0;i<3 && i<n-1;i++)
 for(j=0;j<n-i-1;j++)
 if(i==0)
  d[i][j]=y[j+1]-y[j];
  else
  d[i][j]=d[i-1][j+1]- d[i-1][j];
  while(1)
  {
  printf(" Give option: Press 1 for Forward: 2 for Backward:3 for Exit\n ");
  scanf("%d",&op);
  if(op==3)  break;
  printf("Give value of x for which y to be interpolated");
  scanf("%f",&xx);
  switch(op)
  {
   case 1: u=(xx-x[0])/(x[0]);yy=y[0];t=1;
       for(i=0;i<3 && i<n-1;i++)
       {
t*=(u-i)/(i+1);
yy+=t*d[i][0];
       }
  break;
   case 2: u=(xx-x[n-1])/(x[i]-x[0]);yy=y[n-1];t=1;
  for(i=0;i<3&&i<n-1;i++)
       {
t*=(u+i)/(i+1);
yy+=t*d[i][n-i-2];
       }
   }
   printf("x=%f\t y=%f\n",xx,yy);
   getch();
   }
  }

Newton Raphson Method


/* This Program finds root by Newton Raphson Method*/

#include<stdio.h>
#include<conio.h>
#include<math.h>

float root(float x)
{
float f;
f=x*sin(x)+cos(x);
return(f);
}

float diff(float x)
{
float g;
g=x*cos(x);
return(g);
}

void main()
{
int i,n;
float a,x,e,t;
clrscr();
printf("Type a\n");
scanf("%f",&a);
printf("Type maximum number of iteration n\n");
scanf("%d",&n);
printf("Type the value of e\n");
scanf("%f",&e);
i=1;t=1;
while(t>e&&i<=n)
{
 if(diff==0)
 {
  printf("\n Root does not found");
  printf("Type another value of a\n");
 }
 else
 x=a-root(a)/diff(a);
 t=fabs(x-a);
    if(t>fabs(root(x)))
 t=fabs(root(x));
 a=x;
 i++;
 printf("\n x=%f",x);
}
if(i>n)
printf("Iteration does not converge");
else
 {
  printf("\n The approximate root=%8.4f\n",x);


 printf("The number of iteration=%d\n",i);
 }
getch();
}

Lagranges Interpolation


/*Lagranges interpolation */


#include<stdio.h>
#include<math.h>
#include<conio.h>
void main()
{
int i,j,n;
float x[20],y[20],u,p,p1,sum;
clrscr();
printf("\nType no of pairs of points");
scanf("%d",&n);
printf("\nType the value which are to be found: u=");
scanf("%f",&u);
for(i=0;i<n;i++)
{
printf("x[%d]\t",i+1);
scanf("%f",&x[i]);
printf("y[%d]\t",i+1);
scanf("%f",&y[i]);
}
p=1;
for(i=0;i<n;i++)
p=p*(u-x[i]);
sum=0;
for(i=0;i<n;i++)
{
p1=y[i]/(u-x[i]);
for(j=0;j<n;j++)
if(i!=j)
p1=p1/(x[i]-x[j]);
sum+=p1;
}
p=p*sum;
for(i=0;i<n;i++)
printf("%8.4f\t %f\n",x[i],y[i]);
printf("The required value of the polynomial at u=%f\n is %8.4f\n",u,p);
getch();
}

Lagrange Interpolation Polynomial


/* The program for Lagrange Interpolation Polynomial which is used for unequal interval */

#include<stdio.h>
#include<math.h>

void main()
{
int i,j,n;
float x[20],y[20],u,p,p1,sum;
clrscr();
printf("Type the no of points: n=");
scanf("%d",&n);
printf("\nType the u which value will be calculated:u=");
scanf("%f",&u);
for(i=0;i<n;i++)
{
printf("x[%d]:",i+1);
scanf("%f",&x[i]);
printf("y[%d]:",i+1);
scanf("%f",&y[i]);
}
p=1;
for(i=0;i<n;i++)
p=p*(u-x[i]);
sum=0;
for(i=0;i<n;i++)
{
p1=y[i]/(u-x[i]);
for(j=0;j<n;j++)
if(i!=j)
p1=p1/(x[i]-x[j]);
sum+=p1;
}
p=p*sum;
for(i=0;i<n;i++)
printf("%8.4f\t %f\n",x[i],y[i]);
printf("The required value of the polynomial at u=%f is %8.4f\n",u,p);
getch();
}

Iteration Method


/ *  The program is find the root of Algebraic and Transcendental  Equations by Iteration Method */

#include<stdio.h>
#include<math.h>

float root (float x)
{
  float f;
  f = sin (x)–10*x +10;
  return (f) ;
 }

float ite (float x)
{
  float g;
  g=1+ sin (x)/10;
  return (g);
 }

void main()
{
  int i, n;
  float  a,b,x,e,t;
  printf(“ Type a \n”) ;
  scanf(“%f ”, &a );
  printf( “ Type maximum no iteration\n”);
  scanf (“%d”, &n);
  printf( “ Type the value of e \n”);
  scanf (“%f”, &e);
   i = 1;   t =1;
   while (t>e && i<=n)
   {
       x = ite (a) ;
       t = fabs (x-a) ;
       a = x;
        i + +;
      }
        if ( i > n)
        printf( “Iteration does not converge” );
        else
        {
          printf(“ approximate root = %8.4f\n ” ,x) ;
          printf (“ number of iteration = %d\n”,i);
         }
      }
  getch();
  return 0 ;
 }

Gaussian Quadrature


/* This program finds integral by gaussian quadrature*/

#include<stdio.h>
#include<math.h>

float g(float d)
{
  float f;
  p=1+d;
  return(p);
 }

void main()
{
  FILE *fp1;
  int  n,i,m;
  float u[100],w[50],sum,sum1,a,b,p1;
  fp1=fopen(“abc.dat”,”r”);
  printf(“type the number of points”);
  scanf(“%d ”, &n);
  printf(“type the lower limit a and upper limit b”);
  scanf(“%f%f ”, &a,&b);
  m=n/2;
  for(i=0; i<=n; i++)
  {
   fscanf(fp1,”%f%f”,&u[i],&w[i]);
   u[i+m] =- u[i];
   w[i+m] = w[i];
   printf(“u[i]=%f, w[i]=%f \n”,u[i],w[i]);
   }
   sum = 0;
   for(i=0; i<=n; i++)
   {
    p1= g((b-a)/2*u[i]+ (b+a)/2);
    sum = sum + w[i]*p1;
    }
    sum = sum *(b-a)/2;
    printf(“required integral = %f\n”, sum1);
    fclose(fp1);
    getch();
    }



  type the number of points n
  6
  type the lower limit a and upper limit b
0 1
u[i] = 0.236819 ,   w[i] = 0.467914
u[i] = 0.661209 ,   w[i] = 0.360762
u[i] = 0.932469,    w[i] = 0.171324
required integral = 1.500000

Gauss Seidel Iterative Method


/*Program for solution of system of linear solutions by Gauss Seidel iterative method*/

#include<stdio.h>
#include<conio.h>
#include<math.h>

void main()
{
  int n,i,j,k,l,nit;
  float a[10][10],b[10],x[10],x1[10],t;
  clrscr();
  printf(“Give no. of variables ”);
  scanf(“%d”, &n);
  for(i=0;i<=n;i++)
  {
     for(j=0;j<n;j++)  {
         printf(“%d ,%d   th coefficient wanted ” , i+1 ,j+1);
         scanf(“%f ”, &a[i][j]);
      }
      printf(“%d th constant wanted ”, i+1);
      scanf(“%f ”, &b[i]);
   }

/* Solution started*/
 for(i=0;i<=n;i++)
 {
     t = fabs(a[i][j]);
     k = i ;
     for(j=i+1;j<n; j++)
        if (fabs ( a[i][j] >t))
        {
          k = j;
          t = fabs ( a[j][i] );
         }
         if( t< 1e - 5){
            printf(“Almost singular matrix, solution not possible\n”);
            exit(0);
         }
         if (i!= k)
            for(l=0;l<n;l++){
                t = a[i][l];
                a[i][l] = a[k][l];
                a[k][l]= t;
                t = b[i] ;
                b[i]= b[l];
                b[l] = t;
             }
     }
     for(i=0;i<=n;i++)
     x[i] = 0;
     nit = 0;
     while(i)
     {
        for(i=0;i<=n;i++)
        {
           t = b[i] ;
           for(l=0;l<n;l++)
               if(l<i) t-=a[i][l] *x1[l];
               else
               if( l>i)t-= a[i][l] *x1[l];

               x1[i]= t/ a[i][i];
        }
        t = 0;
        for(l=0;l<n; l++)
        t += (x i [l] – x[l] ) * ( x1 [l] – x[l]) ;
        if (t< le -5) break;
        nit ++;
        if( nit >30)
       {
          printf(“Divergent\n”);
          exit(1);
         }
        for(l=0;l<n; l++)
        x [l] = x1 [l];
     }
    printf(“ Solution are \n”)  ;
    for( i=0 ; i< n ; i++)
    printf(“ x[%d]= %f \n”,i, x1[i]);
    getch();
  }

Gauss Jordan Method


/*Program for solution of system of linear solutions by Gauss Jordan method*/
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
  int n,i,j,k,l;
  float a[10][10],b[10],t;
  clrscr();
  printf(“Give no. of variables ”);
  scanf(“%d”, &n);
  for( i =0;i<= n; i++)
  {
     for(j=0;j<n;j++)  {
         printf(“%d ,%d   th coefficient wanted ” , i+1 ,j+1);
         scanf(“%f ”, &a[i][j]);
      }
      printf(“%d th constant wanted ”, i+1);
      scanf(“%f ”, &b[i]);
   }
/* Solution started*/
 for(i=0;i<= n; i++)
 {
     t = fabs(a[i][i]);
     k = i ;
     for(j=i+1;j<n; j++)
        if (fabs ( a[j][i] >t))
        {
          k = j;
          t = fabs ( a[j][i] );
         }
         if( t< 1e - 5){
            printf(“Almost singular matrix, solution not possible\n”);
            exit(0);
         }
         if (i!= k)
            for(l=0;l<n;l++){
                t = a[i][l];
                a[i][l] = a[k][l];
                a[k][l]= t;
                t = b[i] ;
                b[i]= b[l];
                b[l] = t;
             }
            t = a[i][i];
            for(l=0;l<=n;l++)  a[i][l]/=t;
b[i]/=t;
for( j=0;j<n;j++)
if( j!= i)
{        
                t= a[i][i] ;
                for(l=0;l<n;l++)  a[i][l]- = t*a[i][l];    
                 b[j]-= t*b[i];
              }
       }
    printf(“ Solution are \n”)  ;
    for( i=0 ; i< n ; i++)
    printf(“ x[%d]= %f \n”,i, b[i]);
    getch();
  }

Gauss Elimination Method


  /*Program for solution of system of linear solutions by Gauss Elimination  method*/

#include<stdio.h>
#include<conio.h>

void main()
{
  int status ,n,i,j;
  float a[10][10],b[10],x[10]t;
  clrscr();
  printf(“\n  SOLUTION  BY GAUSS ELIMINATION METHOD ”);
  printf(“ \n What is the size of the system  (n) ? \n”)
  scanf(“%d”, &n);
  printf(“\ Input coefficients a( i ,j ) , row- wise , \n  ”);
  printf(“ One row on each line \n”) ;
  for( i = 1; i<=n ; i++)
    for ( j=1; j<= n ; j++)
   {
     scanf (“ %f”, &t);
     a[i][j] = t;
    }
  printf(“ \n Input vector b \n”) ;
  for( i = 1; i<=n ; i++)
  {
    scanf (“ %f”, &t);
    b[i] = t;
   }
/ * Obtain solution by simple Gauss elimination method */
  gauss1 ( n,a,b,x, &status);
  if( status != 0)
  {
    printf(“\n SOLUTION VECTOR X \n”);
  for ( i = 1; i<=n ; i++)
     printf(“ % 10.6f ”, x[i]);
     printf(“\n”);
   }
   else
   {
    printf( “ singular matrix , reorder equation \n”);
    }
  getch();
  }
/ * End of main () program */

/* Defining subroutine gauss1( )  */
  gauss1 ( int n , float a[10][10],float b[10], float x[10], int *status)
  {
     int i,j,k;
     float pivot, factor ,sum;
     for(k = 1; k<= n-1; k++)
     {
       pivot a[k][k];
       if( fabs(pivot) < 0.000001 )
       {
         *status = 0;
           return ;
        }
        *status = 1;



         for( i=k+1;i<=n; i++)    
         {
           factor = a[i][k] / pivot;
           for ( j= k+1; j<=n; j++)
           {
             a[i][j] = a[i][j] – factor * a[k][j];
            }      
           b[i] = b[i] – factor * b[k];
         }
     }

 /* Back substitution begins */
   x[n]= b[n]/ a[n][n];
   for( k = n-1; k>= 1; k--)
   {
     sum = 0.0;
     for( j=k+1; j<=n ;j++)
       sum = sum + a[k][j]* x[j];
     x[k] = ( b[k] - sum) / a[k][k];
    }
   return ;
 }

Euler Method


/* This program finds solution of differential equation by Euler method*/

#include<stdio.h>
#include<math.h>

float fun(float x, float y)
{
  float f;
  f = x*x +y;
  return(f);
 }

void main()
{
  int i,n;
  float x0,y0,x,h;
  printf(“Type n\n”);
  scanf(“%d”,&n);
  printf(“Type x0 and y0\n”);
  scanf(“%f%f”,&x0,&y0);
  printf(“Type x\n”);
  scanf(“%d”,&x);
  h= (x-x0)/n;
  for(i=0; i<=n; i++)
  {
   y0+=fun(x0,y0)*h;
   x0+=h;
   }
   printf(“The required value= %8.4f\n”,y0);
   getch();
   }

Bisection Method


//Bisection Method

#include<stdio.h>
#include<math.h>
float root( float x)
{
  float f;
  f=x*x*x-x-1;
  return f;
}
void main()
{
 int i,n;
 float a,b,x,e,t,p;
 clrscr();
 printf(" Type a and b\n");
 scanf("%d%d",&a,&b);
 printf(" Type maximum no. of interval n\n");
 scanf("%d", &n);
 printf(" Type the value of e\n");
 scanf("%f",&e);
 p=root(a)*root(b);
 if(p>0)
  printf(" Root does not lie between a and b");
 else
  {
   i=1; t=1;
   while(t>e && i<=n)
   {
    x=(a+b)/2;
    if(root(x)*root(a)>0)
    a=x;
    else
    b=x;
    t=fabs(a-b);
    if(fabs(root(x)))
     t=fabs(root(x));
     i++;
    }
    if(i>n)
    printf(" Iteration does not converge");
    else
     {
     printf(" approximate root = %8.4f\n",x);
     printf( " Number of iteration = %d\n",i);
     }
   }
   getch();
   }

Backword Method


/*Backwad Method. */

#include<stdio.h>
#include<conio.h>
void main()
{
int n,i,j;
float x[10],y[10],xx,yy,d[3][10],u,t;
clrscr();
printf("No of pairs of points ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("%dth value of x , y wanted", i+1);
scanf("%f%f", &x[i],&y[i]);
}
for(i=0;i<3&& i<n-1;i++)
for(j=0;j<n-1;j++)
if(i==0)
d[i][j]=y[j+1]-y[j];
else
d[i][j]=d[i-1][j+1]- d[i-1][j];
printf("Give value of x for whichy to be interpolated");
scanf("%f",&xx);
u=(xx-x[n-1])/(x[1]-x[0]);
yy=y[n-1];
t=1;
for(i=0;i<3&&i<n-1;i++)
{
t*=(u+i)/(i+1);
yy+=t*d[i][n-i-2];
}
printf("x=%f\t y=%f\n",xx,yy);
getch();
}