The Rectangular Rule A. 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>
#include<cmath>
#include<iomanip>
using namespace std;
double approx(double x)
{
return (6 * pow(x, 4) - 7 * pow(x, 3)+4*x);
}
int main()
{
double a, b;
int n;
cout << "Please, enter lower limit: ";
cin >> a;
cout << "Please, enter higher limit: ";
cin >> b;
cout << "Please, the number of rectangles: ";
cin >> n;
double s = (approx(a) + approx(b)) / 2;
double h = (b - a) / n;
for (int i = 1; i <= n-1; i++)
{
s += approx(a + i*h);
}
double I = h*s;
cout << setprecision(10) << I << endl;
}
Comments
Leave a comment