【问题标题】:gsl multivariable numerical integrationgsl 多变量数值积分
【发布时间】:2018-12-24 05:18:07
【问题描述】:

我一直在使用 GSL 集成包 (gsl_integration.h) 来尝试在某个限制 [a,b] 之间相对于 x 集成一些多变量函数 f(x,y,z)。在这个例子中,我有一个玩具模型:f(x,y,z) = xyz

我想用一个函数输出这个积分的结果

double integral(double a, double b, double y, double z){}

在此之前我可以保持 y 和 z 任意。到目前为止,我的尝试主要涉及在某些预定义函数中将 y、z 设置为常数,然后使用

gsl_integration_qags

函数来集成该函数。但是,我想保持 y 和 z 的值是任意的,直到我在上面的函数中定义它们。到目前为止我得到的最接近的是如下

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

#define PI 3.1415926535897

double integrand(double x, void * params){
  double y = *(double *) params;
  double z = *(double *) params;            // PROBLEM: this just sets z = y
  double tmp = x*z*y;
  return tmp;
}

double integral(double a, double b, double y, double z){
  gsl_integration_workspace * w
    = gsl_integration_workspace_alloc (1000);
  gsl_function F;
  F.function = &integrand;               // Set integrand
  F.params = &y, &z;             // Set the parameters you wish to treat as constant in the integration
  double result, error;
  gsl_integration_qags (&F, a, b, 0, 1e-7, 1000,
                        w, &result, &error);
  gsl_integration_workspace_free (w); // Free the memory
  return result;
}




int main(){
std::cout << "Result "<< integral(0.0,1.0,3.0,5.0)<<std::endl;

}

这给出了一个输出

Result 4.5

代码设置 a = 0.0, b = 1.0, y = z = 3.0 -- 我想要 a = 0.0, b = 1.0, y = 3.0, z = 5.0,结果为 7.5。

如果可能的话,我想坚持 GSL 集成而不是提升。我也咨询过https://www.gnu.org/software/gsl/doc/html/integration.html,但我一点也不聪明。任何建议将不胜感激,谢谢。

【问题讨论】:

  • 能否将答案标记为正确?

标签: c++ integration gsl


【解决方案1】:

我在猜测,但在我看来你想要

double params[] = { y, z };
F.params = params;

double y = ((double *)params)[0];
double z = ((double *)params)[1];

【讨论】:

  • 非常感谢!我已经为此苦苦挣扎了一段时间。您的解决方案解决了问题。
猜你喜欢
  • 1970-01-01
  • 2016-11-05
  • 1970-01-01
  • 2016-12-16
  • 2020-11-08
  • 2022-06-18
  • 1970-01-01
  • 2021-11-08
  • 1970-01-01
相关资源
最近更新 更多