Advertisement
Guest User

Untitled

a guest
May 22nd, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
QBasic 2.50 KB | None | 0 0
  1.  
  2. //////////////////////////////////////////////////////////////////
  3. // This program approximates an integral by USING more AND more //
  4. // points in the Simpson rule.  It requires a FUNCTION          // 
  5. // program FOR the integrand, the interval (a,b) of             //
  6. // integration, an initial number of points, n0, FOR the        //
  7. // approximation, AND a convergence parameter, eps              //
  8. //////////////////////////////////////////////////////////////////
  9.  
  10. #include <iostream.h>
  11. #include <math.h>
  12. float rectangle(float a, float b, INT n); // FUNCTION prototype
  13. float f(float x); // FUNCTION prototype
  14. float simpson(float a, float b, INT n);     // funt prototype
  15. INT main(){
  16.     INT n0; //
  17.     float a, b, eps; //
  18.     // Program Instructions
  19.     cout <<"This program approximates an integral by using more and more\n";
  20.     cout << "points in the Simpson rule.  It requires a function \n";
  21.     cout << "program for the integrand, the interval (a,b) of \n";
  22.     cout << "integration, an initial number of points, n0, for the \n";
  23.     cout << "approximation, and a convergence parameter, eps." << endl;
  24.     cout <<"\n"; // break
  25.     cout << "Enter: a, b, n0, eps = ?\n"; // prompt FOR INPUT
  26.     cin >> a >> b >> n0 >> eps; // INPUT a, b, n0, eps
  27.     cout <<"\n";
  28.     cout << "a = " << a << "     b = " << b << endl; // echo user INPUT
  29.     cout <<"\n";
  30.     cout << "n0 = " << n0 << "     eps = " << eps << endl; //
  31.     cout <<"\n";
  32.     // compute the first two approximations TO integral
  33.     float SC; // SC will hold the current value of S
  34.     SC = simpson(a, b, n0); //
  35.     INT n = n0 + n0;
  36.     float SN; // SN will hold the NEXT value of S
  37.     SN = simpson(a, b, n);
  38.     // repeat integral approximations UNTIL converged
  39.     WHILE(fabs(SN - SC) > eps){
  40.         n = n + n; // DOUBLE n
  41.         SC = SN; // Save current approximation
  42.         SN = simpson(a, b, n); // Compute NEXT approximation
  43.         // Display the results
  44.         cout << "Integral = " << SN << " for n = " << n << endl; //
  45.    
  46.     } //
  47.     cout <<"\n";
  48. RETURN 0;
  49.    
  50. }
  51.  
  52. // This IS a particular FUNCTION TO be integrated
  53.  
  54. float f(float x){
  55.     RETURN x/(1.0 + x * x);
  56. }
  57.  
  58. // This FUNCTION computes simpsons rule approximation TO the
  59. // integral of a FUNCTION f over an interval (a,b) USING n points
  60.  
  61. float simpson(float a, float b, INT n){
  62.     float h = (b - a)/(2 * n);
  63.     float sum_even, sum_odd;
  64.     sum_even = 0;
  65.     sum_odd = 0;
  66.     FOR(INT i = 1; i <= n; i++) sum_odd = sum_odd + f(a + (2 * i - 1) * h);
  67.     FOR(INT j = 1; j < n; j++) sum_even = sum_even + f(a + 2 * j * h);
  68.    
  69.     RETURN ((f(a) + f(b) + 4 * sum_odd + 2 * sum_even) * h / 3);
  70.    
  71. } // ends the FUNCTION
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement