tampurus

Unit 3.1 Forward Differentiation

May 4th, 2022 (edited)
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.12 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[0] )/h;
  21.     printf("\nh=%f u= %f",h,u);
  22.     sum = y[0][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. Output
  55. Enter the value of n:6
  56.  
  57. Enter the elements of x: 1931 1941 1951 1961 1971 1981
  58. Enter the elements of y: 12 15 20 27 39 52
  59.  
  60. Enter x for which y is to be calculated:1996
  61.  
  62. h=10.000000 u= 6.500000The table is :
  63.          1931.00        12.00,  3.00,   2.00,   0.00,   3.00,   -10.00, 0.00,
  64.          1941.00        15.00,  5.00,   2.00,   3.00,   -7.00,  0.00,
  65.          1951.00        20.00,  7.00,   5.00,   -4.00,  0.00,
  66.          1961.00        27.00,  12.00,  1.00,   0.00,
  67.          1971.00        39.00,  13.00,  0.00,
  68.          1981.00        52.00,  0.00,
  69.  
  70. Term=6.500000
  71. Factorial=1
  72. Term=35.750000
  73. Factorial=2
  74. Term=160.875000
  75. Factorial=6
  76. Term=563.062500
  77. Factorial=24
  78. Term=1407.656250
  79. Factorial=120
  80.  
  81. for x = 1966.000000 the ultimate answer is sum =  20.328125
  82. */
Add Comment
Please, Sign In to add comment