Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy's interp function - how to find a value of x for a given value of y?

So I have an array of values of x (in increasing order) and the corresponding y values. Numpy's interp function takes in the X value, and the x and y arrays. How do I get the value of X for a given value of Y?? Eg if y = 0, x = ?.

Cheers!

code:

j = (((1840/(2*pi))**0.5)*exp(phi)) - 1.0 #y axis, phi is a 1-D array
t = linspace(0, 40, 100) #x axis
a = interp(<x-value>,t,j) # this gives me the corresponding y value! 

So what should I do to get the x value for a given y value!!

like image 608
Omkar Myatra Avatar asked Oct 21 '25 06:10

Omkar Myatra


1 Answers

y_interp = np.interp(x_interp, x, y) yields an interpolation of the function y_interp = f(x_interp) based on a previous interpolation y = f(x), where x.size = y.size, x_interp.size = y_interp.size.

If you want x as a function of y, you have to construct the inverse function. As @NPE indicated, you have to make sure that x and y are always increasing. An easy way to check this is to use

np.all(np.diff(x) > 0)
np.all(np.diff(y) > 0)

Now finding the inverse function is actually very simple: you have to reverse the roles of x and y (since we deal with interpolations).

With my notations: x_value = np.interp(y_value, x, y).

With your notations: x_value = interp(y_value, j, t)

like image 123
Flavian Hautbois Avatar answered Oct 23 '25 20:10

Flavian Hautbois



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!