Answer to Question #191251 in C++ for jay

Question #191251

The rectangular rule (also called the midpoint rule) is perhaps the simplest of the three methods for estimating an integral. i. Integrate over an interval a ≤ x ≤ b. ii. Divide this interval up into n equal subintervals of length h = (b − a)/n. iii.Approximate f in each subinterval by f(x*j ), where x*j is the midpoint of the subinterval. iv. Area of each rectangle: f(x*j)h, f(x*j)h,. . . , f(x*n)h.


1
Expert's answer
2021-05-10T08:26:54-0400
#include <iostream>
using std::cout;
using std::endl;


// integrable function formula
double fun_integral (double x) 
{
    return x *x+10;
}
// Function Integration
double rect_integral(double (*f)(double),double a, double b,double n)
{
    if (a == b) 
    {
        return 0;
    }
    if (n<0 || b<a)
    {
        cout << "Invalid argument " << endl;
        return -1;
    }
    double result{ 0 };
    double h = (b - a) / n;
    for(double i = a+h/2;i<b;i =i+ h)
    {
        result += f(i) * h;
    }
    return result;
}
int main()
{
    //example of how the function works
    cout<<rect_integral(fun_integral, 0,12, 10)<<endl;
    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment