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
 Problem in NLSF with conditional structures
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

Bill Choi

Hong Kong
Posts

Posted - 08/28/2006 :  04:55:01 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Hello! I'm new in using Origin C scripts. I've problems in establishing new functions for NLSF with my Origin 7.5. As the function involves the solution of a third order equation, I need to use conditional statements to sepcify different conditions. The equations and conditions can be seen in the attachments.

The function can be compiled, and I've checked the expressions are essentially correct (hopefully). Also the initial guess of the parameters are good enough. However, I just only get a straight line instead of a curve with very large discrepancy. Is there any problem with my scripts?? Thank you very much!!

The following is my script in the function window:
Parameters: Em, Ed, Et, Kd(K1), Kt(K2)
Independent variable: C
Dependent variable: A

double a=2/(3*Kt);
double b=1/(3*Kd*Kt);
double c=-(C)/(3*Kd*Kt);

double Q=((a^2)-3*b)/9;
double R=(2*(a^3)-9*a*b+27*c)/54;
double D=Q^3-R^2;

int F;
if (R>=0)
{
F=1;
}
else
{
F=-1;
}

double t;
double W;
double Cm;

if (D>=0)
{
t=atan((sqrt(D))/R);
Cm=-2*sqrt(Q)*cos((t+2*PI)/3)-(a/3);
}
else
{
W=(sqrt(-D)+abs(R))^(1/3);
Cm=-F*(W+(Q/W))-(a/3);
}

A=Cm*Em+2*Kd*(Cm^2)*Ed+3*Kd*Kt*(Cm^3)*Et;

The following are the values of A and C for the fitting
C A
9.96064E-07 0.05341
1.99213E-06 0.10882
3.98426E-06 0.18392
5.97639E-06 0.24633
7.96852E-06 0.29239
9.96064E-06 0.33974
1.4941E-05 0.49191
1.99213E-05 0.61984

Initial Guess:
Em = 110000
Ed = 10000
Et = 9000
Kd = 180000
Kt = 98000



zachary_origin

China
Posts

Posted - 08/28/2006 :  07:22:22 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi,

This maybe do something with you use number in an integer form, then it converts the results from double to integer, which may cause the lose of precision.
I have changed all the integer constants to a float type. e.g from 2 to 2.0.
The code is listed as following:

if( Kt == 0.0 || Kd == 0.0)
{
A = NANUM;
return;
}

double a=2.0/(3.0*Kt);
double b=1.0/(3.0*Kd*Kt);
double c=-(C)/(3.0*Kd*Kt);

double Q=((a^2.0)-3.0*b)/9.0;
double R=(2.0*(a^3.0)-9.0*a*b+27.0*c)/54.0;
double D=Q^3.0-R^2.0;

if (R == 0.0)
{
A = NANUM;
return;
}
double F;
if (R>=0)
{
F=1.0;
}
else
{
F=-1.0;
}

double t;
double W;
double Cm;

if (D>=0.0)
{
t=atan((sqrt(D))/R);
Cm=-2.0*sqrt(Q)*cos((t+2.0*PI)/3.0)-(a/3.0);
}
else
{
W=(sqrt(-D)+abs(R))^(1.0/3.0);
Cm=-F*(W+(Q/W))-(a/3.0);
}
A=Cm*Em+2.0*Kd*(Cm^2.0)*Ed+3.0*Kd*Kt*(Cm^3.0)*Et;


Now using the code, I can get the fitted curve close to the data.
But the fitted parameters seem not very good comparing with the initialized parameters which are good enough as you mentioned.
Is there something else wrong? I cannot be sure.

Zachary
OriginLab GZ Office

Edited by - zachary_origin on 08/28/2006 09:01:40 AM
Go to Top of Page

Bill Choi

Hong Kong
Posts

Posted - 08/28/2006 :  10:11:09 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
I can get a good fit with your script provided. Thanks so much for your advice and effort!
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