Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- using namespace std;
- class Trapezoidal{
- public:
- double function1(double x);
- double trapezoidal(double a, double b, long n);
- };
- double Trapezoidal::function1(double x){
- double y;
- //y = 4 / (1 + x * x);
- //y = 1 / (1 + x);
- y = x * x * x * x;
- return y;
- }
- double Trapezoidal::trapezoidal(double a, double b, long n){
- long i;
- double s1, s2, h, i_trap;
- h = (b - a)/n;
- s1 = function1(a) + function1(b);
- s2 = 0;
- for(i = 1; i < n; i++){
- s2 = s2 + function1(a + i*h);
- }
- i_trap = 0.5 * h * (s1 + 2 * s2);
- cout << "Number of intervals = " << n << endl;
- cout << "Value of integral using Trapezoidal Rule = " << i_trap << endl;
- return i_trap;
- }
- int main(int argc, char** argv) {
- double a, b, it;
- long n;
- system("cls");
- cout << "Enter lower limit of your integral: ";
- cin >> a;
- cout << "Enter upper limit of your integral: ";
- cin >> b;
- cout << "Enter total number of intervals: ";
- cin >> n;
- Trapezoidal T;
- it = T.trapezoidal(a, b, n);
- cin.ignore();
- cin.get();
- return 0;
- }
Add Comment
Please, Sign In to add comment