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
 Fitting with function containing two integrals
 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 - 04/22/2015 :  01:18:34 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Ver. and Service Release (Select Help-->About Origin): 9.0
Operating System: WinXP

Hi, i need to fit experimental data using expression very similar to this one:


I have implemented the fitting procedure by a single term in this equation using NAG Library. However, i need to add the second term as in equation in the picture. It is a bit unclear to me how to do this (I'm newbie/dummy in Origin C) using the Origin C. Can I perform the fitting procedure by using equation containing two integrals as in picture above in using the OriginLab libraries ? (I've tried do this using LabTalk, however, LabTalk will not integrate...). May be can You give any recommendations or ideas ?

Tank you for your attention !
Best regards, Dmitriy.

Edited by - DimkaFF on 04/26/2015 06:20:46 AM

ZanHUNG

20 Posts

Posted - 04/22/2015 :  03:39:00 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
The answer to your first question is "YES". The number of integrals is irrelevant. Say, you have implemented the first term as y1 = A1*exp(-b1*...). Surely you can implement the second one without problem, as y2 = A2*exp(-b2*...). Then the function you need is just y = y1 + y2.

For the second one. Maybe you should use w, instead of x as independent variable, where
.
Go to Top of Page

DimkaFF

Lithuania
8 Posts

Posted - 04/22/2015 :  5:24:35 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
ZanHUNG, thank you for your reply. I'm sorry, i've incorrectly wrote an equation in the my first topic. This one below is correct:


As it seen, I need to fit experimental data by the integrals containing two different integrand, i.e. convolution of two functions f(t)g(t-x) and f(t)h(t-x), i.e. i need to solve two different integrals in one iteration. To do that, i need declare the both integrands, calculate its quadratures, and return sum of its values . Its is not clear to me or can I do that using OriginLab NAG libraries, or i need to write my personal code.
To perform the fit procedure by the single-term function i have adapted this NAG-library-based code (it works fine and calculates much faster than that do Wolfram Mathematica. So, thanks to guys form OriginLab):
http://www.originlab.com/doc/Tutorials/Fitting-Integral-ParaLimit-NAG


Thank you for your attention.

Edited by - DimkaFF on 04/22/2015 5:25:51 PM
Go to Top of Page

ZanHUNG

20 Posts

Posted - 04/22/2015 :  11:32:52 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
quote:
Originally posted by DimkaFF

ZanHUNG, thank you for your reply. I'm sorry, i've incorrectly wrote an equation in the my first topic. This one below is correct:


As it seen, I need to fit experimental data by the integrals containing two different integrand, i.e. convolution of two functions f(t)g(t-x) and f(t)h(t-x), i.e. i need to solve two different integrals in one iteration. To do that, i need declare the both integrands, calculate its quadratures, and return sum of its values . Its is not clear to me or can I do that using OriginLab NAG libraries, or i need to write my personal code.
To perform the fit procedure by the single-term function i have adapted this NAG-library-based code (it works fine and calculates much faster than that do Wolfram Mathematica. So, thanks to guys form OriginLab):
http://www.originlab.com/doc/Tutorials/Fitting-Integral-ParaLimit-NAG


Thank you for your attention.




What's the difference between "using OriginLab NAG libraries" and "write my personal code"? You always write "personal code" which may or may not call NAG function(s). Two integrals, and two integrands declared. You've known that. Show me the code you wrote for one integral, and point out what stops you from writing code for two integrals calling NAG function(s) twice?
Go to Top of Page

DimkaFF

Lithuania
8 Posts

Posted - 04/24/2015 :  11:29:02 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
quote:

Two integrals, and two integrands declared. You've known that. Show me the code you wrote for one integral, and point out what stops you from writing code for two integrals calling NAG function(s) twice?



Let us say, that we have the code for the single integrand as follows:
========================================================================

#include <origin.h>
#include <oc_nag8.h>
 
// integrand declaratin 
struct user
{
        double a, b, fitX;  // fitX the independent variable of fitting function
};

static double NAG_CALL f1(double x, Nag_User *comm)  // x is the independent variable of the integrand
{
	struct user *sp = (struct user *)(comm->p);
        double aa1, bb1, fitX; // temp variable to accept the parameters in the Nag_User communication struct
        aa1 = sp->a1;
        bb1 = sp->b1;
        fitX = sp->fitX;
        return cosh((x*x+bb1*bb1*fitX1*fitX1)/(bb1+fitX))/(aa1+(x*x+fitX*fitX));
}

