C Program for backward differences generation for interpolation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | #include<stdio.h> #include<math.h> #include<stdlib.h> #include<conio.h> void main() { //Create 2D array for backward difference table double y[20][20], x[20]; int i, j, k, num; //Accept inputs from the user printf("\nBackwad differences generation for Interpolation"); printf("\n\nEnter the value of x and y = f(x)"); scanf("%d", &num); for (i = 0; i < num; i++) { printf("\nEnter value of x%d : ", i); scanf("%lf", &x[i]); printf("\nEnter value of y%d : ", i); scanf("%lf", &y[i][0]); } //Loop is used to calculate backward difference k = 0; for (j = 1; j < num; j++) { k++; for (i = num - 1; i >= k; i--) { y[i][j] = y[i][j - 1] - y[i - 1][j - 1]; } } k = num; printf("\nBackward Difference Table\n"); printf("\n\tx\ty\tDy\tD2y\tD3y\tD4y\tD5y\tD6y\n"); for (i = 0; i < num; i++) { printf("\nx%d = %4.2lf", i, x[i]); for (j = 0; j < i + 1; j++) { printf("\t%4.2lf ", y[i][j]); } printf("\n"); } } |
Output :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | Backwad differences generation for Interpolation Enter the value of x and y = f(x) : 3 Enter value of x0 : 1 Enter value of y0 : 1 Enter value of x1 : 2 Enter value of y1 : 2 Enter value of x2 : 3 Enter value of y2 : 3 The backward difference table is as follows . . . x y Dy D2y D3y D4y D5y D6y x0 = 1.00 1.00 x1 = 2.00 2.00 1.00 x2 = 3.00 3.00 1.00 0.00 |