#!/usr/bin/python # using a demo file for exponential fitting #http://stackoverflow.com/questions/3938042/fitting-exponential-decay-with-no-initial-guessing import numpy as np from scipy.optimize import curve_fit import matplotlib.pyplot as plt #Define your function def func(x, a, b, c): return a*np.exp(-b*x) + c #create a dummy dataset x = np.linspace(0,4,50) #create the 'ideal' negative exponential curve, using the function y = func(x, 2.5, 1.3, 0.5) #the dummy data, with some noise thrown in yn = y + 0.2*np.random.normal(size=len(x)) # Using the python scipy function curve_fit with function and input variables popt, pcov = curve_fit(func, x, yn) print popt.size print pcov.size f1=plt.plot(x,yn,x, func(x,popt[0],popt[1],popt[2])) plt.title('curve fit') plt.xlabel('x-vals') plt.ylabel('y-vals') plt.show()