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
 Origin Forum
 Fitting with integral 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

mruehrig

Germany
Posts

Posted - 12/17/2006 :  11:28:05 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Version (Select Help-->About Origin): 7.5 SP6
Operating System: XP

Hi all,

I am new to the forum and I am seeking advice for a specific problem I have. I have measurement data (signal A as function of parameter x) which I want to fit with a function of the form (hope the upload of the .gif works):

F is a function of two parameters where one of the parameter (y) follows a distribution (B(y))(eg. log normal or gaussian).

Any ideas how I could do this? I can fit vor B(y)= constant but have no idea how to include the integral form into the fit function.

Any help would be greatly appreciated.

Kind regards,

Manfred

zachary_origin

China
Posts

Posted - 12/18/2006 :  01:17:53 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
You can see this for how to create an usr defined function with integration (it does not contain infinitive integration).
As for your function, you may use the trick here to do an infinitive integration.

Zachary
OriginLab Technical Services.

Edited by - zachary_origin on 12/18/2006 06:20:08 AM
Go to Top of Page

mruehrig

Germany
Posts

Posted - 12/19/2006 :  4:06:18 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
I tried to use the nag_cumul_normal function to compute the integral.
Actually when I a genererate a normal distribution, integrate it with the calculus tool, and try to fit the resulting graph with the
nag_cumul_normal function it works extremely well (i.e. it returns the mean and the standard deviation I used to generate the standard distribution)
Hoever, now I'm not sure how to do the convolution of the integral with the Function F(x,y) and fit the result.
Go to Top of Page

zachary_origin

China
Posts

Posted - 12/19/2006 :  9:36:49 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
First, to be more clear, we'd better change the formula you pasted to the form of



Then we can start as follows, create a user-defined fitting function (for how to create the function, see here). the function boby goes like:

double dIntegral = 0.0;
double dInt = 0.0;
double t = 0.0; // the integration interval ranges from 0.0 to infinity
double dPrecision = 1e-6; // the precision to control the integraton to infinity
double dt = 0.05; // this is the step size for the integration.

//the following lines actually perform a integrate to the function, the trapezoidal rule is used.
do
{
dInt = 0.5 * ( B(t)*F(t,x)*pow(t,3.0) + B(t+dt)*F(t+dt,x)*pow(t,3.0)) * dt ; //here the formula for the integration function B(t)*F(t,x) should be changed as you requirment.
dIntegral += dInt;
}while( dInt/dIntegral > dPrecision )
y=y0+A*dIntegral;



Please note that since this will do integration for every X, the fitting may be very time-consuming. The step size dt is very important for the fitting speed.

Zachary
OriginLab Technical Services.
Go to Top of Page

mruehrig

Germany
Posts

Posted - 12/20/2006 :  4:39:50 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Thanks. To be more precise, teh function I want to fit through my measurement looks like this:




According to your advice, I have generated the following function with mu and sigma as parameter:
void _nlsfintegral(
// Fit Parameter(s):
double sigma, double mu,
// Independent Variable(s):
double x,
// Dependent Variable(s):
double& y)
{
// Beginning of editable part
double dIntegral = 0.0;

double dInt = 0.0;

double t = 0.0; // the integration interval ranges from 0.0 to infinity

double dPrecision = 1e-6; // the precision to control the integraton to infinity

double dt = 0.05; // this is the step size for the integration.



//the following lines actually perform a integrate to the function, the trapezoidal rule is used.

do

{

dInt = 0.5 * (B(t,sigma, mu)*F(x,t)+ B(t+dt,sigma,mu)*F(x,t+dt))*dt

dIntegral += dInt;

}while( dInt/dIntegral > dPrecision );

y=dIntegral;

;
// End of editable part
}

I compiled it without errors (after I added a missing semicolon after the while statement and corrected the y=y0+A*dIntegral; to y=dIntegral;)

However, when I start fitting my data, it always pops up with:

"Error 28037. Likely caused by errors in user defined formula or poor parameter initialization."

What to do? I tried several start values without succes.
Go to Top of Page

zachary_origin

China
Posts

Posted - 12/20/2006 :  9:12:14 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Compiled without error? Then I am confused because in your formula B(t, mu, sigma) and F(x,t) are not defined. I wrote them just as pseudo code for the formula, you should give out the detailed expression, e.g, if B(t,mu,sigma) is a gaussian function, you need replace it with exp(-2*(t-mu)*(t-mu)/(sigma*sigma)) / (sigma*sqrt(PI/2)) .

By the way, if you are fitting with the convolution of Gaussian and Lorentz function, you may try the built-in Voigt function.

Zachary
OriginLab Technical Services.
Go to Top of Page

mruehrig

Germany
Posts

Posted - 12/22/2006 :  06:38:25 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Of course, I used B(t, mu, sigma) and F(x,t) also as pseudo code...
You are right, B(t, mu, sigma) is a distribution function (log-normal B(t, mu sigma) = 1/(sqrt(2*PI)*sigma*x)*exp(-(ln(x/mu))^2/(2*sigma^2)).The function F(x,t) is a trigonometric function.
Therefore I do not see how I could use the built in convolutions. Still get teh error message:
"Error 28037. Likely caused by errors in user defined formula or poor parameter initialization."
Go to Top of Page

Moritz Sokolowski

Germany
Posts

Posted - 12/22/2006 :  12:38:14 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
I am facing exactly the same problem using this code! y(x) is not computed, although compilation is successful.
In addition, should there not be a line for incrementation of the variable t?

t+=dt

??

Moritz Sokolowski, Bonn, D
Go to Top of Page

zachary_origin

China
Posts

Posted - 12/23/2006 :  12:45:47 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi,

1> for log-normal function, when t=0, Origin will fail to calculate the function because B(t,sigma,mu) = 1/(sqrt(2*PI)*sigma*t)*exp(...) contains an error of dividing by zero.
Hi

There are indeed some errors in the function definition.
1> for log-normal function, when t=0, Origin will fail to calculate the function because B(t,sigma,mu) = 1/(sqrt(2*PI)*sigma*t)*exp(...) contains an error of dividing by zero. As t approaches 0.0, the limit of function B(t, sigma, mu) is 0.0, we can use a conditional statement to set the value for B() as 0.0.

2> yes, t += dt is required.


The modified code is pasted as follows, Assume F(x,t) = sin(x+t).

void _nlsfIntegral(
// Fit Parameter(s):
double mu, double sigma,
// Independent Variable(s):
double x,
// Dependent Variable(s):
double& y)
{
// Beginning of editable part
double dIntegral = 0.0;
double dInt = 0.0;
double t = 0.0; // the integration interval ranges from 0.0 to infinity
double dPrecision = 1e-6; // the precision to control the integraton to infinity
double dt = 1.0; // this is the step size for the integration.
//the following lines actually perform a integrate to the function, the trapezoidal rule is used.
double F1, F2, B1, B2;
if (mu<0)
{
y = NANUM;
return;
}
do
{
if(t == 0.0)
{
B1 = 0.0; // lim B(t,mu,sigma) = 0;
// t->0
}
else B1 = exp( -0.5*(ln(t/mu))^2/sigma/sigma ) / sqrt(2.0*PI)/t;

B2 = exp( -0.5*(ln((t+dt)/mu))^2/sigma/sigma ) / sqrt(2.0*PI)/(t+dt);
F1 = sin(x+t); // F(x) = sin(x+t);
F2 = sin(x+t+dt);

dInt = 0.5 * (B1*F1 + B2*F2) * dt ;
dIntegral += dInt;
t +=dt;
}while( dInt/dIntegral > dPrecision );
y=dIntegral;
// End of editable part
}



Zachary
OriginLab Technical Services.
Go to Top of Page

Estitxu

13 Posts

Posted - 02/14/2013 :  09:39:06 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi I'm having problems with this code...

I have Origin 8 and I want to fit a function of the form

where x is the independent variable of my fitting, and t is the independent variable of my integral.

I type the code bellow and I do not have problems when compiling, but when simulating the function or when trying to fit it, the only thing I obtain is a constant line with 0 value, no matter the parameters I insert. Thanks a lot!

Here's the code:

// Fit Parameter(s):
double P, double tau, double rhoN, double wN, double tN, double L,
// Independent Variable(s):
double x,
// Dependent Variable(s):
double& y)
{
// Beginning of editable part
double a = 5.2532E-6*sqrt(rhoN)/(wN*tN);
double b = 7.209E8*L^2*rhoN;

double dIntegral = 0.0;
double dInt = 0.0;
double t = 0.0; //the integration interval ranges from 0.0 to infinity
double dPrecision = 1e-6; //the precision to control the integration to infinity
double dt = 0.5; //this is the step size for the integration



do
{
if (t==0.0)
{
dInt = 0.5 * ( exp(-b/dt-dt/tau)/sqrt(dt)*cos(2.21E5*x*(dt))) * dt ; //limit when t->0
}
else

dInt = 0.5 * ( exp(-b/t-t/tau)/sqrt(t)*cos(2.21E5*x*t) + exp(-b/(t+dt)-(t+dt)/tau)/sqrt(t+dt)*cos(2.21E5*x*(t+dt))) * dt ;
dIntegral += dInt;
t +=dt;
}while( dInt/dIntegral > dPrecision );
y=P^2*a*dIntegral;
// End of editable part
}
Go to Top of Page

Sam Fang

291 Posts

Posted - 02/16/2013 :  03:29:12 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Note that there is a decay term in the integration part, if b is very large or tau is very small, the integral may be always zero. To calculate the integral with better accuracy, you'd better use a step size based on your decay constant.

Origin9.0 provides an easy way to define a fitting function with integral in Fitting Function Builder tool. You needn't use code to define it. See the tutorial in the page:
http://www.originlab.com/www/helponline/Origin/en/mergedProjects/Tutorial/Tutorial/Fitting_with_Integral_using_LabTalk_Function.html

In Origin8SR6, you can use NAG function to define the integral. You can see the example in the page:
http://www.originlab.com/www/helponline/Origin/en/mergedProjects/Tutorial/Tutorial/Fitting_with_Integral_using_NAG_Library.html

For your function, you can define it as follows:

#include <oc_nag8.h>



struct user
{
double b, tau, fitX;  // fitX the independent variable of fitting function
};



static double NAG_CALL f_callback(double x, Nag_User *comm)  // x is the independent variable of the integrand
{
 
struct user *sp = (struct user *)(comm->p);
 
double b, tau, fitX; // temp variable to accept the parameters in the Nag_User communication struct
b = sp->b;
tau = sp->tau;
fitX = sp->fitX;
 
return 1/sqrt(x)*exp(-b/x-x/tau)*cos(2.21e5*fitX*x);
}



void _nlsffitint(
// Fit Parameter(s):
double P, double tau, double rhoN, double wN, double tN, double L,
// Independent Variable(s):
double x,
// Dependent Variable(s):
double& y)
{
	// Beginning of editable part
	double a = 5.2532E-6*sqrt(rhoN)/(wN*tN);
	double b = 7.209E8*L^2*rhoN;
	
	double epsabs = 0.00001, epsrel = 1e-3, result, abserr;
	Integer max_num_subint = 100; 
	
	Nag_QuadProgress qp;
	static NagError fail;
	
	Nag_User comm;	
	struct user s;
	s.b = b;
	s.tau = tau;
	s.fitX = x;
	comm.p = (Pointer)&s;
	    
	d01smc(f_callback, Nag_UpperSemiInfinite, 0, 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 = P^2*a*result;
	// End of editable part
}


Sam
OriginLab Technical Services

Edited by - Sam Fang on 02/16/2013 03:36:42 AM
Go to Top of Page

Estitxu

13 Posts

Posted - 02/21/2013 :  11:19:38 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi,
did you check the function? Cause I get the same problem here as well, I get a constant line with 0 value! :(
I'll be grateful to any suggestion... thanks anyway!!
Go to Top of Page

Sam Fang

291 Posts

Posted - 02/22/2013 :  11:40:02 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
A constant line 0 may be due to your initial parameters for decay constant.

You can try following parameters:
P=1
tau=1e3
rhoN=1e-10
wN=1
tN=1
L=1

It will be not a constant line 0.

If the problem still exists, you can send your fitting data to us. We will see whether we can fix it.

To upload a file, click Send File to Tech support button in the top right of the forum.

Sam
OriginLab Technical Services
Go to Top of Page

HelenaRamírez

Brazil
3 Posts

Posted - 08/12/2016 :  3:44:39 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi everyone,

I have a similar fuction like Estitxu, and I get the same problem, a constant line with 0 value. What can i do in this case? I'll be grateful to any suggestion, thanks.
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