C++ programming (rectangular method for definite integral)

General Functions of Definite Integral by Rectangular Method

1. Principle

The idea is to divide the integral interval into n equal parts, and then treat the n equal parts approximately as rectangles (or trapezoids), and then for all rectangles

(or trapezoid) to sum the area.

2. Method

It can be seen that the function that needs to find the definite integral is different each time. You can write a general function integral for definite integral ,

It has 3 formal parameters: the lower limit a, the upper limit b, and the pointer variable fun pointing to the function. The function prototype can be written as:

    float integral (float a, float b,float (*fun)());

Call the integral function 5 times successively, each time a, b and one of sin, cos, exp are used as actual parameters, and the above

The limit, the lower limit and the entry address of the relevant function are passed to the formal parameter fun . Find the definite integral in the process of executing the integral function

value. According to the above ideas, write a program.

3. Code implementation

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
    
    
	float integral(float (*p)(float),float a,float b,int n);
	float fsin(float x);
	float fcos(float x);
	float fexp(float x);
	float a1,b1,a2,b2,a3,b3,result,(*p)(float);
	int n=1000;             //这里将定积分以x轴划分了1000份(划分的越多,所得的值越精确)
	cout<<"intput the upper and lower limit of sin(x) :"<<' ';
	cin>>a1>>b1;
	cout<<"intput the limit of cos(x) :"<<' ';
	cin>>a2>>b2;
	cout<<"intput the limit of exp(x) :"<<' ';
	cin>>a3>>b3;
	p=fsin;                 //使p指针指向fsin函数,便于后续函数的调用 
	result=integral(p,a1,b1,n);
	cout<<"The integral of sin(x) is:"<<result<<endl;
	p=fcos;
	result=integral(p,a2,b2,n);
	cout<<"The integral of cos(x) is:"<<result<<endl;
	p=fexp;
	result=integral(p,a3,b3,n);
	cout<<"The integral of exp(x) is:"<<result<<endl;
	return 0;
	
 } 
 float integral(float(*p)(float),float a,float b,int n)    //用矩形法求定积分的通用函数 
{
    
    
   float x,d,s=0;
   if(a<b) {
    
     int k;               //确保上下限的正确性 
   	k=a;a=b;b=k;          
   }
   d=(a-b)/n;   
   x=b;
   for(int i=0;i<n;i++)
   {
    
    
   	x=x+d;           
   	s=s+(*p)(x)*d;                //将求出来的每个矩形面积累加即可 
   }
   return (s); 
}
 float fsin(float x)
    {
    
     return sin(x); }
 float fcos(float x)
    {
    
     return cos(x); }
 float fexp(float x)
    {
    
     return exp(x); }
 

4. Description

sin , cos and exp are mathematical functions provided by the system, define three functions fsin , fcos and

fexp is used to calculate the values ​​of sin(x), cos(x) and exp(x) respectively, and these three functions should be declared in the main function. in main

In the function, p is defined as a pointer variable pointing to the function, and the definition form is "float(*p)(float)", which means that the function pointed to by p has a real type

parameter, p can point to a function whose return value is real.

There is "p=fsin;" in the main function , which means that the entry address of the fsin function is assigned to p, and when calling the integral function , use p as

As an actual parameter, pass the entry address of the fsin function to the formal parameter p (the formal parameter p is also defined as a pointer variable pointing to the function), so that the formal parameter p also refers to

For the fsin function, (*p)(x) is equivalent to fsin(x). The value of fsin(x) is the value of sin(x). Therefore, by calling the integral function to find

Find the definite integral of sin(x). The same is true for the definite integrals of the remaining two functions.

Guess you like

Origin blog.csdn.net/Terminal_ve/article/details/125965632