C++ C+中用梯形规则积分复数+; #包括 #包括 使用名称空间std; 复数积分(复数(*f)(复数常量和x)、复数l、复数u、大小n) { 复步长=(u-l)/(双)n; 复杂区域(0,0); 对于(大小i=0;i

C++ C+中用梯形规则积分复数+; #包括 #包括 使用名称空间std; 复数积分(复数(*f)(复数常量和x)、复数l、复数u、大小n) { 复步长=(u-l)/(双)n; 复杂区域(0,0); 对于(大小i=0;i,c++,numerical-integration,C++,Numerical Integration,正如所评论的那样,除以内部是一个无关的步骤 #include <complex> #include <iostream> using namespace std; complex<double> integral(complex<double> (*f)(complex<double> const &x), complex<double> l, complex<double> u, size_t n)

正如所评论的那样,除以
内部
是一个无关的步骤

#include <complex>
#include <iostream>
using namespace std;

complex<double> integral(complex<double> (*f)(complex<double> const &x), complex<double> l, complex<double> u, size_t n) 
{
    complex<double> step = (u - l) / (double) n;
    complex<double> area(0, 0);
    for (size_t i = 0; i < n; i++) {
        complex<double> inner = l + (i + 0.5) * step;
        area = area + f(inner) / inner * step;
    }
    return area;
}
int main() 
{
    complex<double> l(0, 0);
    complex<double> u(2, 1);
    cout << integral(cos, l, u, 100);
}
复数积分(复数(*f)(复数常量和x)、复数l、复数u、大小n)
{
复步长=(u-l)/(双)n;
复杂区域(0,0);
对于(大小i=0;i

为什么要使用
/inner
?如果我使用area=area+f(inner)*步骤;它在工作,@taiwan12它不是梯形规则,而是矩形规则
complex<double> integral(complex<double> (*f)(complex<double> const &x), complex<double> l, complex<double> u, size_t n) 
{
    complex<double> step = (u - l) / (double) n;
    complex<double> area(0, 0);
    for (size_t i = 0; i < n; i++) {
        complex<double> inner = l + (i + 0.5) * step;
        area += f(inner) * step;
    }
    return area;
}