I'm trying to implement Monte-Carlo method to solve an integral and can't figure an approximation for my 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?
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. 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);