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
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 Forum for Origin C
 Function in function (Nested function)
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

DimkaFF

Lithuania
8 Posts

Posted - 07/26/2017 :  11:47:54 PM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
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.

Edited by - DimkaFF on 07/27/2017 09:32:08 AM

yuki_wu

896 Posts

Posted - 07/28/2017 :  03:33:30 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
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

Edited by - yuki_wu on 07/28/2017 03:34:32 AM
Go to Top of Page

yuki_wu

896 Posts

Posted - 07/28/2017 :  04:06:30 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
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

Edited by - yuki_wu on 07/28/2017 04:06:52 AM
Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000