Search code examples
c++gslnumerical-integration

Implementing one dimensional gsl integration in C++ with parameters as border of integration


I'd like to perform the following one dimensional integration in C++ using gsl,

I(x) = int_{x/4}^1 dy y^{3/2+a} (1-y)^{1/2} exp(1.13*sqrt(log(4y/x))), where a and x are treated as constants of the integration and I would like to be able to perform the integration for x sampled in the interval 0.000001<x<0.001 for a fixed a provided by the user.

Here is my attempt:

 #include <iostream>
 #include <iomanip>
 #include <fstream>
 #include <vector>
 #include <string>
 #include <cmath>
 #include <gsl/gsl_integration.h>
 #include <stdio.h>
 #include <math.h>

double integrand(double y, void * params) {
double a = *(double *) params;
double x = *(double *) params; 

double intg = pow(y,3e0/2e0+a)*pow(1-y,1e0/2e0)*exp(1.13*sqrt(log(4*y/x)));

return intg;

}

double integral(double a) {

gsl_integration_workspace * w
= gsl_integration_workspace_alloc (1000);
gsl_function F;
F.function = &integrand;                 
F.params = &a;
double result, error;

for(int i = 0; i<100; i++) {
    double x = (0.001-0.000001)*i/100 + 0.000001;


gsl_integration_qags (&F, x/4, 1e0, 0, 1e-7, 1000,
                      w, &result, &error);

}
 gsl_integration_workspace_free (w); // Free memory

return result;
}




int main(){
std::cout << "x" << x << "result "<< integral(-0.046)<<std::endl;

 }

The problem I am having is how to pass the value of x given by the for loop to the integrand? At the moment, the code returns nan because I am not passing x to the integrand, only to the x dependence in the lower integration boundary. Still a newbie in C++ so apologies in advance for possible simplicity of my question.


Solution

  • If the x indicated in the lower limit of integration ("x/4" expression) is the same as the x participating in the formula of the integrand, then the solution is as follows:

    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <vector>
    #include <string>
    #include <cmath>
    #include <gsl/gsl_integration.h>
    #include <stdio.h>
    #include <math.h>
    
    //create a structure for the parameters of the integral function: 
    
    struct params 
    {
        double a;
        double x;
    };
    
    //create an integration function for the x variable: 
    //let all INTERNAL parameters have a prefix "_" in the name:   
    
    double integrand(double y, void * params_from_above)
    {
        //declare our structure for parameters and be sure to do type casting, 
        //and initialize the parameter values passed from above:
        params& _inner_params = *(params*)params_from_above;
        
        //we calculate the value of the integrand using the passed parameter values:
        double _intg = pow(y, 1.5 + _inner_params.a) * pow(1.0 - y, 0.5) * exp(1.13 * sqrt(log(4.0 * y/_inner_params.x)));
    
        return _intg;
    }
    
    
    double integral(double a)
    {
        double result, error;
        
        //declare a structure for storing and passing "a" and "x" parameters:
        params custom_params;
        //create integration workspace:
        gsl_integration_workspace * w = gsl_integration_workspace_alloc (1000);
        
        //declare and initialize gsl callback function for integrand:
        gsl_function F;
        F.function = &integrand;
        
        //declare and initialize gsl params structure:
        //(so far only for the value of "a" parameter)
        F.params = &custom_params;    
        custom_params.a = a;
    
        //start the main cycle of calculations:    
        for(int i = 0; i < 100; i++)
        {
            //compute x parameter value:
            double _x = (0.001-0.000001)*i/100 + 0.000001;
            //save the value of the X parameter for sending to the integrand:
            custom_params.x = _x;
            
            //compute the integral for current "x"/4 parameter value:
            gsl_integration_qags (&F, _x/4.0, 1.0, 0, 1e-7, 1000, w, &result, &error);
            
            //output integral value:
            printf("res = %f\n", result);
            //delay: press any key for continue: 
            system("pause");
    
        }
        gsl_integration_workspace_free (w); // Free memory
    
        return result;
    }
    
    int main()
    {
        //here you are mistaken, since only the last value is displayed...
        //but I left your expression for the sake of calling an integral function:
        std::cout << "result "<< integral(-0.046) << std::endl; 
    } 
    

    And I get the following output for the first five "x" parameter values:

    C:\Users\...\CLionProjects\gsl_test\cmake-build-debug\gsl_test.exe
    res = 15.226887
    Press any key to continue . . .
    
    res = 10.523077
    Press any key to continue . . .
    
    res = 9.467232
    Press any key to continue . . .
    
    res = 8.870463
    Press any key to continue . . .
    
    res = 8.459477
    Press any key to continue . . .