Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstdlib>
- using namespace std;
- class TrapezoidalRule{
- double *y;
- int n;
- double h, a, b;
- public:
- TrapezoidalRule(double, double, int);
- ~TrapezoidalRule();
- double f(double);
- double calcSolution();
- void displaySolution();
- };
- TrapezoidalRule::TrapezoidalRule(double a, double b, int n){
- TrapezoidalRule::a = a;
- TrapezoidalRule::b = b;
- TrapezoidalRule::n = n;
- y = new double[n + 1];
- h = (a - b) / n;
- }
- TrapezoidalRule::~TrapezoidalRule(){
- delete y;
- }
- double TrapezoidalRule::f(double x){
- return 4.0 / (1 + (x * x));
- }
- double TrapezoidalRule::calcSolution(){
- // Generate all value of y0 to yn
- for(int x = 0; x <= n; x++){
- y[x] = f(x);
- }
- // Calculating the value of I
- double i = 0;
- double sum = 0;
- for(int i = 1; i < n; i++){
- sum += y[i];
- }
- sum *= 2;
- sum += (y[0] + y[n]);
- i = (1.0/2.0) * h * sum;
- return i;
- }
- void TrapezoidalRule::displaySolution(){
- cout << "I = " << calcSolution();
- }
- int main(int argc, char** argv) {
- system("cls");
- TrapezoidalRule obj(1, 0, 10);
- obj.displaySolution();
- cin.get();
- return 0;
- }
Add Comment
Please, Sign In to add comment