0

I need to evaluate a double integral where the inner upper Bound is variable:
integral2 between -5 and 5 ( integral1 between 0 and y f(x)dx )dy.

I'm stuck in the calculation of the outer loop which is dependent on the inner loop. My code runs for a really long time but returns zero.

How can i calculate a integral with variable limits?


First I created a function doubleIntegrate. In the first place the function holds the arrays with coefficients for the trapeziodal rule.

double NumericIntegrationDouble::doubleIntegrate(double (*doubleFunc 
(const double &x), double dy, const double &innerLowBound, const double 
&outerLowBound) 
{

double innerValue = 0.0;
double outerValue = 0.0;

// arrays which store function values for the inner (X) and the outer (Y) integration loop 

// vector filled with coefficients for the inner poop (trapezoidal rule)
std::vector<double> vecCoeffsX(numberOfIntervalsDouble+1, 2);
vecCoeffsX[0] = 1;                         // fist coeff = 1
vecCoeffsX[vecCoeffsX.size()-1] = 1;       // last coeff = 1
std::vector<double> funcValuesX(numberOfIntervalsDouble+1); 


// vector filled with coefficients for the inner poop (trapezoidal rule)
std::vector<double> vecCoeffsY(numberOfIntervalsDouble+1, 2);
vecCoeffsY[0] = 1;                        // same as above
vecCoeffsY[vecCoeffsY.size()-1] = 1;      // same as above
std::vector<double> funcValuesY(numberOfIntervalsDouble+1)


// Then i created a loop in a loop where dy and dy stands for step size of integration. The variables xi and yi stand for the current x and y value. 

// outer integration loop dy
for(int i=0; i<=numberOfIntervalsDouble; i++)
{
    double yi = outerLowBound + dy*i;
    funcValuesY[i] = (*doubleFunc)(yi);

    // inner integration loop dx
    for(int j=0; j<=numberOfIntervalsDouble; j++)
    {
        double dx = abs(yi - innerLowBound) / (double)numberOfIntervalsDouble;
        double xi = innerLowBound + j*dx;
        funcValuesX[j] = (*doubleFunc)(xi);
        double multValueX = std::inner_product(vecCoeffsX.begin(), vecCoeffsX.end(), funcValuesX.begin(), 0.0);
        double innerValue = 0.5 * dx * multValueX;
        suminnerValue = suminnerValue + innerValue;
    }
    //auto multValueY = std::inner_product(vecCoeffsY.begin(), vecCoeffsY.end(), funcValuesY.begin(), 0.0);

    outerValue = 0.5 * dy * suminnerValue;
}
return outerValue;
}
  • 1
    So, what's the problem you are having? – Ron Mar 20 '18 at 12:31
  • Is your inner integral defined between 0^y and 1, while the outer one is between -5^5 and 2? If that is the case, for every negative y value, you are trying to calculate an integral between inifinity and 1 using the trapezoidal method and constant steps. No doubt that your code runs for a long time. – Bob__ Mar 20 '18 at 14:47
  • @Bob__ No. It was just bad spelling. Sorry. The limits are between " ^ ". So i integrate the first integral between 0 and y and the second between -5 and 5. I don't see any math or logical failure in my code. –  Mar 21 '18 at 09:33
  • @Ron my code is running in to an endless compiling process. So it must be a loop failure i think. But i don't see any logical issues. –  Mar 21 '18 at 09:38
  • 1
    @Fuchs_derZweite Please [edit](https://stackoverflow.com/posts/49384128/edit) your post to include that info. – Ron Mar 21 '18 at 09:40

0 Answers0