Analysis:
In high school, we learned that we can use the rectangle method or the rectangle method to determine the points.
The idea is to divide the integral range into n equal parts, then regard the n equal parts as a rectangle (or trapezoid), and then sum the area of all the rectangles (or trapezoid.
Simple Example:
Calculates the number of points in the X ^ 2 function.
Rectangle method:
[Cpp]
# Include <iostream>
# Include <math. h>
Using namespace std;
Int main (){
Float fun (float x );
Float a, B;
Cout <"Enter the lower limit a and upper limit B of the fixed points of Function X ^ 2 :";
Cin> a> B;
Int n = 50; // divide the interval into 50 parts
Float h = (B-a)/n; // h indicates the split size of each interval.
Float s = 0; // s is the sum of the rectangle's Area
Float I = 0;
For (I = a; I <B; I + = h ){
S = s + fun (I) * h;
}
Cout <"\ n:" <s <endl;
Cout <endl;
}
Float fun (float x ){
Return pow (x, 2 );
}
# Include <iostream>
# Include <math. h>
Using namespace std;
Int main (){
Float fun (float x );
Float a, B;
Cout <"Enter the lower limit a and upper limit B of the fixed points of Function X ^ 2 :";
Cin> a> B;
Int n = 50; // divide the interval into 50 parts
Float h = (B-a)/n; // h indicates the split size of each interval.
Float s = 0; // s is the sum of the rectangle's Area
Float I = 0;
For (I = a; I <B; I + = h ){
S = s + fun (I) * h;
}
Cout <"\ n:" <s <endl;
Cout <endl;
}
Float fun (float x ){
Return pow (x, 2 );
}
Trapezoid method:
[Cpp]
# Include <iostream>
# Include <math. h>
Using namespace std;
Int main (){
Float fun (float x );
Float a, B;
Cout <"Enter the lower limit a and upper limit B of the fixed points of Function X ^ 2 :";
Cin> a> B;
Int n = 50; // divide the interval into 50 parts
Float h = (B-a)/n; // h indicates the split size of each interval.
Float s = 0; // s is the sum of the rectangle's Area
Float I = 0;
For (I = a; I <B; I + = h ){
S = s + (fun (I) + fun (I + h) * h)/2;
}
Cout <"\ n:" <s <endl;
Cout <endl;
}
Float fun (float x ){
Return pow (x, 2 );
}
# Include <iostream>
# Include <math. h>
Using namespace std;
Int main (){
Float fun (float x );
Float a, B;
Cout <"Enter the lower limit a and upper limit B of the fixed points of Function X ^ 2 :";
Cin> a> B;
Int n = 50; // divide the interval into 50 parts
Float h = (B-a)/n; // h indicates the split size of each interval.
Float s = 0; // s is the sum of the rectangle's Area
Float I = 0;
For (I = a; I <B; I + = h ){
S = s + (fun (I) + fun (I + h) * h)/2;
}
Cout <"\ n:" <s <endl;
Cout <endl;
}
Float fun (float x ){
Return pow (x, 2 );
}
A complicated example
Write a universal function to calculate the definite points of sinx, cosx, e ^ x, and x ^ 2.
Analysis: fun is a universal function used to determine points. when calling the fun function, you need to pass the upper limit, lower limit, number of parts in the interval, and the pointer of the product function.
Rectangle method:
[Cpp]
# 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 <"Enter the upper limit a and lower limit B of the sine function's definite integral :";
Cin> a [0]> B [0];
R [0] = fun (a [0], B [0], 50, fsin );
Cout <"\ n:" <r [0] <endl;
Cout <"\ n enter the upper limit a and lower limit B of the cosine function to determine the integral :";
Cin> a [1]> B [1];
R [1] = fun (a [1], B [1], 50, fcos );
Cout <"\ n:" <r [1] <endl;
For cout <"\ n, enter the upper limit a and lower limit B of the e-based exponent function :";
Cin> a [2]> B [2];
R [2] = fun (a [2], B [2], 50, fe );
Cout <"\ n:" <r [2] <endl;
For cout <"\ n, enter the upper limit a and lower limit B of the X ^ 2 function to set the integral :";
Cin> a [3]> B [3];
R [3] = fun (a [3], B [3], 50, fpf );
Cout <"\ n:" <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; // calculates the area using the rectangle.
}
Return s;
}
# 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 <"Enter the upper limit a and lower limit B of the sine function's definite integral :";
Cin> a [0]> B [0];
R [0] = fun (a [0], B [0], 50, fsin );
Cout <"\ n:" <r [0] <endl;
Cout <"\ n enter the upper limit a and lower limit B of the cosine function to determine the integral :";
Cin> a [1]> B [1];
R [1] = fun (a [1], B [1], 50, fcos );
Cout <"\ n:" <r [1] <endl;
For cout <"\ n, enter the upper limit a and lower limit B of the e-based exponent function :";
Cin> a [2]> B [2];
R [2] = fun (a [2], B [2], 50, fe );
Cout <"\ n:" <r [2] <endl;
For cout <"\ n, enter the upper limit a and lower limit B of the X ^ 2 function to set the integral :";
Cin> a [3]> B [3];
R [3] = fun (a [3], B [3], 50, fpf );
Cout <"\ n:" <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; // calculates the area using the rectangle.
}
Return s;
}
Trapezoid method:
[Cpp]
# 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 <"Enter the upper limit a and lower limit B of the sine function's definite integral :";
Cin> a [0]> B [0];
R [0] = fun (a [0], B [0], 50, fsin );
Cout <"\ n:" <r [0] <endl;
Cout <"\ n enter the upper limit a and lower limit B of the cosine function to determine the integral :";
Cin> a [1]> B [1];
R [1] = fun (a [1], B [1], 50, fcos );
Cout <"\ n:" <r [1] <endl;
For cout <"\ n, enter the upper limit a and lower limit B of the e-based exponent function :";
Cin> a [2]> B [2];
R [2] = fun (a [2], B [2], 50, fe );
Cout <"\ n:" <r [2] <endl;
For cout <"\ n, enter the upper limit a and lower limit B of the X ^ 2 function to set the integral :";
Cin> a [3]> B [3];
R [3] = fun (a [3], B [3], 50, fpf );
Cout <"\ n:" <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; // obtain the area using the trapezoid Method
}
Return s;
}
# 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 <"Enter the upper limit a and lower limit B of the sine function's definite integral :";
Cin> a [0]> B [0];
R [0] = fun (a [0], B [0], 50, fsin );
Cout <"\ n:" <r [0] <endl;
Cout <"\ n enter the upper limit a and lower limit B of the cosine function to determine the integral :";
Cin> a [1]> B [1];
R [1] = fun (a [1], B [1], 50, fcos );
Cout <"\ n:" <r [1] <endl;
For cout <"\ n, enter the upper limit a and lower limit B of the e-based exponent function :";
Cin> a [2]> B [2];
R [2] = fun (a [2], B [2], 50, fe );
Cout <"\ n:" <r [2] <endl;
For cout <"\ n, enter the upper limit a and lower limit B of the X ^ 2 function to set the integral :";
Cin> a [3]> B [3];
R [3] = fun (a [3], B [3], 50, fpf );
Cout <"\ n:" <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; // obtain the area using the trapezoid Method
}
Return s;
}