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. The approximation on the RHS becomes more accurate as more rectangles are used. In fact, You are required to: v. write pseudocode algorithm to determine the integral of a function between two specified points using the rectangular rule. vi. write C++ computer programs to determine the integral of a function between two specified points using the rectangular rule
#include <iostream>
using namespace std;
double f(double x)
{
return (6*x*x*x*x-7*x*x*x+4*x);
}
int main()
{
double a,b;
int n;
cout << "Enter lower limit: ";
cin >> a;
cout << "Enter higher limit: ";
cin >> b;
cout << "Enter the number of rectangles: ";
cin >> n;
double s=(f(a)+f(b))/2;
double h=(b-a)/n;
for (int i=1; i<=n-1; i++)
{
s+=f(a+i*h);
}
double I=h*s;
cout<<setprecision(10)<<I<<endl;
return 0;
}
Comments
Leave a comment