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 User Defined category to put the file and choose Yes to move the file to Origin location, we’ll then use this FDF to fit a curve to our data. To do this, go to Analysis>Fitting>Nonlinear Curve Fit>Open Dialog… and in the Categories drop-down menu go to the “User Defined” category. From there select the “TestExpDec23” function, and then under the “Advanced” node, set recalculation to “Auto”. Leaving everything else as default, click Fit.
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 mode for both axes to “Auto”, so our new data will remain within the frame of our graph.
Now let’s change this to our other set of data by copying and pasting the columns for X2/Y2 in place of the values of our X1/Y1 data. Because rescale has been set to “auto[matic],” Origin will adjust our axes to the values of our new data.
As you can see, for the second fit, fitted parameter values of previous fit were used as initial values which is not a good estimate of the new data therefore the fit won’t properly 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, select Edit a User-Defined Function, and select our function.
Click next until you see the screen below. Check off the “Always run Initialization Code before Fitting” tickbox and then enter the following code-.
Then click next until you can click Finish. If you are using Origin C code to initialize code, make sure the code is set to Use Origin C.
In our worksheet, you’ll see a second worksheet called “after” that should be identical to your original worksheet. Select the first two columns and select Plot>Symbol>Scatter to plot this data a second time. Go back into the axis dialog and once again set the axes to rescale automatically. Once our axes settings have been changed and our data has been plotted, let’s go back to the non-linear fitting dialog and repeat the previous steps, setting our function to the test function we modified, and recalculate to auto. After clicking okay, we should see the same graph that we had started with.
Now that we’ve shown our original fit to be the same, in the worksheet select the 3rd and 4th columns (X2/Y2), copy, and paste these columns in place of the first two. Our graph will update as it had previously and we can clearly see that our initialization script reset the parameters to fit to this new dataset.
This is just one simple example of how initialization scripts can be used to reset initial parameters for automatically analyzing multiple datasets.