Summation Fitting using Worksheet Template

In this example we are going to fit data to a function that contains series summation (see below). This is a complicated fitting function where g_i and E_i assume different value in different summation term.  Origin 2021 improved greatly in Python support so in this blog we will show using Python code in Origin to perform this fitting. 

Download attached project file and open it in Origin 2021, first two columns are the x,y data to fit, col(C) is the initial guess of the fitted parameters E_i and col(D) is the constants g_i in the fitting function.  The fitting has been performed and the fitted parameters are saved in col(E) and fitted Y in col(F). The fitted curve is also plotted in the embedded graph.

Let’s click the green lock on col(E) and Select Change Parameters… to launch Set Column Values dialog. In Origin 2021,  we added Python Function panel besides Before Formula Scripts panel.  The Python functions defined inside Python Functions panel can be called by Labtalk scripts in Before Formula Scripts panel. Let’s see how it works.

In this example run_fit() function is pre-defined in Python Function panel. This function takes four inputs: x data, y data, initial parameter guesses, fitting function constants.  It  first creates a SumFitter fitting model, then fits the model to x,y data, lastly the function returns the fitted parameters and fitted Y values.

In the Before Formula Scripts tab, the py. prefixing indicates that Python function gets called. Worksheet columns are simply specified as inputs and outputs. 

What is also very nice is that the fitting process is saved in the worksheet: Go back to the worksheet, type  36 and 1 to  col(C)[5] and col(D)[5] (add one summation term) and fitting result gets updated.

Please also check a different example where the fitting function contains an infinite summation. Python package mpmath is used for calculating the summation. 

Set Column Value dialog is one of many places where we improved Python in Origin 2021. By showing this example, we hope you are able to make worksheet templates with your own Python code.  This can be a very powerful usage to incorporate Python analysis tools into Origin.

 

Leave a Reply

Your email address will not be published. Required fields are marked *