How to find the definite integral _c language with rectangular method (Trapezoid method)

Source: Internet
Author: User
Tags cos pow sin

Analysis:
In high school, we have learned that we can use rectangular method or rectangular method to find the definite integral.

The idea is to divide the integral interval into n equal parts, then approximate the n equal equal to the rectangle (or trapezoid), and then sum the area of all the rectangles (or trapezoidal).


A simple example:
The definite integral of the function x^2

Rectangular method:

Copy Code code as follows:

#include <iostream>
#include <math.h>
using namespace Std;
int main () {
Float Fun (float x);
float a,b;
cout<< "Please enter the lower bound of the definite integral of the function x^2 A and the upper limit B:";
cin>>a>>b;
int n=50;//divides the interval into 50 parts
Float h= (b-a)/n;//h is the size of each interval
Float s=0;//s is the area of the rectangle and
float i=0;
for (i=a;i<b;i+=h) {
S=s+fun (i) *h;
}
cout<< "\ r \ n result is:" <<s<<endl;
cout<<endl;
}
Float Fun (float x) {
Return pow (x,2);
}




trapezoidal Method:
Copy Code code as follows:

#include <iostream>
#include <math.h>
using namespace Std;
int main () {
Float Fun (float x);
float a,b;
cout<< "Please enter the lower bound of the definite integral of the function x^2 A and the upper limit B:";
cin>>a>>b;
int n=50;//divides the interval into 50 parts
Float h= (b-a)/n;//h is the size of each interval
Float s=0;//s is the area of the rectangle and
float i=0;
for (i=a;i<b;i+=h) {
S=s+ ((Fun (i) +fun (i+h)) *h)/2;
}
cout<< "\ r \ n result is:" <<s<<endl;
cout<<endl;
}
Float Fun (float x) {
Return pow (x,2);
}




a more complicated example
Write a common function to find the definite integral of sinx, COSX, E^x, x^2
Analysis: Fun for the general function of the definite integral, when calling the fun function, we need to pass the upper limit of integral, the lower limit, the number of parts of interval and the pointer of Integrand.

Rectangular method:

Copy Code code as follows:

#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 limit A and lower B:" of the sine function definite integral;
cin>>a[0]>>b[0];
R[0]=fun (A[0],b[0],50,fsin);
cout<< "\ r \ n result is:" <<r[0]<<endl;
cout<< "\ n Please enter the upper limit A and lower limit B:" of the cosine function definite integral;
cin>>a[1]>>b[1];
R[1]=fun (A[1],b[1],50,fcos);
cout<< "\ r \ n result is:" <<r[1]<<endl;
cout<< "\ n Please enter the upper bound of a and lower bound B:" For the exponential function with E as the base. "
cin>>a[2]>>b[2];
R[2]=fun (A[2],B[2],50,FE);
cout<< "\ r \ n result is:" <<r[2]<<endl;
cout<< "\ n Please enter the upper limit A and lower bound B:" For the x^2 function definite integral;
cin>>a[3]>>b[3];
R[3]=fun (A[3],B[3],50,FPF);
cout<< "\ r \ n 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;//uses the formula of rectangular area
}
return s;
}




trapezoidal Method:
Copy Code code as follows:

#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 limit A and lower B:" of the sine function definite integral;
cin>>a[0]>>b[0];
R[0]=fun (A[0],b[0],50,fsin);
cout<< "\ r \ n result is:" <<r[0]<<endl;
cout<< "\ n Please enter the upper limit A and lower limit B:" of the cosine function definite integral;
cin>>a[1]>>b[1];
R[1]=fun (A[1],b[1],50,fcos);
cout<< "\ r \ n result is:" <<r[1]<<endl;
cout<< "\ n Please enter the upper bound of a and lower bound B:" For the exponential function with E as the base. "
cin>>a[2]>>b[2];
R[2]=fun (A[2],B[2],50,FE);
cout<< "\ r \ n result is:" <<r[2]<<endl;
cout<< "\ n Please enter the upper limit A and lower bound B:" For the x^2 function definite integral;
cin>>a[3]>>b[3];
R[3]=fun (A[3],B[3],50,FPF);
cout<< "\ r \ n 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;//trapezoid method to find the area
}
return s;
}


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.