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
 Programming weighting into curve fitting
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

c091

UK
Posts

Posted - 09/01/2004 :  07:09:26 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Origin Version (Select Help-->About Origin): 7.5 SR0
Operating System:Windows XP

Hi Everyone,

I'm trying to program weighting (preferably instrumental) into my automated curve fitting routine (using ExpDec1 fitting function) to take account of error bars. I have no idea how to go about this. Can anyone help?

A portion of the code is attached below (basically a hacked version of the example batch processing code from automation.c):

// Get name of curve
crvData.GetName(strPeakId);

// Plot data curve to active layer
int nPlot = grphLayer.AddPlot(crvData, IDM_PLOT_SCATTER);
grphLayer.Rescale();

// Add error bars to plot - NEED TO REMOVE HARD-CODING...
GraphLayer lay("Graph1");
Curve cc("Peak10_ts", "Peak10_It");
Column colErrBar("Peak10", 2);
int nPlotIndex = lay.AddErrBar(cc, colErrBar);
out_int("nPlotIndex = ", nPlotIndex);


// Perform nonlinear fit to plotted data
using NLSF = LabTalk.NLSF; // access NLSF object properties directly
NLSF.Init(); // initialize fitter
//NLSF.PasteToPlot = 0; // turn off pasting results to graph
NLSF.Func$ = "expdec1"; // assign fitting function
NLSF.FitData$ = strPeakId; // assign fitting dataset
NLSF.Execute("parainit"); // perform auto parameter initialization
NLSF.Fit(100); // perform fit using 100 iterations
NLSF.PasteParams("P"); // Paste fit params to graph
NLSF.End(); // Show fit results in results window


Also, I need to remove the hard-coding in the bit of code that adds the error bars. Rather than use "Peak10", since this will vary according to the name of the worksheet (which is Peakxx), I'd like to use the variable which I also defined as the worksheet name i.e. strPeakId.
However, if I simply substitute this for the hardcoding, it doesn't work (no error bars appear on graph and nPlotIndex=-1). i.e:

// Add error bars to plot - DOESN'T APPEAR TO WORK VERY WELL CURRENTLY
GraphLayer lay("Graph1");
Curve cc(strPeakId + "_ts", strPeakId + "_It"); //My column headings are: ts(X) and It(Y) and dIt(yEr)
Column colErrBar(strPeakId, 2);
int nPlotIndex = lay.AddErrBar(cc, colErrBar);
out_int("nPlotIndex = ", nPlotIndex);

Hope this makes sense...
I'd really appreciate some direction with my programming!

Many thanks in advance,
Sara

easwar

USA
1965 Posts

Posted - 09/01/2004 :  1:49:36 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Sara,

To fit with error bars, you need to set a few more properties of the NLSF object. Even though the code is in OC, it is directly setting properties of the LabTalk NLSF object. For documentation on the LabTalk NLSF object, see the LabTalk language reference documentation.

So in the code pasted below, I am setting the nlsf.wtype property to 1, which is instrumental weighting. Also note that you need to set the error bar dataset name property as well: nlsf.w$. When fitting manually, Origin picks up the plotted error bar name, but when doing this programmatically, need to specify. In the code below, I switched from using Col object for the error bar to a Dataset object because then it is easy to get the needed name using the GetName() method of Dataset. The GetName() method for col will only return col name (such as C, as opposed to say data_c).

Also, in the code below, I am assuming user passes name of a worksheet, and then I work with absolute column numbers for x,y,yerr and so there is no hard coding of dataset names.

Check if this code works for you, and if you need more help, post again.

Easwar
OriginLab


void test(string strWksName)
{
// Start with an active worksheet that has the data and error bars
Worksheet wks = strWksName;
if( !wks )
{
out_str( "Invalid worksheet object!" );
return;
}

// Create a curve from columns of the worksheet
// Refer to columns by position number
Curve crvData(wks, 0, 1);

// Create a new graph
// Can replace default template with custom one if needed
GraphPage gpg;
gpg.Create( "Origin.OTP" );
// Point to Layer 1 and plot the curve
// Can change to point to different layer as needed
GraphLayer gly = gpg.Layers( 0 );
gly.AddPlot( crvData, IDM_PLOT_SCATTER );

// Define error dataset and add to data plot
Dataset dsErr( wks, 2);
int nPlotIndex = gly.AddErrBar( crvData, dsErr );
if( -1 == nPlotIndex)
{
out_str( "Failed to add error bar data" );
return;
}

// Rescale layer - can leave out if not desired
gly.Rescale();

// Perform nonlinear fit to plotted data
using NLSF = LabTalk.NLSF;
NLSF.Init();
NLSF.Func$ = "expdec1";
NLSF.FitData$ = crvData.GetName();
NLSF.Execute("parainit");

// Set NLSF weighting method to instrumental
// Note that you also need to point to the error bar dataset
NLSF.Wtype = 1;
NLSF.W$ = dsErr.GetName();
// Also clear the "Scale parameter errors with reduced chi-sq"
// When using error bars, the default mode is to have this property cleared
NLSF.ChiSqrErr = 0;

NLSF.Fit(100);
NLSF.PasteParams("P");
NLSF.End();

}


Go to Top of Page

c091

UK
Posts

Posted - 09/02/2004 :  09:03:51 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Many thanks, Easwar, your solution worked a treat. I'm very grateful for your time and effort.

I have another question, if I may trouble you further:

For the same project, I'd like to extract the t1 value derived from the ExpDec1 fitting function into a new worksheet for each dataset fitted, so that the final worksheet would have an X-column containing PeakIds (the name of each dataset fitted using the ExpDec1 function) and a Y-Column containing all the t1 values (obtained from curve-fitting each dataset to ExpDec1 function) and a YErr-Column containing all the errors in t1 values (also obtained from curve-fit). Is this possible?

With thanks,
Sara

Go to Top of Page

Mike Buess

USA
3037 Posts

Posted - 09/02/2004 :  09:35:07 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Sara,

The MultiFit add-on module at OriginLab's File Exchange collects all fitting parameters from a multiple curve fitting session into a single worksheet like you described.

http://www.originlab.com/FileExchange/details.aspx?C=5&cid=2&fid=56

The mfCreateOutputWks() function creates the results wks. Code for writing the parameters to the results wks is at the end of mfseq(). Should help get you started at least.

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