upsidedown

Newton fwd interpolation+ output

Aug 19th, 2011
180
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.29 KB | None | 0 0
  1. #include<stdio.h>
  2.  
  3. float fact(int x)
  4. {
  5.     if(x==1)
  6.         return 1;
  7.     else
  8.         return x*fact(x-1);
  9.  
  10. }
  11.  
  12.  
  13. int main()
  14. {
  15.     float y[10][10],x[10],u,t,a,ans,l,xp,sum;
  16.     int n,i,j;
  17.  
  18.     printf("enter n ");
  19.     scanf("%d",&n);
  20.     printf("\n enter the x and y values");
  21.     for(i=0;i<n;i++)
  22.     {
  23.         scanf("%f %f",&x[i],&y[0][i]);
  24.     }
  25.  
  26.     for(i=0;i<n;i++)
  27.         printf("\nx[%d]= %f \t y[%d]=%f",(i+1),x[i],(i+1),y[0][i]);
  28.  
  29.     i=0;
  30.     printf("\n");
  31.     printf("\n");
  32.  
  33.     do
  34.     {
  35.         i++;
  36.  
  37.         for(j=0;j<n-i;j++)
  38.         {
  39.             y[i][j]=y[i-1][j+1]-y[i-1][j];
  40.             printf("y=%f\t",y[i][j]);
  41.         }
  42.         printf("\n");
  43.  
  44.     }while(i<n);
  45.  
  46.     printf("Enter the value to be calculated");
  47.     scanf("%f",&xp);
  48.  
  49.     u=(xp-x[0])/(x[1]-x[0]);
  50.     l=u;
  51.     sum=0;
  52.     a=0;
  53.  
  54.     for(i=1;i<n-1;i++)
  55.     {
  56.         u=u*(l-i);
  57.         t=fact(i+1);
  58.         a=(u*y[i+1][0])/t;
  59.         sum+=a;
  60.     }
  61.  
  62.     ans=y[0][0]+l*y[1][0]+sum;
  63.     printf("\n\nthe ans is %f",ans);
  64.     return 0;
  65. }
  66.  
  67.  
  68.  
  69. OUTPUT:
  70. enter n 4
  71.  
  72.  enter the x and y values20
  73. .3420
  74. 23 .3907
  75. 26 .4384
  76. 29 .4848
  77.  
  78. x[1]= 20.000000          y[1]=0.342000
  79. x[2]= 23.000000          y[2]=0.390700
  80. x[3]= 26.000000          y[3]=0.438400
  81. x[4]= 29.000000          y[4]=0.484800
  82.  
  83. y=0.048700      y=0.047700      y=0.046400
  84. y=-0.001000     y=-0.001300
  85. y=-0.000300
  86.  
  87. Enter the value to be calculated21
  88.  
  89.  
  90. the ans is 0.358326Press any key to continue
Advertisement
Add Comment
Please, Sign In to add comment