Minimum Origin Version Required: OriginPro 9.0 SR0
Prior to running the following example, the nlsf_utils.c file need to be loaded and compiled. This can be done from script with the following command or just add this file to your workspace.
run.LoadOC(Originlab\nlsf_utils.c);
The following code shows how to use NLFitSesssion to fit with ellipse data.
#include <..\originlab\NLFitSession.h> void NLFitSession_Implicit_example1(int nUseGetNBox = 0) { // get the active worksheet Worksheet wks = Project.ActiveLayer(); if(!wks) { out_str("Not a valid worksheet!"); return; // need to activate a worksheet with data } NLFitSession FitSession; // 1. set function if(!FitSession.SetFunction("Ellipse")) { out_str("Set fitting function error!"); return; } // 2. set the data StringArray vstrVars; int numVars; if ( !FitSession.GetNumVars(NULL, &numVars, NULL, &vstrVars) ) { out_str("Get variable error!"); return; } GETN_TREE(trInput); trInput.AddNode("InputData"); int nDataRules = DRR_NO_FACTORS | DRR_NLFIT; trInput.InputData.SetAttribute(STR_DATA_RULES_ATTRIB, nDataRules); TreeNode trInputData; if ( !okutil_create_implicit_fitter_data_node_ex(&trInputData, &trInput, "InputData", "Input Data", &vstrVars, WEIGHT_NONE, TRUE) ) { out_str("Create input data tree error!"); return; } if ( nUseGetNBox ) { trInput.FirstNode.SetAttribute(STR_ATTRIB_BRANCH, GETNBRANCH_OPEN); trInput.FirstNode.FirstNode.SetAttribute(STR_ATTRIB_BRANCH, GETNBRANCH_OPEN); if ( !GetNBox(trInput) ) { out_str("Set GetNBox InputData error!"); return; } trInputData = trInput.InputData; } else { TreeNode trRange = trInputData.FirstNode; TreeNode trX = trRange.FirstNode; TreeNode trWX = trX.NextNode; TreeNode trY = trWX.NextNode; TreeNode trWY = trY.NextNode; string strRange; wks.Columns(0).GetRangeString(strRange); trX.strVal = strRange; wks.Columns(1).GetRangeString(strRange); trY.strVal = strRange; int nWeightMethod = WEIGHT_DIRECT; int nWA = 0, nWB = 0, nWC = 0; wks.Columns(2).GetRangeString(strRange); trWX.strVal = strRange; trWX.SetAttribute(STR_FITTER_WEIGHT_METHOD_ATTRIB, nWeightMethod); trWX.SetAttribute(STR_FITTER_WEIGHT_A_ATTRIB, nWA); trWX.SetAttribute(STR_FITTER_WEIGHT_B_ATTRIB, nWB); trWX.SetAttribute(STR_FITTER_WEIGHT_C_ATTRIB, nWC); wks.Columns(3).GetRangeString(strRange); trWY.strVal = strRange; trWY.SetAttribute(STR_FITTER_WEIGHT_METHOD_ATTRIB, nWeightMethod); trWY.SetAttribute(STR_FITTER_WEIGHT_A_ATTRIB, nWA); trWY.SetAttribute(STR_FITTER_WEIGHT_B_ATTRIB, nWB); trWY.SetAttribute(STR_FITTER_WEIGHT_C_ATTRIB, nWC); } if(!FitSession.SetData(trInputData)) { out_str("Set data error!"); return; } // 3. set parameter init values if(!FitSession.ParamsInitValues()) { out_str("Init values error!"); return; } // 4. start fitting int nFitOutcome; if(!FitSession.Fit(&nFitOutcome)) { string strOutcome = FitSession.GetFitOutCome(nFitOutcome); out_str("Fit error! "+strOutcome); return; } // 5. success, get results RegStats fitStats; NLSFFitInfo fitInfo; FitSession.GetFitResultsStats(&fitStats, &fitInfo, false, 0); vector<string> vsParamNames; int nNumParamsInFunction = FitSession.GetParamNamesInFunction(vsParamNames); vector vParamValues, vErrors; FitSession.GetFitResultsParams(vParamValues, vErrors); printf("# Iterations = %d, Reduced Chisqr = %g\n", fitInfo.Iterations, fitStats.ReducedChiSq); for(int nParam=0; nParam<vParamValues.GetSize(); nParam++) { printf("# %s = %f, %s_error = %f\n", vsParamNames[nParam], vParamValues[nParam], vsParamNames[nParam], vErrors[nParam]); } }