forum.alglib.net http://forum.alglib.net/ |
|
integrate function with more than one parameter http://forum.alglib.net/viewtopic.php?f=2&t=424 |
Page 1 of 1 |
Author: | ygasawa [ Mon Aug 29, 2011 4:28 pm ] |
Post subject: | integrate function with more than one parameter |
Hi, I would like to integrate a function f(x,y) with respect to x. In the examples, I didn't find something like that. For example, how can I modify the D1 example to integrate exp(x*z) instead, where z is an additional parameter to the function int_function_1_func ? (see below) I know this should be a trivial question, but any help would be welcome, as I'm not familiar with alglib or C++. Many thanks in advance. void int_function_1_func(double x, double xminusa, double bminusx, double &y, void *ptr) { // this callback calculates f(x)=exp(x) y = exp(x); } int main(int argc, char **argv) { // // This example demonstrates integration of f=exp(x) on [0,1]: // * first, autogkstate is initialized // * then we call integration function // * and finally we obtain results with autogkresults() call // double a = 0; double b = 1; autogkstate s; double v; autogkreport rep; autogksmooth(a, b, s); alglib::autogkintegrate(s, int_function_1_func); autogkresults(s, v, rep); printf("%.2f\n", double(v)); // EXPECTED: 1.7182 return 0; } |
Author: | phi_rus [ Mon May 14, 2012 11:26 am ] |
Post subject: | Re: integrate function with more than one parameter |
Hey, I had the same problem this week. The key ist the last argument in your function (the pointer). It's possible to pass additional arguments with this pointer: void int_function_1_func(double x, double xminusa, double bminusx, double &y, void *ptr) { double *param = (double *) ptr; a = *param; // this callback calculates f(x)=exp(x) y = exp(x)/a; // or whatever you want } int main(int argc, char **argv) { // // This example demonstrates integration of f=exp(x) on [0,1]: // * first, autogkstate is initialized // * then we call integration function // * and finally we obtain results with autogkresults() call // double a = 0; double b = 1; /* this could be your parameter */ double param = 5; autogkstate s; double v; autogkreport rep; autogksmooth(a, b, s); alglib::autogkintegrate(s, int_function_1_func, ¶m); /* the pointer to your parameter is an additional argument */ autogkresults(s, v, rep); printf("%.2f\n", double(v)); // EXPECTED: 1.7182 return 0; } |
Page 1 of 1 | All times are UTC |
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group http://www.phpbb.com/ |