Wednesday 14 December 2011

c program of Least Square method for curve fitting.


Implementation of Least-square method for curve fitting.

#include<stdio.h>
#include<conio.h>
#include<math.h>
 main()
{
  float x[10],y[10],a[10][10];
  int i,j,k,n,itr;
  printf("\n ENTER THE SIZE OF MATRIX n:");
  scanf("%d",&n);
  printf("\n ENTER MATRIX ELEMENTS AND RHS:\n");
  for(i=1;i<=n;i++)
  {
    for(j=1;j<=n+1;j++)
    scanf("%f",&a[i][j]);
  }
  for(i=1;i<=n;i++)
  {
    x[i]=0.0;
    y[j]=0.0;
  }
  itr=0.0;
  top:
  itr=itr+1;
  for(i=1;i<=n;i++)
  {
    x[i]=a[i][n+1];
    for(j=1;j<=n;j++)
    {
      if(i==j)
      continue;
     else
     x[i]=x[i]-a[i][j]*x[j];
    }
    x[i]=x[i]/a[i][j];
  }
  for(k=1;k<=n;k++)
  if(fabs(x[k]-y[k])>0.0001)
  {
    printf("\n ITERATION=%d}",itr);
    for(i=1;i<=n;i++)
    {
      y[i]=x[i];
      printf("\n x(%d)=%f",i,x[i]);
    }
    goto top;
    }
    else
    continue;
    return;

    }

c program of simpson's 1/3 rule.


Implementation of Simpson’s 1/3 Rule.

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#include<string.h>
float fun(float);
void main()
{
  float result=1;
  float a,b;
  float h,sum;
  int i,j;
  int n;
  clrscr();
  printf("\n\n Enter the range-");
  printf("\n\n Lower Limit a-");
  scanf("%f",&a);
  printf("\n\n Lower Limit b-");
  scanf("%f",&b);
  printf("\n\n Enter number of subintervals -");
  scanf("%d",&n);
  h=(b-a)/n;
  sum=0;
  sum=fun(a)+4*fun(a+h)+fun(b);
  for(i=3;i<n;i++)
  {
    sum+=fun(a+(i-1)*h)+4*fun(a+i*h);
  }
  result=sum*h/3;
  printf("\n\n\n\n Value of the integral is %6.4f\t",result);
  printf("\n\n\n Press Enter to Exit");
  getch();
  }
  float fun(float x)
  {
    float temp;
    temp=1/(1+(x*x));
    return temp;
 }

c program of Gauss-seidel method.


Implementation of Gauss-Seidel Method.

#include<stdio.h>
#include<conio.h>
#include<math.h>
 main()
{
  float x[10],y[10],a[10][10];
  int i,j,k,n,itr;
  printf("\n ENTER THE SIZE OF MATRIX n:");
  scanf("%d",&n);
  printf("\n ENTER MATRIX ELEMENTS AND RHS:\n");
  for(i=1;i<=n;i++)
  {
    for(j=1;j<=n+1;j++)
    scanf("%f",&a[i][j]);
  }
  for(i=1;i<=n;i++)
  {
    x[i]=0.0;
    y[j]=0.0;
  }
  itr=0.0;
  top:
  itr=itr+1;
  for(i=1;i<=n;i++)
  {
    x[i]=a[i][n+1];
    for(j=1;j<=n;j++)
    {
      if(i==j)
      continue;
     else
     x[i]=x[i]-a[i][j]*x[j];
    }
    x[i]=x[i]/a[i][j];
  }
  for(k=1;k<=n;k++)
  if(fabs(x[k]-y[k])>0.0001)
  {
    printf("\n ITERATION=%d}",itr);
    for(i=1;i<=n;i++)
    {
      y[i]=x[i];
      printf("\n x(%d)=%f",i,x[i]);
    }
    goto top;
    }
    else
    continue;
    return;

    }

c program of Regular-Falsi Method.


