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
 NLSF exponential decay
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

winflowers

USA
34 Posts

Posted - 02/20/2006 :  11:18:11 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Version (Select Help-->About Origin): 7.5 SR5
Operating System: windows XP

Hi all,

I modified some codes copied from this forum to perform exponoential decay fit. Everything was going well until I tried to extrapolate some data. Here is an example, I would like to use the last 20 data in column b to do exponential decay fit and extend this fitting curve to some certain Y value.

Here is the code I am using:

void test(string wksName)

{

Worksheet wks(wksName);
string strDVar = wksName + "_b";
Dataset dsTime(wks,0), dsLum(wks,1);
double dHalfLum = dsLum[0]/2;
int iUpperbound = dsTime.GetUpperBound();
double dHalfLife;


using NLSF = LabTalk.NLSF;

NLSF.Init();

NLSF.Func$ = "ExpDec1";

NLSF.P1 = 0;

NLSF.P2 = 1000;

NLSF.P3 = 1000;

NLSF.V1 = 0;

NLSF.dataBegin = iUpperbound-19;
NLSF.dataEnd = iUpperbound+1;


NLSF.xBegin = dsTime[iUpperbound-19]; // set up x values
NLSF.xEnd = ??;
NLSF.xPoints = 50;

NLSF.FitData$ = strDVar;
NLSF.ChiSqrErr = 0;

NLSF.IterateEx(100);

NLSF.End();
double dDVal = dHalfLum, dIVal;
int iErr = nlfXfromY( dDVal, &dIVal, 1);
printf("The value of x where y = %g is %g", dDVal, dIVal );

}

Is it possible to set the xEnd value where certain Y is included? Or I need to perform mathematical function first to get the X value according to the parameters found from the fitting?

Any kind help is appreciated!

Mike Buess

USA
3037 Posts

Posted - 02/20/2006 :  12:52:49 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
quote:
Or I need to perform mathematical function first to get the X value according to the parameters found from the fitting?
That's what nlfXfromY does. xBegin and xEnd are used with the makeCurve() method which you haven't used yet. (Perhaps it is called automatically by iterateX() or End()). Try something like this but I'm sure it will need some tweaking...

void test(string wksName)
{
Worksheet wks(wksName);
string strDVar = wksName + "_b";
Dataset dsTime(wks,0), dsLum(wks,1);
double dHalfLum = dsLum[0]/2;
int iUpperbound = dsTime.GetUpperBound();
double dHalfLife;
using NLSF = LabTalk.NLSF;
NLSF.Init();
NLSF.Func$ = "ExpDec1";
NLSF.FitData$ = strDVar;
NLSF.P1 = 0;
NLSF.P2 = 1000;
NLSF.P3 = 1000;
NLSF.V1 = 0;
NLSF.ChiSqrErr = 0;

NLSF.dataBegin = iUpperbound-19;
NLSF.dataEnd = iUpperbound+1;

NLSF.Iterate(100);
NLSF.Iterate(100);
NLSF.Iterate(100);

double dYVal = dHalfLum, dXVal;
nlfXfromY(dYVal, &dXVal, 1);
printf("The value of x where y = %g is %g\n", dYVal, dXVal );
NLSF.xMode = 5; // 1 and 5 work the same. Actually, you don't need this line at all!
NLSF.xBegin = dsTime[iUpperbound-19]; // set up x values
NLSF.xEnd = dXVal;
NLSF.xPoints = 50;
//NLSF.makeCurve("func"); // this is called by NLSF.End(). Use one or the other.
NLSF.End();
}

...Corrections marked in red work with my test data. The final message is this: everything you needed was in your original code but a bit of reordering was required to make it work.

Mike Buess
Origin WebRing Member

Edited by - Mike Buess on 02/20/2006 1:55:07 PM

Edited by - Mike Buess on 02/20/2006 2:48:02 PM

Edited by - Mike Buess on 02/20/2006 3:02:43 PM
Go to Top of Page

winflowers

USA
34 Posts

Posted - 02/21/2006 :  08:15:07 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Thank Mike for the codes!
Actually I have tested some similar codes but got the same problem. It seems that nlfXfromY(dYVal, &dXVal, 1) can only get the correct value within the fitting range, ohterwise you would get some unexpected result. In my case, I got 0 which is definitely wrong. That's why I raised this question.
What I am doing is just as I said in the first post to calculate the desired X value from Y value according to the parameters found from the fitting. Or is there any other solution?
Go to Top of Page

Mike Buess

USA
3037 Posts

Posted - 02/21/2006 :  11:39:39 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Apparently you're right about nlfXfromY but nlfFit works outside the fitting range. You can try something like this to find dXVal at dY=dHalfLum without using the fit parameters directly. You could even use it to generate the fit curve itself.

double dYVal,dXVal = dsTime[iUpperbound-19];
double dXInc = dsTime[iUpperbound-18] - dXVal;
do
{
dYVal = nlfFit(dXVal);
dXVal += dXInc;
} while (dYVal>=dHalfLum);
printf("The value of x where y = %g is %g\n", dHalfLum, dXVal );

Mike Buess
Origin WebRing Member
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