Advertisement
tampurus

Unit 3.2 Backword Differentiation

May 19th, 2022 (edited)
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.01 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <math.h>
  3. int main()
  4. {
  5.     int i,n,j,fact=1;
  6.     float x[20],y[20][20],h,term=1,u,sum,xval;
  7.     printf("Enter the value of n:");
  8.     scanf("%d",&n);
  9.     printf("\nEnter the elements of x: ");
  10.     for(i=0 ; i<n ; i++){
  11.         scanf("%f",&x[i]);
  12.     }
  13.     printf("Enter the elements of y: ");
  14.     for(i=0 ; i<n ; i++){
  15.         scanf("%f",&y[i][0]);
  16.     }
  17.     h=x[1]-x[0];
  18.     printf("\nEnter x for which y is to be calculated:");
  19.     scanf("%f",&xval);
  20.     u = (xval - x[n-1] )/h;
  21.     printf("\nh=%f u= %f",h,u);
  22.     sum = y[n-1][0];
  23.    
  24.     for(j=1 ; j<n ; j++){
  25.         for(i=0 ; i< (n-j) ; i++){
  26.             // when j =2 then i =0 ,1 & when j =3 then i =0)
  27.             y[i][j]=y[i+1][j-1] - y[i][j-1];
  28.         }
  29.     }
  30.     printf("The table is :\n");
  31.     for(i=0; i<n ; i++){
  32.         printf("\t %.2f ",x[i]);
  33.         for(j=0 ; j<(n-i)+1 ; j++){
  34.             printf("\t%.2f,",y[i][j]);
  35.         }
  36.         printf("\n");
  37.     }
  38.    
  39.     for(j=1 ; j<n ; j++){
  40.         for(i=1 ; i<=j ; i++){
  41.             fact = fact * i;
  42.         }
  43.         term = term * (u-(j-1));
  44.         printf("\nTerm=%f",term);
  45.         printf("\nFactorial=%d",fact);
  46.         sum = sum + ((y[0][j] * term) / fact);
  47.         fact = 1;
  48.     }
  49.     printf("\n\nfor x = %f the ultimate answer is sum =  %f",xval,sum);
  50.    
  51.     return 0;
  52. }
  53.  
  54. /*
  55. Enter the value of n:5
  56.  
  57. Enter the elements of x: 1891
  58. 1901
  59. 1911
  60. 1921
  61. 1931
  62. Enter the elements of y: 12
  63. 15
  64. 20
  65. 27
  66. 39
  67.  
  68. Enter x for which y is to be calculated:1925
  69.  
  70. h=10.000000 u= -0.600000The table is :
  71.          1891.00        12.00,  3.00,   2.00,   0.00,   3.00,   0.00,
  72.          1901.00        15.00,  5.00,   2.00,   3.00,   0.00,
  73.          1911.00        20.00,  7.00,   5.00,   0.00,
  74.          1921.00        27.00,  12.00,  -0.00,
  75.          1931.00        39.00,  0.00,
  76.  
  77. Term=-0.600000
  78. Factorial=1
  79. Term=0.960000
  80. Factorial=2
  81. Term=-2.496000
  82. Factorial=6
  83. Term=8.985600
  84. Factorial=24
  85.  
  86. for x = 1925.000000 the ultimate answer is sum =  39.283199
  87. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement