upsidedown

newton backward interpolation

Oct 7th, 2011
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.06 KB | None | 0 0
  1. #include<stdio.h>
  2. void main()
  3. {
  4.     double ax[50],ay[50],diff[50][50];
  5.     double x,h,yp,nr=1,p,dr=1;
  6.     int n,i,j;
  7.     printf("enter the nuber of known data values:");
  8.     scanf("%d",&n);
  9.     printf("enter the values of x and y respectively\n");
  10.     for(i=0;i<n;i++)
  11.     {
  12.         printf("x%d=",i);
  13.         scanf("%lf",&ax[i]);
  14.         printf("  y%d=",i);
  15.         scanf("%lf",&ay[i]);
  16.         printf("\n");
  17.     }
  18.     printf("enter the value of x to find y\n");
  19.     scanf("%lf",&x);
  20.     h=ax[1]-ax[0];
  21.     for(i=0;i<n;i++)
  22.         diff[i][0]=ay[i+1]-ay[i];
  23.     for(j=1;j<(n-1);j++)
  24.     {
  25.         for(i=0;i<=(n-j);i++)
  26.             diff[i][j]=diff[i+1][j-1]-diff[i][j-1];
  27.     }
  28.     yp=ay[n-1];
  29.     i=n-1;
  30.     p=(x-ax[n-1])/h;
  31.     for(j=1;j<(n-1);j++)
  32.     {
  33.         nr=nr*(p+j-1);
  34.         dr=dr*j;
  35.         yp=yp+(nr/dr)*diff[i-1][j-1];
  36.         i--;
  37.     }
  38.     printf("For x = %.4lf y = %.4lf\n",x,yp);
  39. }
  40.  
  41. OUTPUT:
  42. enter the nuber of known data values:5
  43. enter the values of x and y respectively
  44. x0=200
  45.   y0=15.04
  46.  
  47. x1=250
  48.   y1=16.81
  49.  
  50. x2=300
  51.   y2=18.42
  52.  
  53. x3=350
  54.   y3=19.9
  55.  
  56. x4=400
  57.   y4=21.27
  58.  
  59. enter the value of x to find y
  60. 218
  61. For x = 218.0000 y = 15.7021
  62. Press any key to continue
  63.  
Add Comment
Please, Sign In to add comment