void _nlsfnag_integration_fitting_cosh(
// Fit Parameter(s):
double a1, double b1, double c, double d,
// Independent Variable(s):
double x,
// Dependent Variable(s):
double& y)
{
	// Beginning of editable part
	double epsabs = 0.00001, epsrel = 0.0000001, result, abserr;
	Integer max_num_subint = 500;  
	Nag_QuadProgress qp;
	static NagError fail;
 
        Nag_User comm;	
	struct user s;
	s.a = a1;
	s.b = b1;
	s.fitX = x;
        comm.p = (Pointer)&s;
 
	d01sjc(f1, 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);
	} 
	y = log(result); 
}

========================================================================
And now i would to adapt this code to integrate two different integrands and return the sum the integrals integral1+integral2. However, the syntax how i should do that is unclear to me (as I mentioned, I have never programmed in C/ Origin C, however, I beginning intensively studies). Ok, step-by-step. Firstly we should to declare the second integrand as follows (if i correctly understand):

========================================================================

#include <origin.h>
#include <oc_nag8.h>
 
// Fitting function 
struct user
{
        double a, b, a1, b1 fitX;  // fitX the independent variable of fitting function
};

static double NAG_CALL f1(double x, Nag_User *comm)  // x is the independent variable of the integrand
{
	struct user *sp = (struct user *)(comm->p);
        double aa1, bb1, fitX; // temp variable to accept the parameters in the Nag_User communication struct
        aa1 = sp->a1;
        bb1 = sp->b1;
        fitX = sp->fitX;
        return cosh((x*x+bb1*bb1*fitX1*fitX1)/(bb1+fitX))/(aa1+(x*x+fitX*fitX));
}

static double NAG_CALL f2(double x, Nag_User *comm)  // x is the independent variable of the integrand
{
	struct user *sp = (struct user *)(comm->p);
        double aa2, bb2, fitX; // temp variable to accept the parameters in the Nag_User communication struct
        aa2 = sp->a2;
        bb2 = sp->b2;
        fitX = sp->fitX;
        return cosh((x*x+bb2*bb2*fitX*fitX)/(bb2+fitX))/(aa2+(x*x+fitX*fitX));
}

========================================================================

And in the following part of the code I should separately integrate the integrands f1 and f2 in the same range, let us say, from c to d, and then return its sum y =integral(f1)+integral(f2). The question is how I can return and sum results of two integration procedures
d01sjc(f1, c, d, epsabs, epsrel, max_num_subint, &result, &abserr, &qp, &comm, &fail);
and
d01sjc(f2, c, d, epsabs, epsrel, max_num_subint, &result, &abserr, &qp, &comm, &fail);

========================================================================

void _nlsfnag_integration_fitting_cosh(
// Fit Parameter(s):
double a1, double b1, double a2, double b2,  double c, double d,
// Independent Variable(s):
double x,
// Dependent Variable(s):
double& y)
{  <...> 
	<y = result1+result2;> 
}

========================================================================


Go to Top of Page

ZanHUNG

20 Posts

Posted - 04/25/2015 :  10:44:36 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Something similar to the folowing:

void _nlsfnag_integration_fitting_cosh(
// Fit Parameter(s):
double a1, double b1, double a2, double b2,  double c, double d,
// Independent Variable(s):
double x,
// Dependent Variable(s):
double& y)
{
    // Beginning of editable part
    double epsabs = 0.00001, epsrel = 0.0000001, result1, result2, abserr1, abserr2;
    Integer max_num_subint = 500;  
    Nag_QuadProgress qp1, qp2;
    static NagError fail1, fail2;
 
    Nag_User comm1, comm2;
    struct user s1, s2;
    s1.a = a1;
    s1.b = b1;
    s1.fitX = x;
    s2.a = a2;
    s2.b = b2;
    s2.fitX = x;
    comm1.p = (Pointer)&s1;
    comm2.p = (Pointer)&s2;
 
    d01sjc(f1, c, d, epsabs, epsrel, max_num_subint, &result1, &abserr1, &qp1, &comm1, &fail1);
    d01sjc(f2, c, d, epsabs, epsrel, max_num_subint, &result2, &abserr2, &qp2, &comm2, &fail2);
    
    if (fail1.code != NE_INT_ARG_LT && fail1.code != NE_BAD_PARAM && fail1.code != NE_ALLOC_FAIL)
    {
        NAG_FREE(qp1.sub_int_beg_pts);
        NAG_FREE(qp1.sub_int_end_pts);
        NAG_FREE(qp1.sub_int_result);
        NAG_FREE(qp1.sub_int_error);
    }
    if (fail2.code != NE_INT_ARG_LT && fail2.code != NE_BAD_PARAM && fail2.code != NE_ALLOC_FAIL)
    {
        NAG_FREE(qp2.sub_int_beg_pts);
        NAG_FREE(qp2.sub_int_end_pts);
        NAG_FREE(qp2.sub_int_result);
        NAG_FREE(qp2.sub_int_error);
    }
	
    y = result1 + result2;
}

