简体   繁体   中英

How to fit data by exponential curve

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 :

在此输入图像描述

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 :

在此输入图像描述

What do you think about the result ?

easiest way is to apply logarithmic scaling to your plot. As you certainly know log(exp(x)) = x, ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM