我需要整合一个(两个变量的)函数。 我知道我可以通过使用 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 = α
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/