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
 LabTalk Forum
 nlsf fit of multiple data sets
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

porta

UK
Posts

Posted - 01/22/2004 :  11:08:16 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Dear all,

i want to fit a lorentzian curve to about 50 data curves in columns in a worksheet named PLASMONS (the indep. variable x is in the first column) for a given x interval, and output the fitted parameters to a new worksheet...as i have no clue about programming, can anyone help me write a script to do it?
many thanks in advance

Mike Buess

USA
3037 Posts

Posted - 01/22/2004 :  9:08:10 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
In Origin 7.0 or later you can use MultiFit from OriginLab's File Exchange.

Mike Buess
Origin WebRing Member
Go to Top of Page

porta

UK
Posts

Posted - 01/23/2004 :  12:27:36 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Thanks for the link...however, i keep getting command error on entering mfInit...it compiles ok and i have a graph window open with 100 lines displayed. I have Origin7 SR4 v7.0552
any ideas?
Go to Top of Page

Mike Buess

USA
3037 Posts

Posted - 01/23/2004 :  10:50:58 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
I haven't tried it with that many curves. I'm not sure what caused your command error but I see now that you can't fit more than 50 curves in a single NLSF fitting session. (The number of curves is determined by the nlsf.numfitsets property which has a maximum value of 50.) Trying to fit more than 50 should not have caused a command error, but try again with 50 and see if it works. Please post the results one way or the other.

...I can set up a 50 curve session with mfInit but the last few curves need to be manually initialized. I suspect that the most curves that can be simultaneously fit without fixing and/or sharing at least one parameter is more like 25. This is with the Lorentz function which has 4 parameters.

Mike Buess
Origin WebRing Member

Edited by - Mike Buess on 01/23/2004 11:28:30 PM
Go to Top of Page

porta

UK
Posts

Posted - 01/26/2004 :  05:28:25 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Mike
well, i usually use the NLSF MultiFit option from the pull-down menu on a maximum of 40 at a time, but i can have up to 300 data sets to fit...rather than selecting and fitting 40 (which, judging from the curvature matrix are fitted at the same time?) i want to be able to fit all 300, in the same x(indep. variable) interval, perhaps one at a time...but without the need for graphs and such. The problem is that i have no knowledge of writing these things in C and i am more or less a beginner in LabTalk.
Go to Top of Page

Mike Buess

USA
3037 Posts

