OriginLab’s automatic recalculation mode is often useful for modelling curves when new data has been brought into the same columns. However by default any recalculated fitting function adjusts to new data based on parameters obtained from the previous fit, known as in-process parameters. While fitting functions can have fixed parameters, this will not automatically reinitialize those which are not. In this post we’ll be exploring how to install a user-defined fitting function and how to modify it to automatically re-initialize parameters whenever new data is introduced. You can follow along with this blog by downloading this OPJ.
Here we have some example data representing a gradual exponential decay curve.
In this next graph we see another exponential decay data with an apparently larger decay constant. Because these two sets of data have such different shapes, pasting new data in and using autorecalculate to fit the curve from one set to another may not be practical, as we will demonstrate.
Let’s say we have a user-defined fitting function file (.FDF) that we would like to use to fit both these sets of data. To do this, simply drag the downloaded the TestExpDec23.fdf file into the Origin window. You should then be prompted with the following dialog:
Select the User Defined category to put the file and choose Yes to move the file to the default User Files Folder location. We’ll now use this fitting definition file (FDF) to fit a curve to our data.
To do this, activate the graph window, then choose Analysis>Fitting>Nonlinear Curve Fit> and open the dialog box. Set the Category drop-down menu to User Defined , then set Function to TestExpDec23. At the top of the page, set the Recalculate drop-down to Auto. Leaving everything else as default, click Fit. Answer Yes to the prompt, then move and resize the result table, as needed so that you can clearly read the parameters and see the fit result curve.
As you can see, our fitting function has successfully converged with our data. Fitted parameter values y0, A1 and t1 show in the result table on graph. Before we change out the data on our graph, double-click on an axis to open the Axis Dialog. Select both Horizontal and Vertical on left and set Rescale for both axes to Auto, so that our new data will remain within the frame of our graph.
Right-click on the scatter plot and choose Change X > [Book1]Before!C(X):time. Right-click a second time and choose Change Y > [Book1]Before!D(Y):Y2. Because rescale has been set to Auto, Origin will adjust our axes to match the values of our new dataset.
As you can see for the second fit, when fitted parameter values from the previous fit were used as initial values, they did not provide a good estimate of the new data and therefore the fit didn’t properly converge (you will get an error message telling you that the fit did not converge). To make recalculation work with this new data we’ll need to go into the Fitting Function Builder and assign some parameter initial values in Initialization code field. To access the function builder press F8 or go to Tools>Fitting Function Builder and when the wizard opens, select the Edit a User-Defined Function radio button, the select TestExpDec23 from the drop-down list.
Click the Next >> button until you arrive at the following page. Make sure to check the Always run Initialization Code before Fitting box, then enter the following in the Initialization Code box:
Click Next>> until you can click Finish.
Returning to our workbook, you’ll see a second worksheet named After that duplicates the data in your original worksheet. Select the first two columns and select Plot>Basic 2D>Scatter to plot this data a second time. Double-click on the axis to open the Axis Dialog and once again select the Horizontal and Vertical axes, then click on the Scale tab and set Rescale to Auto.
Now, reopen the the non-linear fitting dialog (Analysis>Fitting>Nonlinear Curve Fit> Open Dialog) and repeat the previous steps, setting our function to the test function (the one whose parameters we modified in Fitting Function Builder), and set Recalculate to Auto. After clicking Fit, we should see the same graph that we had started with.
Again, right-click on the scatter plot and choose Change X > [Book1]After!C(X):time. Right-click a second time and choose Change Y > [Book1]After!D(Y):Y2. Because rescale has been set to Auto, Origin will adjust our axes to match the values of our new dataset.
This is just one simple example of how initialization scripts can be used to reset initial parameters for automatically analyzing multiple datasets.