Answer to Question #196487 in C++ for Jaydon Martin

Question #196487

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-21T05:15:28-0400
#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;
}

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

LATEST TUTORIALS
New on Blog