How to use the rectangle method of of trapezoid method to find the definite integral

  • 2020-04-02 01:25:28
  • OfStack

Analysis:
In high school, we learned that we could use the rectangle method or the rectangle method to find definite integrals.

The idea is to divide the interval into n equal parts, and then approximate the n equal parts as rectangles (or trapezoids), and then sum over the area of all the rectangles (or trapezoids).


< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/2013090209300113.jpg ">

Simple example:
The integral of the function X^2 at PI

Rectangular method:


#include<iostream>
#include<math.h>
using namespace std;
int main(){
 float fun(float x);
 float a,b;
 cout<<" Please enter the function X^2 The lower limit of the definite integral of a And the ceiling b : ";
 cin>>a>>b;
 int n=50;//Divide the interval into 50 parts
 float h=(b-a)/n;//H is the magnitude of each interval
 float s=0;//S is the sum of the area of the rectangle
 float i=0;
 for(i=a;i<b;i+=h){
  s=s+fun(i)*h;
 } 
 cout<<"n As a result, :"<<s<<endl;
 cout<<endl; 
}
float fun(float x){
 return pow(x,2);
}


< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/2013090209300114.jpg ">

Trapezoidal method:

#include<iostream>
#include<math.h>
using namespace std;
int main(){
 float fun(float x);
 float a,b;
 cout<<" Please enter the function X^2 The lower limit of the definite integral of a And the ceiling b : ";
 cin>>a>>b;
 int n=50;//Divide the interval into 50 parts
 float h=(b-a)/n;//H is the magnitude of each interval
 float s=0;//S is the sum of the area of the rectangle
 float i=0;
 for(i=a;i<b;i+=h){
  s=s+((fun(i)+fun(i+h))*h)/2;
 } 
 cout<<"n As a result, :"<<s<<endl;
 cout<<endl; 
}
float fun(float x){
 return pow(x,2);
}


< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/2013090209300215.jpg ">

A more complicated example
Write a general function for sine of x     ,   cosx       ,   E ^ x     ,   X ^ 2   The definite integral
Analysis: fun is a general function used to calculate definite integral. When calling the fun function, it is necessary to pass the upper limit, lower limit of the integral, the number of parts divided by the interval and the pointer to the integrand function.

Rectangular method:


#include<iostream>
#include<math.h>
using namespace std;
int main(){
 float fsin( float x);
 float fcos( float x);
 float   fe( float x);
 float fpf(float x);
 float fun(float a,float b, int n,float (*p)(float x));
 float a[4],b[4],r[4];
 cout<<" Please enter the upper bound of the definite integral of sine function a And the lower limit b : ";
 cin>>a[0]>>b[0];
 r[0]=fun(a[0],b[0],50,fsin);
 cout<<"n The result is: "<<r[0]<<endl;
 cout<<"n Please enter the upper bound of the definite integral of the cosine function a And the lower limit b : ";
 cin>>a[1]>>b[1];
 r[1]=fun(a[1],b[1],50,fcos);
 cout<<"n The result is: "<<r[1]<<endl;
 cout<<"n Please enter to find e Is the upper bound of the integral of the exponential function of the base a And the lower limit b : ";
 cin>>a[2]>>b[2];
 r[2]=fun(a[2],b[2],50,fe);
 cout<<"n The result is: "<<r[2]<<endl;
 cout<<"n Please enter the o X^2 The upper bound of the integral a And the lower limit b : ";
 cin>>a[3]>>b[3];
 r[3]=fun(a[3],b[3],50,fpf);
 cout<<"n The result is: "<<r[3]<<endl; 
 cout<<endl;
 return 0;
}
float fsin(float x){
 return sin(x);
}
float fcos(float x){
 return cos(x);
}
float fe(float x){
 return exp(x);
}
float fpf(float x){
 return pow(x,2);
}
float fun(float a,float b,int n,float (*p)(float x)){
 float i;
 float h=(b-a)/n;
 float s=0;
 for(i=a;i<b;i+=h){
  s=s+p(i)*h;//The formula for finding the area of a rectangle is used
 }
 return s;
}


< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/2013090209300516.jpg ">

Trapezoidal method:

#include<iostream>
#include<math.h>
using namespace std;
int main(){
 float fsin( float x);
 float fcos( float x);
 float   fe( float x);
 float fpf(float x);
 float fun(float a,float b, int n,float (*p)(float x));
 float a[4],b[4],r[4];
 cout<<" Please enter the upper bound of the definite integral of sine function a And the lower limit b : ";
 cin>>a[0]>>b[0];
 r[0]=fun(a[0],b[0],50,fsin);
 cout<<"n The result is: "<<r[0]<<endl;
 cout<<"n Please enter the upper bound of the definite integral of the cosine function a And the lower limit b : ";
 cin>>a[1]>>b[1];
 r[1]=fun(a[1],b[1],50,fcos);
 cout<<"n The result is: "<<r[1]<<endl;
 cout<<"n Please enter to find e Is the upper bound of the integral of the exponential function of the base a And the lower limit b : ";
 cin>>a[2]>>b[2];
 r[2]=fun(a[2],b[2],50,fe);
 cout<<"n The result is: "<<r[2]<<endl;
 cout<<"n Please enter the o X^2 The upper bound of the integral a And the lower limit b : ";
 cin>>a[3]>>b[3];
 r[3]=fun(a[3],b[3],50,fpf);
 cout<<"n The result is: "<<r[3]<<endl; 
 cout<<endl;
 return 0;
}
float fsin(float x){
 return sin(x);
}
float fcos(float x){
 return cos(x);
}
float fe(float x){
 return exp(x);
}
float fpf(float x){
 return pow(x,2);
}
float fun(float a,float b,int n,float (*p)(float x)){
 float i;
 float h=(b-a)/n;
 float s=0;
 for(i=a;i<b;i+=h){
  s=s+((p(i)+p(i+h))*h)/2;//The trapezoid method
 }
 return s;
}


< img Alt = "" border = 0 SRC =" / / files.jb51.net/file_images/article/201309/2013090209300617.jpg ">


Related articles: