简体   繁体   English

在C++中实现一维gsl积分,以参数为积分边界

[英]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,我想使用 gsl 在 C++ 中执行以下一维积分,

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. I(x) = int_{x/4}^1 dy y^{3/2+a} (1-y)^{1/2} exp(1.13*sqrt(log(4y/x))),其中ax被视为积分的常数,我希望能够对用户提供的固定a在区间0.000001<x<0.001中采样的x执行积分。

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?我遇到的问题是如何将 for 循环给出的x值传递给被积函数? 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.目前,代码返回nan因为我没有将x传递给被积函数,而只是传递给下集成边界中的 x 依赖项。 Still a newbie in C++ so apologies in advance for possible simplicity of my question.仍然是 C++ 的新手,因此请提前为我的问题的简单性道歉。

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:如果积分下限(“x/4”表达式)中表示的 x 与参与被积函数公式的 x 相同,则解如下:

#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:对于前五个“x”参数值,我得到以下 output:

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 . . .

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

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