The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
 All Forums
 Origin Forum for Programming
 Forum for Origin C
 Function in function (Nested function)

Note: You must be registered in order to post a reply.
To register, click here. Registration is FREE!

Screensize:
UserName:
Password:
Anti-Spam Code:
Format Mode:
Format: BoldItalicizedUnderlineStrikethrough Align LeftCenteredAlign Right Horizontal Rule Insert HyperlinkUpload FileInsert Image Insert CodeInsert QuoteInsert List
   
Message:

* HTML is OFF
* Forum Code is ON
Smilies
Smile [:)] Big Smile [:D] Cool [8D] Blush [:I]
Tongue [:P] Evil [):] Wink [;)] Clown [:o)]
Black Eye [B)] Eight Ball [8] Frown [:(] Shy [8)]
Shocked [:0] Angry [:(!] Dead [xx(] Sleepy [|)]
Kisses [:X] Approve [^] Disapprove [V] Question [?]

 
Check here to subscribe to this topic.
   

T O P I C    R E V I E W
DimkaFF Posted - 07/26/2017 : 11:47:54 PM
Origin Ver. and Service Release (Select Help-->About Origin): 2015
Operating System: WinXP

Hello, i shortly introduce to the my problem.

I need to fit experimental data (independent variable is frequency and dependent variable is amplitude) using the Fourier integral. However, the problem is a complex integrand as in the picture below.


The integrand is the product of two inner integrals that are dependent on time t and needed to be integrated over inner variable x. Independent fitting variable is angular frequency w and some fitting parameters (they are the same for both inner integrals). Outer integration variable is time t. To do numerical integration i planned to use NAG library: nag_1d_quad_inf_wt_trig_1 (d01ssc) routine for Fourier integrals and nag_1d_quad_gen_1 (d01sjc) for inner integrals. However, I am a little bit confused, how can I construct such the complex integrand to return it to the nag_1d_quad_inf_wt_trig_1 (d01ssc) routine. I would ask is there a way to call function inside other function in OriginC using NAG library ?

Thank you for attention.
2   L A T E S T    R E P L I E S    (Newest First)
yuki_wu Posted - 07/28/2017 : 04:06:30 AM
Oh, I forgot one thing:

Please note that using a nested integral function as the fitting function may cause a slow fitting.

Regards,
Yuki
OriginLab
yuki_wu Posted - 07/28/2017 : 03:33:30 AM
Hi,

You can call one integral function in another integrand function directly. I know it is literally nonintuitive, so I wrote a simple example below that may help to understand:

It is supposed that I have a function like:


The following integral routine shows how to calculate a nested integral:

#include <OC_nag.h> 
static docuble NAG_CALL f_callback_1(double x, Nag_User *comm)
{
	int *use_comm = (int *)comm->p;
	return (x*x);
}

double nag_d01sjc_1()
{
	double a = 0.0;        
	double b = 2.0;  // integration interval 
	double epsabs, abserr, epsrel, result;
   // you may use epsabs and epsrel and this quantity to enhance your desired precision 
   // when not enough precision encountered        
	epsabs = 0.0;
	epsrel = 0.0001;
	// The max number of sub-intervals needed to evaluate the function in the integral
	// The more diffcult the integrand the larger max_num_subint should be
	// For most problems 200 to 500 is adequate and recommmended
	int max_num_subint = 200;         
	Nag_QuadProgress qp;
	NagError fail;           
	Nag_User comm;
	static int use_comm[1] = {1};        
	comm.p = (Pointer)&use_comm;
	d01sjc(f_callback_1, a, b, epsabs, epsrel, max_num_subint,&result, &abserr, &qp, &comm, &fail);

	// For the error other than the following three errors which are due to bad input parameters 
	// or allocation failure  NE_INT_ARG_LT  NE_BAD_PARAM   NE_ALLOC_FAIL
	// You will need to free the memory allocation before calling the integration routine again to 
	// avoid memory leakage
	if (fail.code != NE_INT_ARG_LT && fail.code != NE_BAD_PARAM && fail.code !=  NE_ALLOC_FAIL)
	{   
		NAG_FREE(qp.sub_int_beg_pts);
		NAG_FREE(qp.sub_int_end_pts);
		NAG_FREE(qp.sub_int_result);
		NAG_FREE(qp.sub_int_error);        
	} 
	return result;
}

static double NAG_CALL f_callback_2(double y, Nag_User *comm)
{
	int *use_comm = (int *)comm->p;
	return (nag_d01sjc_1()*y);
}

void nag_d01sjc_2()
{
	double c = 0.0;        
	double d = 3.0;
	double epsabs, abserr, epsrel, result;   
	epsabs = 0.0;
	epsrel = 0.0001;
	int max_num_subint = 200;         
	Nag_QuadProgress qp;
	NagError fail;           
	Nag_User comm;
	static int use_comm[1] = {1};        
	comm.p = (Pointer)&use_comm;
	
	d01sjc(f_callback_2, c, d, epsabs, epsrel, max_num_subint,&result, &abserr, &qp, &comm, &fail);
	
	if (fail.code != NE_INT_ARG_LT && fail.code != NE_BAD_PARAM && fail.code !=  NE_ALLOC_FAIL)
	{   
		NAG_FREE(qp.sub_int_beg_pts);
		NAG_FREE(qp.sub_int_end_pts);
		NAG_FREE(qp.sub_int_result);
		NAG_FREE(qp.sub_int_error);        
	} 
	printf("%10.6f", result); 
}


Hope it helps.

Regards,
Yuki
OriginLab

The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000