Wednesday, 14 December 2011

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();
   }

No comments:

Post a Comment