Advertisement
Guest User

Untitled

a guest
Jun 18th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. class Newton_Interpolation
  4. {
  5. public:
  6.  
  7. int i,j,n;
  8. double x[100],y[100];
  9. void get_input()
  10. {
  11. cin>>n;
  12. for( i = 0; i < n; i++)
  13. {
  14. cin>>x[i]>>y[i];
  15. }
  16.  
  17. }
  18. double fun(int start,int end)
  19. {
  20. if( start == end)
  21. return y[start];
  22. if(start+1 == end)
  23. {
  24. return (( y[end] - y[start]) / ( x[end] - x[start]));
  25. }
  26.  
  27. return (fun( start+1 , end) - fun(start, end-1)) / (x[end] - x[start]);
  28. }
  29. double funx(int i,double value)
  30. {
  31. double temp = 1;
  32. for(int j = 0; j<i; j++)
  33. {
  34. temp *= value - x[j];
  35. }
  36. return temp;
  37. }
  38. double calculation(double x)
  39. {
  40. double fx = 0;
  41. for( i=0; i <=n; i++)
  42. {
  43. fx += (fun(0,i) * funx(i,x));
  44. }
  45. return fx;
  46. }
  47.  
  48. };
  49. int main()
  50. {
  51. Newton_Interpolation ob;
  52.  
  53. ob.get_input();
  54. double value;
  55. cin>>value;
  56. cout<<fixed<<setprecision(3)<<ob.calculation(value)<<endl;
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement