import numpy as np
from scipy import interpolate
from scipy import optimize
from matplotlib import pyplot
time = np.linspace(0, 1, 20)
noise = (np.random.random(len(time)) - 0.5) * 0.3
measures = 1.5 * np.sin(2 * np.pi * time) + noise
kinds = ['linear', 'nearest', 'zero', 'slinear', 'quadratic', 'cubic']
points = np.linspace(0, 1, 100)
pyplot.scatter(time, measures)
for kind in kinds:
li = interpolate.interp1d(time, measures, kind=kind)
# pyplot.plot(points, li(points), label=kind)
#pyplot.legend()
def f(x, a, b):
return a * np.sin(b * x)
params, covariance = optimize.curve_fit(f, time, measures, [2, 6])
print(params)
g = lambda x: f(x, *params)
pyplot.plot(time, g(time))
print(optimize.minimize_scalar(g, bounds=[0, 0.5], method="bounded"))
pyplot.show()
Comments