One: Analysis:
When I was studying for a freshman, we learned that we could use the rectangle method to find the integral. The idea is to divide the integral interval into n equal parts, then approximate the n equal parts as a rectangle (or trapezoid), and then sum the area of all rectangles (or trapezoidal).
Two: A simple example
The definite integral of finding function x^2
Rectangle Method:
#include <iostream>#include<math.h>using namespacestd;intMain () {floatFunfloatx); floatA, B; cout<<"Please enter the lower bound of the definite integral of the function x^2 A and upper bound B:"; CIN>> a >>b; intn = -;//divide the interval into 50 parts floath = (b-a)/n;//h is the size of each interval floats =0;//S is the area of the rectangle and floati =0; for(i = A; I < b; i + =h) {s= s + Fun (i) *h; } cout<<"\ n The result is:"<< s <<Endl; cout<<Endl;}
floatFunfloatx) { returnPOW (x,2);}Three: Using C language to achieve the following three functions of the definite integral solution
#define_crt_secure_no_warnings#include<stdio.h>#include<stdlib.h>#include<math.h>//a general function of using rectangle method to find definite integral//p is the function pointer, a is the lower bound, B is the upper bound, N is the equal numberfloatIntegralfloat(*p) (float),floatAfloatBintN) { inti; floatArea=0; floatew = (b-a)/N; for(i =1; I <= n;i++) Area+ = (*p) (A + i*ew) *ew; returnArea ;}floatF_sin (floatx) { returnsin (x);}floatF_cos (floatx) { returncos (x);}floatF_exp (floatx) { returnexp (x);}intMain () {floatA, B,area; float(*p) (float); intn = -; printf ("Test Sin,input A, B:"); scanf ("%f,%f", &a, &b); P=F_sin; Area=Integral (P, a, B, N); printf ("Get value:%f\n", area); printf ("Test Cos,input A, B:"); scanf ("%f,%f", &a, &b); P=F_cos; Area=Integral (P, a, B, N); printf ("Get value:%f\n", area); printf ("Test Exp,input A, B:"); scanf ("%f,%f", &a, &b); P=F_exp; Area=Integral (P, a, B, N); printf ("Get value:%f\n", area); System ("Pause"); return 0;}
C Language Review---Rectangular method for determining integral function