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 namespace std;
double functionIntegral(double a){
return a *a+10;
}
double rectagleIntegral(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(){
cout<<rectagleIntegral(functionIntegral,0,24,20)<<endl;
return 0;
}
Comments
Leave a comment