Author |
Topic |
|
Estitxu
13 Posts |
Posted - 08/06/2013 : 05:26:49 AM
|
Hi,
I'm using Origin 8.5 and I need to fit an integral function, which inside the integrand has a Heaviside function. I don't find the Heaviside function in this version of Origin, so I'm trying to define it myself... The function I need to fit is the following:
I tried defining the step function in two ways, but I have some compilation errors which I don't know where they come from! I used the following two codes:
CODE 1:
#pragma warning(error : 15618) #include <origin.h> #include <oc_nag8.h> #define NAG_FREE(X) x04bdc((Pointer *)&(X))
// 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.
struct user { double e, EF, KT, Vb, fitX; // fitX the independent variable of fitting function double x0=EF-e*x+e*Vb; double step(double FitX, double x0) { return (fitX<x0?0:(fitX==x0?1:1)); }
}; 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 ee, EE, KK, VV, fitX; // temp variable to accept the parameters in the Nag_User communication struct e= sp->e; EF = sp->EF; KT = sp->KT; Vb = sp->Vb; x0 = sp->x0; fitX = sp->fitX;
return (1/(1+exp(fitX/KT))-1/(1+exp((fitX-e*x)/KT)))*step(fitX,x0); }
// 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 _nlsfnag_integration_marco( // Fit Parameter(s): double e, double EF, double KT, double R, double Vb, // Independent Variable(s): double x, double It, // 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.e = e; s.EF = EF; s.KT = KT; s.Vb = Vb; s.fitX = x; comm.p = (Pointer)&s;
d01sjc(f_callback, Nag_LowerSemiInfinite, Nag_UpperSemiInfinite, 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 = R*It*result; // note use log of the integral result as return as the integral result is large, // you are not necessary to do so // End of editable part }
CODE 2:
#pragma warning(error : 15618) #include <origin.h> #include <oc_nag8.h> #define NAG_FREE(X) x04bdc((Pointer *)&(X))
// 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.
struct user { double e, EF, KT, Vb, 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 ee, EE, KK, VV, fitX; // temp variable to accept the parameters in the Nag_User communication struct ee = sp->e; EE = sp->EF; KK = sp->KT; VV = sp->Vb; fitX = sp->fitX;
return
{ if(fitX<EE-ee*x+ee*VV) { return 0; } else { return (1/(1+exp(fitX/KK))-1/(1+exp((fitX-ee*x)/KK))); } } }
// 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 _nlsfCopyOfnag_integration_marco( // Fit Parameter(s): double e, double EF, double KT, double R, double Vb, // Independent Variable(s): double x, double It, // 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.e = e; s.EF = EF; s.KT = KT; s.Vb = Vb; s.fitX = x; comm.p = (Pointer)&s;
d01sjc(f_callback, Nag_LowerSemiInfinite, Nag_UpperSemiInfinite, 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 = R*It*result; // note use log of the integral result as return as the integral result is large, // you are not necessary to do so // End of editable part }
Do you know what's wrong? Or do you know how I could improve them?? (of course it's enough with one of them as long as it works ;) )
Thanks a lot!! |
|
Estitxu
13 Posts |
Posted - 08/06/2013 : 05:32:28 AM
|
Sorry, I should have put x (indep. parameter) instead of V in the function!! so the function I wanna fit is the following:
The rest is on the previous post! thanks again!! |
|
|
Sam Fang
293 Posts |
Posted - 08/07/2013 : 05:57:08 AM
|
Function body for NAG_CALL f_callback in CODE 2 can be changed as:
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 ee, EE, KK, VV, fitX; // temp variable to accept the parameters in the Nag_User communication struct
ee = sp->e;
EE = sp->EF;
KK = sp->KT;
VV = sp->Vb;
fitX = sp->fitX;
/*return
{ if(fitX<EE-ee*x+ee*VV) {
return 0;
} else {
return (1/(1+exp(fitX/KK))-1/(1+exp((fitX-ee*x)/KK)));
}
}*/
if(fitX<EE-ee*x+ee*VV)
{
return 0;
}
else
{
return (1/(1+exp(fitX/KK))-1/(1+exp((fitX-ee*x)/KK)));
}
}
Sam OriginLab Technical Services |
|
|
Estitxu
13 Posts |
Posted - 08/07/2013 : 09:41:46 AM
|
Hi Sam,
thanks for your help! Now I can compile the function, but I can not get to do the fitting. I want to fit R and Vb, the last one is inside the Heaviside function and it seems that the function is independent of its value! What I mean is that when I do the fitting, the program only fits R's value and leaves the initial value of Vb unchanged. And regardless the initial value of Vb that I put (in a range between 0.1 and 100), the fitted value of R doesn't change!
Do you know where the problem can be??
Thanks again!!
|
|
|
Sam Fang
293 Posts |
Posted - 08/09/2013 : 02:28:26 AM
|
You can try to change Vb in a larger range. In fact as long as fitX>EE-ee*x+ee*VV, all VV can get the same result.
If it still fails, you can send your fitting data to us. To send a file to us, click Send File to Tech support on the top right of the forum.
Sam OriginLab Technical Services |
Edited by - Sam Fang on 08/09/2013 02:29:11 AM |
|
|
|
Topic |
|
|
|