I have (x,y) dataset which is continuous and differentiable. The exact functional form is not known. I want to taylor expand the graph at some point. I have tried using algopy/Adipy. The problem is they demand the functional form.
I am attaching the sample code of algopy.
import numpy; from numpy import sin,cos
from algopy import UTPM
def f(x):
return sin(cos(x) + sin(x))
D = 100; P = 1
x = UTPM(numpy.zeros((D,P)))
x.data[0,0] = 0.3
x.data[1,0] = 1
y = f(x)
print('coefficients of y =', y.data[:,0])
where D is the order of polynomial.
I tried using the following (x1 and y1 are 1D arrays):
from scipy.interpolate import interp1d
f1 = interp1d(x1, y1, kind='cubic')
def f(x):
temp1=f1(x)
return np.float64(temp1)
However, interpolation does not seem to take in the data type of x returned by UTPM.
Error Message:
Traceback (most recent call last):
File "tay.py", line 26, in <module>
y = f(x)
File "tay.py", line 15, in f
temp1=f1(x)
File "/usr/lib/python2.7/dist-packages/scipy/interpolate/polyint.py", line 54, in __call__
y = self._evaluate(x)
File "/usr/lib/python2.7/dist-packages/scipy/interpolate/interpolate.py", line 449, in _evaluate
y_new = self._call(self, x_new)
File "/usr/lib/python2.7/dist-packages/scipy/interpolate/interpolate.py", line 441, in _call_spline
return spleval(self._spline, x_new)
File "/usr/lib/python2.7/dist-packages/scipy/interpolate/interpolate.py", line 919, in spleval
res[sl] = _fitpack._bspleval(xx,xj,cvals[sl],k,deriv)
TypeError: Cannot cast array data from dtype('O') to dtype('float64') according to the rule 'safe'