thespeedracer38

Trapezoidal Method by AN Sir

Feb 7th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.06 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3.  
  4. using namespace std;
  5.  
  6. class Trapezoidal{
  7.     public:
  8.         double function1(double x);
  9.         double trapezoidal(double a, double b, long n);
  10. };
  11.  
  12. double Trapezoidal::function1(double x){
  13.     double y;
  14.     //y = 4 / (1 + x * x);
  15.     //y = 1 / (1 + x);
  16.     y = x * x * x * x;
  17.     return y;
  18. }
  19.  
  20. double Trapezoidal::trapezoidal(double a, double b, long n){
  21.     long  i;
  22.     double s1, s2, h, i_trap;
  23.     h = (b - a)/n;
  24.     s1 = function1(a) + function1(b);
  25.     s2 = 0;
  26.     for(i = 1; i < n; i++){
  27.         s2 = s2 + function1(a + i*h);
  28.     }
  29.     i_trap = 0.5 * h * (s1 + 2 * s2);
  30.     cout << "Number of intervals = " << n << endl;
  31.     cout << "Value of integral using Trapezoidal Rule = " << i_trap << endl;
  32.     return i_trap;
  33. }
  34.  
  35. int main(int argc, char** argv) {
  36.     double a, b, it;
  37.     long n;
  38.     system("cls"); 
  39.     cout << "Enter lower limit of your integral: ";
  40.     cin >> a;
  41.     cout << "Enter upper limit of your integral: ";
  42.     cin >> b;
  43.     cout << "Enter total number of intervals: ";
  44.     cin >> n;
  45.     Trapezoidal T;
  46.     it = T.trapezoidal(a, b, n);
  47.     cin.ignore();
  48.     cin.get();
  49.     return 0;
  50. }
Add Comment
Please, Sign In to add comment