Advertisement
sajid161

gauss backward

Mar 14th, 2020
214
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. double x[100];
  4. double y[100][100];
  5. double find_p(double x1,int n)
  6. {
  7. double temp;
  8. int k=1,s=4;
  9. if(n==0)
  10. {
  11. return 1;
  12. }
  13. for(int j=1; j<=n; j++)
  14. {
  15. if(j==1)
  16. {
  17. temp=x1;
  18. continue;
  19. }
  20. if(j==s)
  21. {
  22. s=s+2;
  23. if(j%2==0)
  24. k++;
  25. }
  26. if(j%2==0)
  27. {
  28. temp=temp*(x1+k);
  29.  
  30. }
  31. else
  32. {
  33. temp=temp*(x1-k);
  34.  
  35. }
  36.  
  37. }
  38.  
  39. return temp;
  40. }
  41.  
  42.  
  43. int fact(int i)
  44. {
  45. int f=1;
  46. for(int j=i; j>=1; j--)
  47. {
  48. f*=j;
  49.  
  50. }
  51. return f;
  52. }
  53. int main()
  54. {
  55. int n,p;
  56. int q;
  57. cout<<"enter the total element of x and y:";
  58. cin>>n;
  59. for(int i=0; i<n; i++)
  60. {
  61. cout<<"enter the element of x:";
  62. cin>>x[i];
  63. cout<<"enter the element of y[][]:";
  64. cin>>y[i][0];
  65.  
  66. }
  67. for(int i=1; i<n; i++)
  68. {
  69. for(int j=0; j<n-i; j++)
  70. {
  71. y[j][i]=y[j+1][i-1]-y[j][i-1];
  72.  
  73. }
  74. }
  75. cout<<"difference table:"<<endl;
  76. for(int i=0; i<n; i++)
  77. {
  78. cout<<x[i]<<"\t";
  79. for(int j=0; j<n-i; j++)
  80. {
  81. cout<<y[i][j]<<"\t";
  82. }
  83. cout<<endl;
  84. }
  85. if(n%2==0)
  86. {
  87. p=n/2;
  88. q=x[p];
  89. }
  90. else
  91. {
  92. p=n/2;
  93. q=x[p];
  94. }
  95. double value,x1;
  96. cout<<"enter desired value:";
  97. cin>>value;
  98. x1=(value-q)/(x[1]-x[0]);
  99. double sum=0;
  100. int k=1;
  101. cout<<q<<endl;
  102.  
  103. for(int i=0; i<n; i++)
  104. {
  105. if(i==1)
  106. {
  107. p--;
  108. }
  109. if(i==k+2)
  110. {
  111. k=k+2;
  112. p--;
  113.  
  114. }
  115.  
  116. sum=sum+(find_p(x1,i)*y[p][i])/fact(i);
  117.  
  118.  
  119. }
  120. cout<<"desired y="<<sum;
  121.  
  122.  
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement