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
 Fitting a function with numerical integration

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
cyberjuda Posted - 05/29/2009 : 06:17:41 AM
Origin Ver. 8 and Service Release 4(Select Help-->About Origin):
Operating System: Windows XP Professional

I can't seem to able to use this function as a fit function. Any basic help in the right direction would be appreciated.


where T, t, a and b are variables whereas c, d are constants.

I have read through the wiki but can't seem to find anything I can use specifically for this problem.
6   L A T E S T    R E P L I E S    (Newest First)
larry_lan Posted - 12/13/2009 : 9:03:56 PM
Hi:

Could you please read the following tutorials first:

Fitting with Integral using NAG Library

Fitting Integral Function with parametric limit using NAG Library

Fitting with Summation

User Defined Fitting Funciton using GNU Scientific Library

If you still have problems after reading the tutorials, please contact our technical support team and send your data to us.

Thanks
Larry
cmohan76 Posted - 12/11/2009 : 09:00:32 AM
can any one help me in fitting data using the function


where gamma(0)and "a" are constant

can some generate a user define function for NLSFit fitting the data
Echo_Chu Posted - 06/03/2009 : 05:25:49 AM
Hi,

You can use Peak Analyser with this user-defined function.Please refer to this page for more details.

http://www.originlab.com/www/helponline/origin8/en/Origin.htm#UserGuide/Define_a_Peak_Function.htm

Echo
OriginLab

cyberjuda Posted - 06/03/2009 : 03:51:11 AM
I realized that I have been using the wrong tutorial. Well my fit function is finally working. Here is the code and the equation in case it helps someone else. Just another question though. Is it possible to modify this code so that I can use it to fit multiple peaks? The function would simply be a summation for all the peaks. If someone can again point me in the right direction, it would be highly appreciated.


a=w
b=xc
[Edit] I forgot to mention that in the parameter options I had to set c=0 and d is the Amplification factor.
#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.
#include <oc_nag8.h>
 
 
// Add code here for other Origin C functions that you want to define in this file,
// and access in your fitting function.
struct user
{
	double a, b, 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 aa, bb, fitX; // temp variable to accept the parameters in the Nag_User communication struct
        aa = sp->a;
        bb = sp->b;
        fitX = sp->fitX;
 
        return exp(fitX/(0.8))*exp(-bb/(8.314*fitX))*exp((-aa*8.314*fitX*fitX*exp(-bb/(8.314*fitX)))/(0.4*bb));
}
 
// 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.

// 

//----------------------------------------------------------
// 
void _nlsfPWwithparametric(
// Fit Parameter(s):
double a, double b, 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;  
	        // you may use epsabs and epsrel and this quantity to enhance your desired precision 
	        // when not enough precision encountered
	 
	Nag_QuadProgress qp;
	static NagError fail;
	 
	// the parameters parameterize the integrand can be input to the call_back function
	        // through the Nag_User communication struct 
	        Nag_User comm;	
	struct user s;
	s.a = a;
	s.b = b;
	s.fitX = x;
	        comm.p = (Pointer)&s;
	 
	d01sjc(f_callback, c, s.fitX, epsabs, epsrel, max_num_subint, &result, &abserr, &qp, &comm, &fail);
	 
	 
	        // you may want to exam the error by printing out error message, just uncomment the following lines
	// if (fail.code != NE_NOERROR)
	        // printf("%s\n", fail.message);
	 
	 
	// 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);
	}
	 
	 
	y = d*(exp(-x/0.8))*(a/0.4)*result; 
	// End of editable part
}
cyberjuda Posted - 06/02/2009 : 05:13:16 AM
Thank you for the help. I have been able to change the code from one of the tutorials to fit my equation but it doesn't seem to work. Can you please look at the code and see if everything is in order. Oh and I also attach the new form of the equation that I am using. I am able to simulate it correctly but when I try to fit it, it gives me an error that "Fit did not converge - reason unknown." The edits that I made to the code concerns the part where the integral is given and then in the second last line I added the part before the integral. I think the problem might be related to the limits of the integration but don't know how to edit that.

#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.
#include <oc_nag8.h>
 
 
// Add code here for other Origin C functions that you want to define in this file,
// and access in your fitting function.
 
struct user   // parameters in the integrand
{
	double amp, center, width;
 
};
// Function supplied by user, return the value of the integrand at a given x.
static double NAG_CALL f_callback(double x, Nag_User *comm) 
{
        struct user *sp = (struct user *)(comm->p);
 
        double amp, center, width;    // temp variable to accept the parameters in the Nag_User communication struct
        amp = sp->amp;
        center = sp->center;
        width = sp->width;
 
	return amp * exp(x/(0.8))*exp(-center/(8.314*x))*exp((-width*8.314*x*x*exp(-center/(8.314*x)))/(0.4*center));
}
 
 
// 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.


//----------------------------------------------------------
// 
void _nlsfPW(
// Fit Parameter(s):
double y0, double A, double xc, double w,
// Independent Variable(s):
double x,
// Dependent Variable(s):
double& y)
{
	// Beginning of editable part
	// Through the absolute accuracy epsabs, relative accuracy epsrel and max_num_subint you can  
	// control the precision of the integration you need 
	// if epsrel is set negative, the absolute accuracy will be used. 
	// Similarly, you can control only relative accuracy by set the epsabs negative
	double 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
	Integer max_num_subint = 200;
	 
	// Result keeps the approximate integral value returned by the algorithm
	// abserr is an estimate of the error which should be an upper bound for the |I - result| 
	// where I is the integral value
	double result, abserr;
	 
	// The structure of type Nag_QuadProgress, 
	// it contains pointers allocated memory internally with max_num_subint elements
	Nag_QuadProgress qp;
	 
	// The NAG error parameter (structure)
	static NagError fail;
	 
	// Parameters passed to integrand by Nag_User communication struct
	        Nag_User comm;	
	struct user s;
	        s.amp = A;
	        s.center = xc;
	        s.width = w;
	        comm.p = (Pointer)&s;
	 
	// Perform integration
	// There are 3 kinds of infinite boundary types you can use in Nag infinite integrator
	// Nag_LowerSemiInfinite, Nag_UpperSemiInfinite, Nag_Infinite
	d01smc(f_callback, Nag_LowerSemiInfinite, x, epsabs, epsrel, max_num_subint, &result, &abserr, &qp, &comm, &fail);
	 
	        // you may want to exam the error by printing out error message, just uncomment the following lines
	// if (fail.code != NE_NOERROR)
	      //  printf("%s\n", fail.message);
	 
	// 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);
	}
	 
	// Calculate the fitted value
	y = y0 + exp(-x/0.8)*w/0.4*result;
	// End of editable part
}
Echo_Chu Posted - 05/31/2009 : 05:39:27 AM
Hi,

These are two tutoruals to show how to fit with integral function in Origin.

http://wiki.originlab.com/~originla/howto/index.php?title=Tutorial:Fitting_with_Integral_using_NAG_Library

http://wiki.originlab.com/~originla/howto/index.php?title=Tutorial:Fitting_Integral_Function_with_parametric_limit_using_NAG_Library

To intergral from 0 to x, you can use d01sjc(f_callback, 0, x,...).

Echo
OriginLab Corp


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