I have data z
sampled from a 2D function f
at grid points x, y
, as in z = f(x, y)
.
It is easy to interpolate f
with scipy.interp2d
via f = interp2d(x, y, z
).
However, evaluating f(x, y)
returns an entire 2D grid as if I had done
xx, yy = np.meshgrid(x, y)
f(xx, yy)
The behaviour I want is to simply return the values [f(x[i], y[i]) for i in range(len(x))]
, which I believe is the behaviour for pretty much any other method in numpy.
The reason I want this behaviour is that I'm looking for the path traced out along the surface of f
over "time" by the pair (t, u(t))
.
It is also surprising that np.diag(f(t, u(t)))
differs from np.array([f(ti, u(ti)) for ti in t])
, so It's not clear to me how to get at the path f(t, u(t))
from what is returned via interp2d
.
EDIT: About diag
, I just thought it seemed we should have np.diag(f(t, u(t))) == np.array([f(ti, u(ti)) for ti in t])
, but that isn't the case.
Complete example:
def f(t, u):
return (t**2) * np.exp((u**2) / (1 + u**2))
x = np.linspace(0, 1, 250)
xx, yy = np.meshgrid(x, x)
z = f(xx, yy)
f = scipy.interpolate.interp2d(x, y, z)
print(f(x, y))
print(np.array([f(xi, yi)[0] for xi, yi in zip(x, y)]))
I would like the output of both print
statements to be the same.
See Question&Answers more detail:
os