Skip to content

Instantly share code, notes, and snippets.

@Nil-Zil Nil-Zil/curve_fit.py
Created Aug 22, 2017

Embed
What would you like to do?
import numpy as np
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
def func(x, a, b, c):
return a * np.exp(-b * x) + c
x_data = np.linspace(0, 10, 20)
y_data = [1.005530886735986, 0.36271009697000134, 0.10282751652440644, 0.03638464925638259, 0.0177328764419975, 0.007038622747455951, -0.011725650107022643, 0.017847156954980542, -0.0006555003543666825, 0.021014391787336193, -
0.0041821789264064055, -0.003770619380216366, -0.010447019521175183, 0.007990387331388177, 0.008399550924641605, -0.007744658211731844, -0.001971923782799532, -0.0038740290211056695, 0.01347986832021233, -0.017588388303206814]
popt, pcov = curve_fit(func, x_data, y_data)
a, b, c = popt
plt.plot(x_data, [a * np.exp(-b * x) + c for x in x_data], x_data, y_data)
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.