Implementation of Regular-Falsi Method.

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<string.h>
#include<process.h>
#define EPS 0.00005
#define f(x) 3*x+sin(x)-exp(x)
void FAL_POS();
void main()
{
  clrscr();
printf("\n Solution by FALSE POSITION method\n");
printf("Equation is");
printf("\n\t\t\t 3*x+sin(x)=0\n\n");
FAL_POS();
void FAL_POS()
{
  float f0,f1,f2;
  float x0,x1,x2;
  int i;
printf("Enter the number of iteration:");
scanf("%d",&itr);
for(x1=0.0;;)
{
   f1=f(x1);
if(f1>0)
{
  break;
}
else
{
  x1=x1+0.1;
}
}
x0=x1-0.1;
f0=f(x0);
printf("\n\t\t----------------------------");
printf("\n\t\t     ITERATION\t x2\t\t F(x)\n");
for(i=0;i<itr;i++)
{
  x2=x0-((x1-x0)/(f1-f0))*f0;
  f2=f(x2);
if(f0*f2>0)
{
 x1=x2;
 f1=f2;
}
else
{
x0=x2;
f0=f2;
}
if(fabs(f(2))>EPS)
{
  printf("\n\t\t            %d\t%f\n",i+1,x2,f2);
}
}
printf("\t\t---------------------------");
printf("\n\t\t\tRoot=%f\n",x2);
printf("\t\t---------------------------");
getch();
}

c program of Bisection Method.


Implementation of Bisection Method.

#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<process.h>
#include<string.h>
#define EPS 0.00000005
#define F(x) (x)*log10(x)-1.2
void Bisect();
int count=1,n;
float root=1;
void main()
{
 clrscr();
printf("\n Solution by BISECTION method\n");
printf("\ Equation is");
printf("\n\t\t\t x*log(x)-1.2=0 \n");
printf("\n Enter the number of iterations\n");
scanf("%d",&n);
Bisect();
getch();
}
void Bisect()
{
float x0,x1,x2;
float f0,f1,f2;
for(x2=1; ;x2++)
{
f2=F(x2);
if(f2>0)
{
break;
}
}
for(x1=x2-1; ;x2++)
{
f1=F(x1);
if(f1<0)
{
break;
}
}
printf("\t\t----------------------------------------");
printf("\n\t\t ITERATIONS\t\t\  ROOTS\n");
printf("\t\t-----------------------------------------");
for(;count<n;count++)
{
x0=(x1+x2)/2.0;
f0=F(x0);
if(f0==0)
{
root=x0;
}
if(f0*f1<0)
{
x2=x0;
}
else
{
x1=x0;
f1=f0;
}
printf("\n\t\t ITERATION%d",count);
printf("\t  :\t   %f",x0);
if(fabs((x1-x2)/x1) <EPS)
{
printf("\n\t\t--------------------------------");
printf("\n\t\t       Root= %f",x0);
printf("\n\t\t   Iterations = %d\n",count);
printf("\t\t-----------------------------------");
getch();
exit(0);
}
}
printf("\n\t\t-------------------------------------");
printf("\n\t\t\t Root = %7.4f",x0);
printf("\n\t\t\t  Iteration = %d\n", count-1);
printf("\t\t---------------------------------------");
getch();}

c program of Trapezoidal Rule.


Implementation of Trapezoidal Rule.
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<math.h>
#include<process.h>
float fun(float);
void main()
{
 float result=1;
 float a,b;
 float h,sum;
 int i,j;
 int n;
 clrscr();
 printf("\n\n Enter the range-");
 printf("\n\n Lower Limit a-");
 scanf("%f",&a);
 printf("\n\n Upper Limit b-");
 scanf("%f",&b);
 printf("\n\n Enter number of subintervals-");
 scanf("%d",&n);
 h=(b-a)/n;
 sum=0;
 sum=fun(a)+fun(b);
 for(i=1;i<n;i++)
 {
   if(i%3==0)
   {
     sum+=2*fun(a+i*h);
     }
    
       result=sum*h/2;
       printf("\n\n\n Value of the intergal is %6.4f\t",result);
       printf("Press enter to Exit");
       getch();
       }
       float fun(float x)
       {
           float temp;
         temp=1/(1+(x*x));
         return temp;
        }  

c program of simpson's 3/8 rule.



C  Propgram to implement simpson's 3/8rd rule.

#include<stdio.h>
#include<math.h>
#include<conio.h>
#include<process.h>
#include<string.h>
float fun(float);
void main()
{
 float result=1;
 float a,b;
 float h,sum;
 int i,j;
 int n;
 clrscr();
 printf("\n\n Enter the range-");
 printf("\n\n Lower Limit a-");
 scanf("%f",&a);
 printf("\n\n Upper Limit b-");
 scanf("%f",&b);
 printf("\n\n Enter number of subintervals-");
 scanf("%d",&n);
 h=(b-a)/n;
 sum=0;
 sum=fun(a)+fun(b);
 for(i=1;i<n;i++)
 {
   if(i%3==0)
   {
     sum+=2*fun(a+i*h);
     }
     else
     {
       sum+=3*fun(a+(i)*h);
       }
       }
       result=sum*3*h/8;
       printf("\n\n\n Value of the intergal is %6.4f\t",result);
       printf("Press enter to Exit");
       getch();
       }
       float fun(float x)
       {
             float temp;
             temp=1/(1+(x*x));
             return temp;
             }

c programe of Newton backward interpolation formula


Implementatio of Newton’s Backward Interpolation Formula.

#include<stdio.h>
#include<conio.h>
#include<math.h>
#include<process.h>
#include<string.h>
void main()
{
  int n;
  int i,j,k;
  float mx[10];
  float my[10];
  float x;
  float x0=0;
  float y0;
  float sum;
  float h;
  float fun;
  float p;
  float diff[20][20];
  float y1,y2,y3,y4;
  clrscr();
  printf("\n Enter the number of term -");
  scanf("%d",&n);
  printf("\n Enter the value in the form of x - -");
  for(i=0;i<n;i++)
  {
     printf("\n Enter the value of x%d  -",i+1);
     scanf("%f",&mx[i]);
  }
  printf("\n Enter the value in the form of y - -");
  for(i=0;i<n;i++)
  {
     printf("\n Enter the value of y%d  -",i+1);
     scanf("%f",&my[i]);
  }

   printf("\n Enter the value of x for- -");
   printf("\n which you want the value of y -");
   scanf("%f",&x);
   h=mx[1]-mx[0];
   for(i=0;i<n-1;i++)
   {
     diff[i][1]=my[i+1]-my[i];
   }
   for(j=2;j<=4;j++)
   {
     for(i=0;i<n-j;i++)
     {
               diff[i][j]=diff[i+1][j-1]-diff[i][j-1];
     }
  }
  i=0;
  while(!mx[i]>x)
  {
   i++;
   }
   x0=mx[i];
   sum=0;
   y0=my[i];
   fun=1;
   p=(x-x0)/h;
   sum=y0;
   for(k=1;k<=4;k++)
   {
     fun=(fun*(p-(k-1)))/k;
     sum=sum+fun*diff[i][k];
   }
   printf("\n when x=%6.4f,y=%6.8f",x,sum);
   printf("\n\n\n Press Enter to Exit");
   getch();
   }