Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.60 KB | None | 0 0
  1. #include <iostream>
  2. #include <cmath>
  3. using namespace std;
  4. double f(double x)
  5. {
  6.     double fx = 0;
  7.     fx = x;
  8.     return fx;
  9. }
  10. /*double S(double m)
  11. {
  12.        double sum = 0, x, a=0, b=0, h, fx;
  13.        h = (b-a)/(m);
  14.        for(double i = 1; i < m; i++)
  15.              {
  16.                    x = a+i*h;
  17.                    fx = f(x);      
  18.                    sum += fx;
  19.              }
  20.        return sum;
  21. }
  22. */
  23. int main()
  24. {
  25.     double m = 4;
  26.     double sum = 0, a=0, b=1, h, h1;
  27.     double e=0.0001, grl, grl1, sum1=0;
  28.     double x, fx, fx1, fxn, z, col;
  29.     h = (b-a)/(m);
  30.     h1 = (b-a)/(2*m);
  31.     col = m;
  32.    
  33.      do{  
  34.            sum = sum1 = 0;
  35.            h = (b-a)/(m);
  36.            h1 = (b-a)/(2*m);
  37.            for(double i = 1; i < m; i++)
  38.              {
  39.                    x = a+i*h;
  40.                    fx = f(x);      
  41.                    sum += fx;
  42.              }
  43.              for(double i = 1; i < 2*m; i++)
  44.              {                  
  45.                    x = a+i*h1;
  46.                    fx = f(x);      
  47.                    sum1 += fx;
  48.              }
  49.              x = a;
  50.              fx1 = f(x);
  51.              x = b;
  52.              fxn = f(x);
  53.              //cout << "S = " << S(m) << endl;
  54.              //cout << "S = " << S(2*m) << endl;
  55.              grl = h*(sum + (fx1 + fxn)/2);
  56.              grl1 = (h1)*(sum1 + (fx1 + fxn)/2);    
  57.              z = grl - grl1;
  58.              col *= 2;
  59.              m *= 2;
  60.        }while(fabs(z) > e);            
  61.        
  62.        cout << "I2 = " << grl1 << endl;
  63.        cout << "Col = " << col << endl;
  64.  
  65.     system ("pause");
  66.  
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement