upsidedown

newtons forward interpolation

Aug 19th, 2011
160
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.03 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<math.h>
  3. double fact(double x)
  4. {
  5.     if(x==1)
  6.         return 1;
  7.     else
  8.         return (x*fact(x-1));  
  9. }
  10. void main()
  11. {
  12.     int n,i,j;
  13.     double x[100],y[100][100],xp,u,l,t,sum=0,a,ans,k;
  14.     printf("enter the number of values to be entered\n");
  15.     scanf("%d",&n);
  16.     printf("enter the values of x and y respectly\n");
  17.     for(i=0;i<n;i++)
  18.         scanf("%lf %lf",&x[i],&y[0][i]);
  19.     printf("the values of x and y are\nx:\t");
  20.     for(i=0;i<n;i++)
  21.         printf("%.0lf   ",x[i]);
  22.     printf("\ny:\t");
  23.     for(i=0;i<n;i++)
  24.         printf("%.4lf   ",y[0][i]);
  25.     printf("\n(dell)y:\t");
  26.     i=0;
  27.     k=n;
  28.     n--;
  29.     do
  30.     {
  31.         i++;
  32.         for(j=0;j<n;j++)
  33.         {
  34.             y[i][j]=y[i-1][j+1]-y[i-1][j];
  35.             printf("%.4lf   ",y[i][j]);
  36.         }
  37.         n--;
  38.         if((i+1)<k)
  39.         printf("\n(dell)%dy:\t",(i+1));
  40.     }while(i<k);
  41.     printf("\nenter the value to be calculated\n");
  42.     scanf("%lf",&xp);
  43.     u=(xp-x[0])/(x[1]-x[0]);
  44.     l=u;
  45.     for(i=1;i<(k-1);i++)
  46.     {
  47.         u=u*(l-i);
  48.         t=fact(i+1);
  49.         a=(u*y[i+1][0])/t;
  50.         sum=sum+a;
  51.     }
  52.     ans=y[0][0]+(l*y[1][0])+sum;
  53.     printf("the answer is %.4lf\n",ans);
  54. }
Advertisement
Add Comment
Please, Sign In to add comment