- #1
CAF123
Gold Member
- 2,948
- 88
I'm trying to pass through some parameters of a function to the gsl integration routine but my code is currently not returning correct values. I attach a version of my code using dummy example functions and names.
I believe the issue is with the myStruct.a parameter in ``func and somehow it is not being passed through the integration routine properly. The code does not return an error but yields values ~10^(129) suggesting something is not quite right. I have tested this by simply hardcoding ten values of a one at a time in func instead of using a for loop and seeing if the code returns sensible results which it does so. Do you see immediately any error in what I have written? Thanks in advance!
C:
struct myStruct_t {
double a;
};
double func(double z, void* params) {
myStruct_t &myStruct = *(myStruct_t *)(params);
double res = pow(z*myStruct.a,2);
return res;
}
int main() {
double result, error;
myStruct_t myStruct;
gsl_integration_workspace * w = gsl_integration_workspace_alloc(1000);
gsl_function F;
F.params = (void*)(&myStruct);
F.function = func;
for(int i = 0; i < 10; i++) {
double b = pow(2,i);
double a = 2*b;
myStruct.a = a;
gsl_integration_qags(&F, 0, 1, 1e-3, 1e-3, 1000, w, &result, &error);
std::cout << b << result << std::endl;
}
}
Last edited by a moderator: