# Curve fitting using Python: LavenbergMarquardt Algorithm, numpy and matplotlib.

**
Suyog7130
** |
** **

**Description:**

Python libraries like numpy and scipy have many great functionalities that are routinely used by researchers all over the world for their computational needs. This present work is part of a similar research that I did in the first half of 2019. The research is titled 'Finding Evidence of Waldmeier Effect in Sun-like Stars'. And it principally is a data analysis project relying heavily on computer routines written in Python.

So, first of all, what is the Waldmeier Effect (WE)? It is widely known that stars like our Sun have strong magnetic field and that this field varies periodically with time. Now, it has been extensively studied for Solar Magnetic Cycles that the stronger cycles rise faster. This is known as the Waldmeier Effect. Our work is focused on finding the evidence of existence of the Waldmeier Effect in other stars. One of the prime motivations behind this is that if it can showed, even preliminarily, that some characteristic features of the Solar Cycle are visible in other stellar magnetic cycles as well, then it could be possible that the same dynamo mechanism operates in these stars.

Now, the original raw data is very irregular owing to rotations of the star and uneven observations. Thus, the original data in itself cannot be used for the analysis. So, we fit some appropriate profiles to the raw data. Now, since this fitting has to be done for each cycle separately, we need to isolate the cycles first.

This brings us to the start of the actual data analysis part. We bin the raw data with a time interval of one year to get the yearly median values. Then we do a 3-point running average smoothing so get monthly interpolated values in between the yearly medians. Cycles are isolated between two consecutive minima.

Based upon previous similar studies with respect to the Sun, we selected two profile functions to fit to the data, namely, the quasi-Planck fit and the skewed-Gaussian fit. And after proper fitting is obtained, we calculate the value of the Rise Rate and process to make a plot.

Coming to the Python routines now. We employ the scipy function curve_fit fitting the curves to the raw data. This function uses the trusted region reflective method with the LavenbergMarquardt Algorithm (LMA) to find the best fit parameters. The LMA does this by finding the least sum of squares of deviation from the original value. LMA was first proposed by More et al in 1985. The first heuristic values of the parameters are estimated based upon the interpolated curve. What the LMA does is find the most suited parameter values iteratively. The maximum number of iterations is 1200. Suppose this limit is reached before optimum values are found then we manually change the heuristic values a little and re-run the program.

Although the curve_fit function is at the heart of the analysis, numerous other Python libraries and functions are used. Some of these are listed below.

julian, datetime, decimal, numpy, scipy.optimize, scipy.interpolate, pandas, pdfkit, math, matplotlib.pyplot.

The slides of a presentation that I had the opportunity to give at a recent conference in Nepal are attached below. This is different from what I have in mind for PyCon, in which I would be focusing more on the programming aspect rather than the scientific outputs.

**Prerequisites:**

Core Python

**Content URLs:**

https://www.slideshare.net/secret/wUGkhcBQ1VutWW

**Speaker Info:**

Mr. Suyog Garg is currently an undergraduate student at IIITDM Kancheepuram, Chennai, India. In his sophomore year at the college, Mr. Suyog published his first paper in the proceedings of LAMSYS 2018, a prestigious national conference by ISRO. Mr. Suyog has more than one year of doing scientific computing using Python programming language. Before switching to Python, he used to work in C and Fortran. Mr. Suyog has been a participant of One Day School at IAUS 340, Jaipur. He recently attended the second International Conference on Application of Mathematics to Non-Linear Sciences at Pokhara, Nepal. He is currently the contributing editor of the institute magazine, apart from been associated with various volunteering causes like AIESEC.

**Speaker Links:**

Researchgate: https://www.researchgate.net/profile/Suyog_Garg

Blog: http://suyog20.blogspot.com/?m=1

LinkedIn: https://www.linkedin.com/in/suyog-garg-9443b611b