Skip to content

Instantly share code, notes, and snippets.

@Foadsf
Last active May 6, 2018 14:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Foadsf/da7dab3a75f8a9cab275473230264a4c to your computer and use it in GitHub Desktop.
Save Foadsf/da7dab3a75f8a9cab275473230264a4c to your computer and use it in GitHub Desktop.
custom functionfit in libreoffice calc
x y
10 0.197084039
30 0.182641149
50 0.179242834
100 0.203031108
150 0.180092409
200 0.163100779
300 0.163100779
400 0.169897437
600 0.169897437
800 0.14186123
1000 0.172446176
1400 0.152905792
1800 0.155454546
set datafile separator ","
set xrange [0:1800]
set yrange [0.14:0.2]
a=0.0633
b=0.005092
c=0.155
f(x)=a*exp(-b*x)+c
fit f(x) 'data.csv' u 1:2 via a,b,c
plot 'data.csv' using 1:2 title 'data' with lines,\
f(x) title sprintf('%.2f *e^(- %.2f x)+%.2f', a,b,c)
pause -1
import numpy as np
data=np.loadtxt(open("data.csv", "rb"), delimiter=",", skiprows=1)
from scipy.optimize import curve_fit
def func(x, a, b, c):
return a * np.exp(-b * x) + c
popt, pcov = curve_fit(func, data[:,0], data[:,1],p0=[0.0633,0.005092,0.155])
import matplotlib.pyplot as plt
plt.plot(data[:,0],data[:,1],label='data')
plt.plot(data[:,0], func(data[:,0], *popt), 'r-', label='fit: a=%5.3f, b=%5.3f, c=%5.3f' % tuple(popt))
plt.legend()
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment