Skip to content

Instantly share code, notes, and snippets.

@smurfix
Last active December 17, 2015 23:59
Show Gist options
  • Save smurfix/5693509 to your computer and use it in GitHub Desktop.
Save smurfix/5693509 to your computer and use it in GitHub Desktop.
import numpy
from scipy.optimize import curve_fit
import matplotlib.pyplot as plt
import math
# Define test data.
points = numpy.float64([ 0., .17, .285, .365, .425, .47, .505, .532, .553, .571, .586 ])
xpos = numpy.float64(range(11))
# Define model function to be used to fit to the data above:
def mitigation(x, *p):
a,b = p
res = a*numpy.arctan(x/b)
return res
def miti2(x, *p):
a,b = p
res = a*numpy.log(x/b+1)
return res
# p0 is the initial guess for the fitting coefficients (A, mu and sigma above)
p0 = [.5,3.]
coeff, var_matrix = curve_fit(mitigation, xpos, points, p0=p0)
coeff2, var_matrix = curve_fit(miti2, xpos, points, p0=p0)
print "atan",coeff ## reasonably close to 4/9 and e
print "log",coeff2
# Get the fitted curve
fit = mitigation(xpos, *coeff)
fit2 = miti2(xpos, *coeff2)
plt.plot(xpos, points, label='Test data')
plt.plot(xpos, fit, label='Fitted data')
plt.plot(xpos, fit2, label='log')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment