Advertisement
Guest User

Trapezoidal Rule

a guest
Jun 19th, 2019
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.85 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. double a, b, x[10], y[10], delX, T_n, result;
  6.  
  7. void Trapizoidal_rules(double n)
  8. {
  9.     int i;
  10.     delX = (b - a)/n;
  11.     for( i = 0; i <= n; i ++)
  12.     {
  13.         x[i] =  a;
  14.         a += delX;
  15.     }
  16.  
  17.     for(i = 0; i <= n; i++)
  18.     {
  19.         y[i] = sqrt(1 - x[i]*x[i]);
  20.     }
  21.  
  22.     for(i = 0; i <= n; i ++)
  23.     {
  24.         if((i ==  0) || (i ==  n))
  25.             T_n += y[i];
  26.         else
  27.             T_n += 2*y[i];
  28.     }
  29.  
  30.     result = T_n * 0.5 * delX;
  31.  
  32.  
  33. }
  34.  
  35.  
  36. int main()
  37. {
  38.     double n;
  39.     int j = 1;
  40.     cout<< "Please input the value of n." << endl;
  41.     cin >> n;
  42.     cout <<"Now input the limit(lower limit, upper limit):";
  43.     cin >> a >> b;
  44.  
  45.     Trapizoidal_rules(n);
  46.  
  47.         cout << "While n = " <<  n <<  " and result is = " << result  << endl;
  48.  
  49.  
  50.  
  51.     return 0;
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement