Skip to content

Instantly share code, notes, and snippets.

@ikautak
Created February 14, 2013 14:16
Show Gist options
  • Save ikautak/4953095 to your computer and use it in GitHub Desktop.
Save ikautak/4953095 to your computer and use it in GitHub Desktop.
scipy leastsq sample.
#!/usr/bin/env python
import numpy as np
from scipy.optimize import leastsq
def fit_func(parameter, x, y):
a = parameter[0]
b = parameter[1]
residual = y - (a*x*x + b)
return residual
px = [-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5]
py = [25.5, 16.9, 9.7, 4.3, 2.1, 1.02, 1.98, 4.9, 9.9, 15.8, 25.7]
px = np.array(px)
py = np.array(py)
parameter0 = np.array([0, 0])
result = leastsq(fit_func, parameter0, args=(px, py))
print result[0]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment