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 function with integral
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

annalisabranca

Netherlands
Posts

Posted - 10/14/2008 :  11:54:44 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Hi!
I'm using Origin 8 SR0.
I read the other questions on the same topic, but I'm still lost, since I'm new to programming .

Expression:
y=Intagrate{(exp[(-x^2*L^2)/(16*Pi^2)]*(4*Pi*x^2))/(((w-(w0-120*(x/(2*Pi/a0))^2))^2)+(G/2)^2}; dx

integration boundaries : 2*Pi/a0 (upper), 0 (lower)

y is the dependent variable, x the indipendent variable and all the others are parameters.
The data of the spectra that I need to fit are on 2 columns: column A wavelength (x axis), column B intensity (y axis)

Some help for the code?
Thank you in advance!

LisaB

Hideo Fujii

USA
1582 Posts

Posted - 10/16/2008 :  3:41:08 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Lisa,

Following tutorials may help you:

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

Hope these samples help you.

--Hideo Fujii
OriginLab Corp.

P.S. If you are using Origin 8 SR0, I strongly recommend to upgrade to SR4 because lots of bug fixes and improvements.
http://www.originlab.com/index.aspx?s=9&lm=315

Edited by - Hideo Fujii on 10/16/2008 3:44:22 PM
Go to Top of Page

annalisabranca

Netherlands
Posts

Posted - 10/21/2008 :  12:12:37 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Hideo Fujii

Thanks for your help. I updated my Origin8 to SR4 version.
I also tried to use the tutorials you linked, but I still have a problem. I am able to compile it, but when I simulate it, nothing changes (whatever parameters I put in), it just stays a straight line at 0. I am sure that the function is correct cause it is reported in many scientific articles, so it might be a matter of code. Could you have a look on the code I wrote? I am not able to find what is wrong.
This is what Ifve done:
Thanks again!
Lisa

I need to program this fitting function



Where:
q0=2/5.483
L (size),w0,C(width) are parameters
w(centre) is the independent variable of the fitting function
x is the independent variable of the integrand


#pragma warning(error : 15618)
#include <origin.h>

#include <oc_nag8.h>
#define NAG_FREE(X) x04bdc((Pointer *)&(X))

// 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 size, center, width,w0;

};
// 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 size, center, width,w0; // temp variable to accept the parameters in the Nag_User communication struct
size = sp->size;
center = sp->center;
width = sp->width;
w0 = sp->w0;


return (exp((-x*x*size*size)/(16*PI*PI))*4*PI*x*x)/((((center-(w0-120*(x/(2*PI/5.483))*(x/(2*PI/5.483)))))*((center-(w0-120*(x/(2*PI/5.483))*(x/(2*PI/5.483))))))+(width/2)*(width/2));
}

//----------------------------------------------------------

void _nlsfnag_integration_fitting(
// Fit Parameter(s):
double L, double g, double w0,
// Independent Variable(s):
double x,
// Dependent Variable(s):
double& y)
{
// Beginning of editable part

double epsabs = 0.0, epsrel = 0.0001;
double a=0;
double b=2*PI/5.483;

Integer max_num_subint = 500;

// Result keeps the approximate integral value returned by the algorithm

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.size = L;
s.center = x;
s.width = g;
s.w0 = w0;


comm.p = (Pointer)&s;

// Perform integration


d01smc(f_callback,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);
}

// Calculate the fitted value
y = result;


// End of editable part
}
Go to Top of Page

annalisabranca

Netherlands
Posts

Posted - 10/22/2008 :  05:28:34 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi!
I found the mistake!
Still thanks a lot!
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