- #1
Fabio Kopp
- 16
- 0
I am having some problems to use the gsl library. I do the integration via monte carlo and I have obtained the right result, but a need to know how to use this result when I have a dependence on another variable. For example, f(x,y)=x*y, but the integration is only on x. Because I want to evaluate this function on y after the integration.
For the simple case, only one integration without the dependence in another variables, I obtained using this code:
Mod note: Edited to add [ code ] and [ /code ] tags.
To compile the above code, I used:
c++ -Wall -I/usr/local/include -c teste00.cc
c++ -L/usr/local/lib teste00.o -lgsl -lgslcblas -lmI have a function that has the following form
A=x*k[0].
As I said before, I do not know how to implement this dependence in x to be evaluated after the integration on GSL.
Does anybody have any idea how to do this?
For the simple case, only one integration without the dependence in another variables, I obtained using this code:
Mod note: Edited to add [ code ] and [ /code ] tags.
C:
#include <iostream>
#include <gsl/gsl_math.h>
#include <gsl/gsl_monte.h>
#include <gsl/gsl_monte_vegas.h>
#include <cmath>
using namespace std;
double g (double *k, size_t dim, void *params)
{
double A;
A=1*k[0];
return A;}int main(void)
{
double res, err; double xl[10] = { 0.0, 0.0,0.0};
double xu[10] = { 1.0, 1.0,1.0};
const gsl_rng_type *T;
gsl_rng *r;
gsl_monte_function G = { &g, 3, 0 };
gsl_rng_env_setup ();
T = gsl_rng_default;
r = gsl_rng_alloc (T);
{
gsl_monte_vegas_state *s = gsl_monte_vegas_alloc (3);
gsl_monte_vegas_integrate (&G, xl, xu, 3, 10000, r, s,
&res, &err);
do
{
size_t calls = 50000;
gsl_monte_vegas_integrate (&G, xl, xu, 3, calls, r, s,
&res, &err);
}
while (fabs (gsl_monte_vegas_chisq (s) - 1.0) > 0.1);
cout<<"The result is "<< res <<"."<<endl;
gsl_monte_vegas_free (s);
}
gsl_rng_free (r);
return 0;
}
To compile the above code, I used:
c++ -Wall -I/usr/local/include -c teste00.cc
c++ -L/usr/local/lib teste00.o -lgsl -lgslcblas -lmI have a function that has the following form
A=x*k[0].
As I said before, I do not know how to implement this dependence in x to be evaluated after the integration on GSL.
Does anybody have any idea how to do this?
Last edited by a moderator: