简体   繁体   English

C++ 积分复数表达式

[英]C++ Complex number expression for integrating

I'm trying to implement Monte-Carlo method to solve an integral and can't figure an approximation for my function.我正在尝试实施蒙特卡洛方法来求解积分,但无法计算出我的 function 的近似值。

double function(double x)
{
    std::complex<double> fz = (pow(-7 * pow(x, 4) - 3 * x + 11, 1.0/3.0) / pow(-2 * pow(x, 6) - 14 * x - 8, 1.0/4.0));
    cout << fz << endl;
    double f = fabs(fz);
    return f;
}

When I substitute 0.25, the approximate result has to be 0.83 - 0.83i (using online calculators) But in my C++ code, it results in 1. What did I do wrong?当我替换 0.25 时,近似结果必须是 0.83 - 0.83i(使用在线计算器)但是在我的 C++ 代码中,它的结果是 1。我做错了什么?

The code for approximation:近似代码:

double integral(unsigned int N)
{
    double sum{}, randomNumber;
    for (unsigned int i = 0; i < N; i++)
    {
        randomNumber = static_cast<double>(rand()) / static_cast<double>(RAND_MAX) / 3;
        sum += function(randomNumber); // estimate function with random value [0; 3]
    }
    return (3 * sum / N);
}

The problem was with std::pow function .问题出在std::pow function 上 To apply it to complex numbers, the first argument of it must be of complex type:要将它应用于复数,它的第一个参数必须是复数类型:

std::complex<double> numerator(-7 * pow(x, 4) - (3 * x) + 11);
std::complex<double> denumerator(-2 * pow(x, 6) - (14 * x) - 8);
std::complex<double> fz = pow(numerator, third) / pow(denumerator, quarter);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM