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.
#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;
}
Comments
Leave a comment