Advertisement
Guest User

Untitled

a guest
Mar 3rd, 2015
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. #include <vector>
  4.  
  5. using namespace std;
  6.  
  7. // Function of given equation
  8. double function(double x)
  9. {
  10. double result;
  11. result = 0.2+25*x-200*pow(x,2)+675*pow(x,3)-900*pow(x,4)+400*pow(x,5);
  12. return result;
  13. }
  14.  
  15. int main()
  16.  
  17. {
  18. double boundup; // Upper bound
  19. double bounddown; // Lower bound
  20. double n; // Number of intervals
  21. double step; // Step size
  22. double fsimp; // Simpson's rule
  23. double ftrap; // Trapezoidal rule
  24.  
  25. cout << "Enter lower bound: " ;
  26. cin >> bounddown;
  27.  
  28. cout << "Enter upper bound: " ;
  29. cin >> boundup;
  30.  
  31. cout << "Number of intervals: " ;
  32. cin >> n;
  33.  
  34. // Vector class for both x and fx
  35. vector<double> x(n+1);
  36. vector<double> fx(n+1);
  37.  
  38. // Define first and last values in x and fx because they will not change
  39. step = (boundup-bounddown)/n;
  40. x[0] = bounddown;
  41. x[n] = boundup;
  42.  
  43. fx[0] = function(x[0]);
  44. fx[n] = function(x[n]);
  45.  
  46. fsimp = fx[0];
  47.  
  48. for (int i=0; i<n; i++)
  49. {
  50. x[i+1] = x[i]+step;
  51. fx[i+1] = function(x[i+1]);
  52.  
  53. if (i%2=0)
  54. {
  55. fsimp = 2*fx[i+1] + fsimp;
  56. }
  57.  
  58. if (i%2=1)
  59. {
  60. fsimp = 4*fx[i+1] + fsimp;
  61. }
  62.  
  63. fsimp = fx[n] + fsimp;
  64. }
  65.  
  66. cout << "Bounds of integration: " << bounddown << ", " << boundup << endl;
  67. cout << "Number of intervals: " << n << 'n' << endl;
  68. cout << "Integral value of f(x): " << fsimp << endl;
  69.  
  70. return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement