Numerical integration in C++ -
hello need integrate function (of 2 variables). know can using fubini theorem integrate 1 variable functions using numerical methods such rectangle method or trapezoidal rule.
but there pre-built functions in c++ ? need integrate on the unit r2
triangle ((0,0), (1,0), (0,1))
.
you can use gnu scientific library, supports many "numerical analysis" functions including integration.
a simple example of integration manual few lines of code:
#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; }
Comments
Post a Comment