forum.alglib.net

ALGLIB forum
It is currently Mon Dec 23, 2024 4:57 am

All times are UTC


Forum rules


1. This forum can be used for discussion of both ALGLIB-related and general numerical analysis questions
2. This forum is English-only - postings in other languages will be removed.



Post new topic Reply to topic  [ 2 posts ] 
Author Message
 Post subject: integrate function with more than one parameter
PostPosted: Mon Aug 29, 2011 4:28 pm 
Offline

Joined: Mon Aug 29, 2011 4:19 pm
Posts: 1
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;
}


Top
 Profile  
 
 Post subject: Re: integrate function with more than one parameter
PostPosted: Mon May 14, 2012 11:26 am 
Offline

Joined: Mon May 14, 2012 11:17 am
Posts: 1
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, &param); /* the pointer to your parameter is an additional argument */

autogkresults(s, v, rep);

printf("%.2f\n", double(v)); // EXPECTED: 1.7182
return 0;
}


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 2 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 11 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group