Fit erf function to data
<p>So I have hysteresis loop. I want to use the erf function to fit it with my data.</p>

<p>A portion of my loop is shown in black on the lower graph.</p>

<p>I am trying to use the <a href="https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.curve_fit.html" rel="nofollow"><code>scipy.optimize.curve_fit</code></a> and <code>scipy.special.erf</code> function to fit the data with the following code:</p>

<pre><code>import scipy.special
import scipy.optimize

def erfunc(x,a,b):
    return mFL*scipy.special.erf((x-a)/(b*np.sqrt(2)))

params,extras = scipy.optimize.curve_fit(erfunc,x_data,y_data)

x_erf = list(range(-3000,3000,1))
y_erf = erfunc(x_erf,params[0],params[1])
</code></pre>

<p><code>mFL</code> is a constant, <code>a</code> controls the position of the erf curve and <code>b</code> the slope of the curve. (To my knowledge)</p>

<p>However, when I plot the obtained x_erf and y_erf data (in blue). I get the following fitting, which is not ideal to say the least:</p>

<p><img src="http://i.stack.imgur.com/BE4tM.png" alt="Data with erf fitting plot"></p>

<p>Is there a way i can get a proper fit?</p>

<p>Edit:
Link to data file: <a href="https://www.dropbox.com/s/o0uoieg3jkliun7/xydata.csv?dl=0" rel="nofollow">https://www.dropbox.com/s/o0uoieg3jkliun7/xydata.csv?dl=0</a>
Params[0] = 1.83289895, Params<a href="http://i.stack.imgur.com/LdUv0.png" rel="nofollow">1</a> =  0.27837306</p>

<p>I suspect two things are needed for a good fit here. First, I believe you need to add <code>mFL</code> to your <code>erfunc</code> function, and second, as suggested by Glostas, you need to specify some initial guesses for your fitting parameters. I created some artificial data in an attempt to replicate your data. The plot on the left is before giving <code>curve_fit</code> some initial parameters and the plot on the right is after.</p>

<p><a href="http://i.stack.imgur.com/iR16A.png" rel="nofollow"><img src="http://i.stack.imgur.com/iR16A.png" alt="enter image description here"></a></p>

<hr>

<p>Here is the code to reproduce the above plots</p>

<pre><code>import numpy as np
from scipy.special import erf
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt


def erfunc(x, mFL, a, b):
    return mFL*erf((x-a)/(b*np.sqrt(2)))

x_data  = np.linspace(-3000, 3000, 100)

mFL, a, b = 0.0003, 500, 100

y_data  = erfunc(x_data, mFL, a, b)
y_noise = np.random.rand(y_data.size) / 1e4
y_noisy_data = y_data + y_noise

params, extras = curve_fit(erfunc, x_data, y_noisy_data)
# supply initial guesses to curve_fit through p0 arg
superior_params, extras = curve_fit(erfunc, x_data, y_noisy_data,
                                    p0=[0.001, 100, 100])

fig = plt.figure()
ax1 = fig.add_subplot(121)
ax2 = fig.add_subplot(122)

ax1.plot(x_data, erfunc(x_data, *params))
ax1.plot(x_data, y_noisy_data, 'k')
ax1.set_title('Before Guesses')

ax2.plot(x_data, erfunc(x_data, *superior_params))
ax2.plot(x_data, y_noisy_data, 'k')
ax2.set_title('After Guesses')
</code></pre>

python python-3.x numpy scipy