from mathutils import deriv, interpolate, fit_polynomial, PolynomialFunction, get_values

        
start = 0
end = 10
step_size = 0.1

result= lscan(out,sin,start,end,[step_size,],0.01)
readable = result.getReadable(0)
positions = result.getPositions(0)

pars_polynomial = (a0, a1, a2, a3, a4, a5, a6) = fit_polynomial(readable, positions, 6)
fitted_polynomial_function = PolynomialFunction(pars_polynomial)

resolution = step_size/100
fit_polinomial = []
for x in frange(start,end,resolution, True):
    fit_polinomial.append(fitted_polynomial_function.value(x))
x = frange(start, end+resolution, resolution)

d = deriv(fitted_polynomial_function,x)

d2 = deriv(readable,positions)
f = interpolate(readable,positions)
f2 = interpolate(readable,positions, "cubic")


plot([get_values(f,frange(0,10,0.01)), get_values(f2,frange(0,10,0.01))], ["f", "f2"], title = "aux")
plots = plot([readable, fit_polinomial, d, d2] , ["data", "polinomial", "deriv", "d2"], xdata = [positions,x,x, positions], title="Data")