Posted - 01/26/2004 :  11:49:31 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
The Origin C function below is a crude but effective way to fit multiple datasets individually without plotting them first. The active window must be a worksheet in which the first column is X and the rest are Y. It fits each Y column separately and outputs the parameters and errors to separate rows in a new worksheet. (As always, the quality of each fit depends on how well its automatic parameter initialization works. That, in turn, depends mostly on the signal-to-noise ratio of your data.) A graph window is created in the process (I don't know how to suppress that yet) but should be removed automatically at the end. I will probably clean up this function and add it to the MultiFit functions on the File Exchange.
void MultiFit()
{
using nlsf = LabTalk.NLSF;
string sMsg;
if( nlsf.isBadFunc )
{
sMsg.Format("%s: No such function found!",nlsf.func$);
MessageBox( GetWindow(), sMsg, "Attention!", MB_OK | MB_ICONHAND );
return;
}

string sFunc = nlsf.func$;
nlsf.init();

Worksheet ws = Project.ActiveLayer();
if( !ws )
{
sMsg = "A worksheet window must be active.";
MessageBox( GetWindow(), sMsg, "Attention!", MB_OK | MB_ICONHAND );
return;
}
string sName = Project.Pages().GetName();

int i,j,nDat = ws.Columns.Count()-1;
if( !nDat )
{
sMsg ="Active worksheet has no data.";
MessageBox( GetWindow(), sMsg, "Attention!", MB_OK | MB_ICONHAND );
return;
}

nlsf.numfitsets = 1;
nlsf.msgprompt = 0;
int nPar = nlsf.npara;
WorksheetPage wpg;
wpg.Create("Origin.otw");
wpg.Label = sFunc;
LabTalk.page.title = 3;
string wName = wpg.GetName();
Worksheet wks(wName);
wks.Columns(0).SetType(OKDATAOBJ_DESIGNATION_NONE);
wks.Columns(0).SetName("Dataset");
wks.Columns(0).SetWidth(12);
wks.Columns(1).SetName(nlsf.n$(1)$);
wks.AddCol("err1");
wks.Columns(2).SetType(OKDATAOBJ_DESIGNATION_ERROR);
for(i=1;i<nPar;i++)
{
wks.AddCol(nlsf.n$(i+1)$);
sMsg.Format("err%d",i+1);
wks.AddCol(sMsg);
wks.Columns(2*i+2).SetType(OKDATAOBJ_DESIGNATION_ERROR);
}
wks.AddCol("chisqr");

LT_execute("win -a " + sName);
for(i=0;i<nDat;i++)
{
nlsf.fitdata$ = sName + "_" + ws.Columns(i+1).GetName();
nlsf.execute("paranint");
nlsf.iterate(100);
wks.SetCell(i,0,nlsf.fitdata$);
for(j=0;j<nPar;j++)
{
wks.SetCell(i,2*j+1,nlsf.p$(1+j));
wks.SetCell(i,2*j+2,nlsf.e$(1+j));
}
wks.SetCell(i,2*nPar+1,nlsf.chiSqr);
}
LT_execute("if(exist(%H)==3) win -c %H;");
LT_execute("win -a " + wName);
}


Mike Buess
Origin WebRing Member
Go to Top of Page

porta

UK
Posts

Posted - 01/26/2004 :  1:25:28 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
thanks Mike
that seems to do exactly what i need, and in about 1/100th of the time!..one last question: how do i set one of the intial parameters to zero and keep it that way (for example, when doing it with a Lorentzian, i want to keep y0=0).
Again, many thanks for the script
Go to Top of Page

Mike Buess

USA
3037 Posts

Posted - 01/26/2004 :  3:54:08 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
I incorporated the separate fitting method into MultiFit.c and added the ability to fix parameters. The function is now called mf1by1() and takes up to 10 parameter values as arguments. Arguments are handled like they are by the QuickFit function which is also available on the File Exchange. Default argument values are NANUM which means that the parameter is to be varied. So...

mf1by1 0; // fix the first parameter to zero (y0=0) but allow all other parameters to vary

mf1by1 skip skip 1; // fix the third parameter (w=1) and allow others to vary (skip==NANUM)

Now the function can be run while either a worksheet or plot is active.

Mike Buess
Origin WebRing Member

Edited by - Mike Buess on 01/27/2004 08:28:36 AM
Go to Top of Page

porta

UK
Posts

Posted - 01/27/2004 :  10:02:33 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Unfortunately, i still can't get it to work (multifit.c)...although it compiles, it does not recognise any of the commands (returns Command Error). The script you posted before works perfectly after including the Origin and OC_nag libraries.am i missing a library or something?
Go to Top of Page

Mike Buess

USA
3037 Posts

Posted - 01/27/2004 :  11:28:11 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
It works in my 7.0 SR4 with only Origin.h included. Compile should really be Build (Compile+Link). In CodeBuilder try Tools->Rebuild All and look for MultiFit.c among the compiler messages. The command mfhelp only uses printf so you can try that first after you build. (Check first to see if the mfhelp function is there... should be first in the file. If it's not then download MultiFit.c again from the File Exchange. I added mfhelp after I first put up the link in my previous message.)

Mike Buess
Origin WebRing Member
Go to Top of Page

porta

UK
Posts

Posted - 01/27/2004 :  12:45:18 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
i was building+compile...however, downloaded it again and it works great now! it's a nice piece of script..congrats!
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