簡體   English   中英

在C++中實現一維gsl積分,以參數為積分邊界

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

我想使用 gsl 在 C++ 中執行以下一維積分,

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執行積分。

這是我的嘗試:

 #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;

 }

我遇到的問題是如何將 for 循環給出的x值傳遞給被積函數? 目前,代碼返回nan因為我沒有將x傳遞給被積函數,而只是傳遞給下集成邊界中的 x 依賴項。 仍然是 C++ 的新手,因此請提前為我的問題的簡單性道歉。

如果積分下限(“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; 
} 

對於前五個“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