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
 Simple code!
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

espenhjo

Norway
20 Posts

Posted - 11/19/2003 :  08:28:27 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Hi,
Because I have been less lucky with the response to some of my postings here I'd like to state a quite simple question related to my C code for use with NLSF.

My function, parplane, has a loop that calculates an array of values that is to be used in the NLSF equation. The parameter I want to optimize is also a part of the array definition so I want the array to vary freely during the fit. Now, the array must be initialized and I have been doing that in the C code for the function. I think this makes a problem for NLSF because the array is reset each time NLSF tries to improve the fit.

My question(s) then is:
Should I move the array initialization loop to "Initialize parameters" in NLSF? Do I need to declare the array also there then? Should the array be declared both for parameter initialization and in the function code?

My code is:
#define JMAX 50
#define xacc 1e-12
#define nr_roots 100 //number of roots to be found

The function:
double parplane(double a, double q) //function for use in NLSF
{
double dSum = 0; //cumulative output set to zero
double rtn[nr_roots],x1[nr_roots],x2[nr_roots],fx[nr_roots],df[nr_roots],dx[nr_roots];
int j;
int i;

The array initialization:
for (i=0; i<nr_roots; i++)
{
x1[i] = (i+1)*PI-1;
x2[i] = (i+1)*PI+1;
rtn[i] = 0.5*(x1[i]+x2[i]);
}
//these guess for the range in which a root exists could come from a
//bracketing and bisection algorithm

i = 0;
while (i < nr_roots)
{
for (j=1;j<JMAX; j++)
{
fx[i] = rtn[i] * tan(rtn[i]) / a; //the function with roots
df[i] = tan(rtn[i]) / a + rtn[i] * (1+tan(rtn[i]) * tan(rtn[i])) / a; //first derivative of the function

dx[i] = -fx[i]/df[i];
rtn[i] += dx[i];

printf("%e\n", dx[i]);

if ((x1[i]-rtn[i])*(rtn[i]-x2[i]) < 0.0){
printf("error, jumped outside bounds");
exit(1);}
if (fabs(dx[i]) < xacc){
////printf("found root after %d attempts, at %lf\n", j, rtn[i]);
printf("The value of rtn[%d] is %lf\n", i, rtn[i]);
break;}
if (j = JMAX - 1){
printf("error - exceeded max tries no root");
i++;
exit(1);}
}
dSum += exp(-(rtn[i]^2) / (a^2)) * 2 * (1 + sin(2*rtn[i])/(2*rtn[i]))^(-1) * ((2*PI*q*a)*sin(2*PI*q*a)*cos(rtn[i])-rtn[i]*cos(2*PI*q*a)*sin(rtn[i]))^2 / ((2*PI*q*a)^2-rtn[i]^2)^2;
i++;
}
return dSum;
}

Thank yoouu!

Espen

yuivanov

USA
11 Posts

Posted - 11/19/2003 :  10:29:09 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Espen
Usually if you want some part of the function to be executed only on first call you may use static BOOL to indicate that function is being executed for the first time.
If I understood you correctly you want the following part of the function to be executed only once.

for (i=0; i<nr_roots; i++)
{
x1[i] = (i+1)*PI-1;
x2[i] = (i+1)*PI+1;
rtn[i] = 0.5*(x1[i]+x2[i]);
}

so if you do this:

static BOOL l_bFirstTime = TRUE;
if( l_bFirstTime )
{
l_bFirstTime = FALSE;
for (i=0; i<nr_roots; i++)
{
x1[i] = (i+1)*PI-1;
x2[i] = (i+1)*PI+1;
rtn[i] = 0.5*(x1[i]+x2[i]);
}
}

it should initialize your array only once per fitting session.
Hope this helps.
Yuri
Go to Top of Page

espenhjo

Norway
20 Posts

Posted - 11/20/2003 :  02:53:50 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
This was exactly what I ment!

Thank you!

Espen
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