Edited by - ZanHUNG on 04/25/2015 10:48:05 AM
Go to Top of Page

DimkaFF

Lithuania
8 Posts

Posted - 04/25/2015 :  10:18:16 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
quote:
Something similar to the folowing:


ZanHUNG, thank you for taking the time to help me write the code. The fitting after your explanation works fine ! Thanks again.

Below I posted the code, enabling to perform integration of two separate function with parametric limit. Maybe the code may be useful for other OriginC dummies like me.

#include <origin.h>

// Add your special include files here.
// For example, if you want to fit with functions from the NAG library, 
// add the header file for the NAG functions here.

// Add code here for other Origin C functions that you want to define in this file,
// and access in your fitting function.

// You can access C functions defined in other files, if those files are loaded and compiled 
// in your workspace, and the functions have been prototyped in a header file that you have
// included above. 

// You can access NLSF object methods and properties directly in your function code.

// You should follow C-language syntax in defining your function. 
// For instance, if your parameter name is P1, you cannot use p1 in your function code. 
// When using fractions, remember that integer division such as 1/2 is equal to 0, and not 0.5
// Use 0.5 or 1/2.0 to get the correct value.

// For more information and examples, please refer to the "User-Defined Fitting Function" 
// section of the Origin Help file.
#include <oc_nag8.h>
 
// Fitting function 
struct user
{
        double a1, b1, a2, b2, fitX;  // fitX the independent variable of fitting function
};

static double NAG_CALL f1(double x, Nag_User *comm1)  // x is the independent variable of the integrand
{
	struct user *sp1 = (struct user *)(comm1->p);
        double aa1, bb1, fitX; // temp variable to accept the parameters in the Nag_User communication struct
        aa1 = sp1->a1;
        bb1 = sp1->b1;
        fitX = sp1->fitX;
        return cosh((x*x+bb1*bb1*fitX*fitX)/(bb1+fitX))/(aa1+(x*x+fitX*fitX));
}

static double NAG_CALL f2(double x, Nag_User *comm2)  // x is the independent variable of the integrand
{
	struct user *sp2 = (struct user *)(comm2->p);
        double aa2, bb2, fitX; // temp variable to accept the parameters in the Nag_User communication struct
        aa2 = sp2->a2;
        bb2 = sp2->b2;
        fitX = sp2->fitX;
        return cosh((x*x+bb2*bb2*fitX*fitX)/(bb2+fitX))/(aa2+(x*x+fitX*fitX));
}

//----------------------------------------------------------
// 
void _nlsf_integration_fitting_cosh(
// Fit Parameter(s):
double a1, double b1, double a2, double b2, double c, double d,
// Independent Variable(s):
double x,
// Dependent Variable(s):
double& y)
{
	// Beginning of editable part
	
	    double epsabs = 0.00001, epsrel = 0.0000001, result1, result2, abserr1, abserr2;
	    Integer max_num_subint = 500;  
	    Nag_QuadProgress qp1, qp2;
	    static NagError fail1, fail2;
	 
	    Nag_User comm1, comm2;
	    struct user s1, s2;
	    s1.a1 = a1;
	    s1.b1 = b1;
	    s1.fitX = x;
	    s2.a2 = a2;
	    s2.b2 = b2;
	    s2.fitX = x;
	    comm1.p = (Pointer)&s1;
	    comm2.p = (Pointer)&s2;
	 
	    d01sjc(f1, c, d, epsabs, epsrel, max_num_subint, &result1, &abserr1, &qp1, &comm1, &fail1);
	    d01sjc(f2, c, d, epsabs, epsrel, max_num_subint, &result2, &abserr2, &qp2, &comm2, &fail2);
	    
	    if (fail1.code != NE_INT_ARG_LT && fail1.code != NE_BAD_PARAM && fail1.code != NE_ALLOC_FAIL)
	    {
	        NAG_FREE(qp1.sub_int_beg_pts);
	        NAG_FREE(qp1.sub_int_end_pts);
	        NAG_FREE(qp1.sub_int_result);
	        NAG_FREE(qp1.sub_int_error);
	    }
	    if (fail2.code != NE_INT_ARG_LT && fail2.code != NE_BAD_PARAM && fail2.code != NE_ALLOC_FAIL)
	    {
	        NAG_FREE(qp2.sub_int_beg_pts);
	        NAG_FREE(qp2.sub_int_end_pts);
	        NAG_FREE(qp2.sub_int_result);
	        NAG_FREE(qp2.sub_int_error);
	    }
	
	    y = log(result1) + log(result2);
	// End of editable part
}

Edited by - DimkaFF on 04/26/2015 4:04:27 PM
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