Advertisement
mikhail_dvorkin

SciPy interpolation, curve fit and optimization

Apr 21st, 2016
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.79 KB | None | 0 0
  1. import numpy as np
  2. from scipy import interpolate
  3. from scipy import optimize
  4. from matplotlib import pyplot
  5.  
  6. time = np.linspace(0, 1, 20)
  7. noise = (np.random.random(len(time)) - 0.5) * 0.3
  8. measures = 1.5 * np.sin(2 * np.pi * time) + noise
  9.  
  10. kinds = ['linear', 'nearest', 'zero', 'slinear', 'quadratic', 'cubic']
  11. points = np.linspace(0, 1, 100)
  12. pyplot.scatter(time, measures)
  13.  
  14. for kind in kinds:
  15.     li = interpolate.interp1d(time, measures, kind=kind)
  16.     # pyplot.plot(points, li(points), label=kind)
  17. #pyplot.legend()
  18.  
  19. def f(x, a, b):
  20.     return a * np.sin(b * x)
  21.  
  22. params, covariance = optimize.curve_fit(f, time, measures, [2, 6])
  23. print(params)
  24. g = lambda x: f(x, *params)
  25. pyplot.plot(time, g(time))
  26.  
  27. print(optimize.minimize_scalar(g, bounds=[0, 0.5], method="bounded"))
  28.  
  29. pyplot.show()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement