1

I get a little problem with my project because I have a set of data, I plot it in order to get 2 curves and I would like to fit this plots by an exponential curve.

I watched this post : fitting exponential decay with no initial guessing. But my example is kind different.

This is what I'm getting with data :

enter image description here

My script is as following :

mask_G = np.bitwise_and( tbdata['G'] < 99.99, tbdata['GERR'] < 0.2)
mask_R = np.bitwise_and( tbdata['R'] < 99.99, tbdata['RERR'] < 0.2)

G_corrected = tbdata[mask_G]
R_corrected = tbdata[mask_R]


fig13 = plt.gcf()
fig13.set_size_inches(16, 9)


fig13, (ax1,ax2) = plt.subplots(1,2)

fig_error_g = ax1.plot(G_corrected['G'], G_corrected['GERR'], '.')
ax1.set_xlabel('G')
ax1.set_ylabel('GERR')
ax1.set_title('Evolution de GERR en fonction de G')

fig_error_r = ax2.plot(R_corrected['R'], R_corrected['RERR'], '.')
ax2.set_xlabel('R')
ax2.set_ylabel('RERR')
ax2.set_title('Evolution de RERR en fonction de R')

fig13.tight_layout() 

plt.savefig('graphique.png')

plt.show()

I tried to write that, which based on scipy doc :

def exponential(x,a,b,c) :
    return a * np.exp(-b * x) + c

xdata = G_corrected['G']
y = G_corrected['GERR']
ydata = y + 0.2 * np.random.normal(size=len(xdata))

popt, pcov = curve_fit(exponential, xdata, ydata)

but I get :

/home/user/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/scipy/optimize/minpack.py:601: OptimizeWarning: Covariance of the parameters could not be estimated
category=OptimizeWarning)

Do you have any idea on how I can process ?

Thank you so much ;)

EDIT :

I tried to fit my plot like that :

mask_G = np.bitwise_and( tbdata['G'] < 99.99, tbdata['GERR'] < 0.2)
mask_R = np.bitwise_and( tbdata['R'] < 99.99, tbdata['RERR'] < 0.2)

G_corrected = tbdata[mask_G]
R_corrected = tbdata[mask_R]


params = np.polyfit(G_corrected['G'], np.log(G_corrected['GERR']),1)
a = params[0]
A = np.exp(params[1])


fig13 = plt.gcf()
fig13.set_size_inches(16, 9)


fig13, (ax1,ax2) = plt.subplots(1,2)

fig_error_g = ax1.plot(G_corrected['G'], (G_corrected['GERR']), '.')
fig_error_g = ax1.plot(G_corrected['G'], (A*np.exp(a*G_corrected['G'])),'.')

ax1.set_xlabel('G')
ax1.set_ylabel('GERR')
ax1.set_title('Evolution de GERR en fonction de G')

fig_error_r = ax2.plot(R_corrected['R'], np.log(R_corrected['RERR']), '.')
ax2.set_xlabel('R')
ax2.set_ylabel('RERR')
ax2.set_title('Evolution de RERR en fonction de R')

fig13.tight_layout() 

plt.savefig('graphique.png')

plt.show()

and I get :

enter image description here

What do you think about the result ?

Community
  • 1
  • 1
Essex
  • 6,042
  • 11
  • 67
  • 139

1 Answers1

4

easiest way is to apply logarithmic scaling to your plot. As you certainly know log(exp(x)) = x, i.e. if you apply log() to your y-values and plot that you should get a linear plot. Once you have that, you can fit it with your linear toolbox (Gaussian Least Square method). The resulting slope is the prefactor in exp(ax), which you try to obtain.

If you have another dependency on the x-axis, it might be beneficial to make a log-log plot of your data to figure out all dependencies.

Frank
  • 406
  • 2
  • 13
  • Thank you to your answer. As you said, I applied logarithmic scaling in my plot on my y-values (i.e question edited). Now, I need to use `curve_fit` from scipy ? – Essex Apr 25 '16 at 08:32
  • I would use linear fit on the log data: http://docs.scipy.org/doc/scipy-0.14.0/reference/generated/scipy.stats.linregress.html - Can you plot your data on a logarithmic scale to see if it is linear on that scale (your edit)? Maybe log-log scaling, i.e. both axis should do it. – Frank Apr 25 '16 at 12:17