c++ - C++ 中的数值积分

标签 c++ integration numerical

我需要整合一个(两个变量的)函数。 我知道我可以通过使用 Fubini 定理 对一个变量函数求积分,然后使用诸如矩形法梯形法则 之类的数值方法来做到这一点.

但是在 C++ 中是否有任何预建函数可以做到这一点?我需要对单元 R2 三角形 ((0,0), (1,0), (0,1)) 进行积分。

最佳答案

您可以使用 GNU Scientific Library , 它支持包括集成在内的许多“数值分析”功能。

一个非常简单的example of integration手册中只有几行代码:

#include <stdio.h>
#include <math.h>
#include <gsl/gsl_integration.h>

double f (double x, void * params) {
  double alpha = *(double *) params;
  return log(alpha*x) / sqrt(x);
}

int
main (void)
{
  double result, error;
  double expected = -4.0;
  double alpha = 1.0;
  gsl_integration_workspace * w 
    = gsl_integration_workspace_alloc (1000);

  gsl_function F;
  F.function = &f;
  F.params = &alpha;

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

  printf ("result          = % .18f\n", result);
  printf ("exact result    = % .18f\n", expected);
  printf ("estimated error = % .18f\n", error);
  printf ("actual error    = % .18f\n", result - expected);
  printf ("intervals =  %d\n", w->size);

  gsl_integration_workspace_free (w);

  return 0;
}

关于c++ - C++ 中的数值积分,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16512817/

相关文章:

c++ - 我需要帮助来理解这个嵌套的问题 if else

c++ - QT HTML5 带有菜单栏,就像真实的程序一样

python - scipy 与 matlab 中的 dblquad 给出不同的结果

java - jms集成开发的单元测试

save - Jenkins:如何保存构建的变更日志

c++ - 仅在 C++ 中对一个变量进行两个变量函数的数值积分(使用 Numerical Recipes 库)

c++ - Xcode 错误 "No matching function for call to ' max'"

c++ - 使用用户定义的类在 Boost 序列化中出错

xslt - xsl :sort: sorting by numeric value

java - 在Java中,如何在没有数组的情况下以数字方式将数字分配给字符串中的字符?