Python Forum
how to get x values based on y-axis values from curvefit function - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: how to get x values based on y-axis values from curvefit function (/thread-21181.html)



how to get x values based on y-axis values from curvefit function - python_newbie09 - Sep-18-2019

Hi,

I have fitted a function for extrapolation and now I would like to get the x value when y is of a certain value. How do I get this?

def func(x, b1, b2):
    return b1*np.exp(-b2*(x))

x = np.array([47,77,103,129,152,177,207,234,255,282,311,337,358,382,416,440,463,490,512,535,566,573])
y = np.array([0.904996,0.946489,0.931208,0.930137,0.915004,0.92138,0.911744,0.908241,0.894834,0.895659,0.897973,0.885373,0.895916,0.883586,0.880162,0.857255,0.872408,0.87055,0.855421,0.854406,0.852419,0.853073])

popt, pcov = curve_fit(func, x, y, p0=[0.1,0.1])

plt.plot(x, y, 'x')
xx = np.linspace(0, 2000, 100)
yy = func(xx, *popt)
plt.plot(xx, yy, lw=1)
Thanks.


RE: how to get x values based on y-axis values from curvefit function - scidam - Sep-19-2019

This problem can be solved in many ways, e.g. consider the following:

from scipy.optimize import root_scalar
root_scalar(lambda x: func(x, *popt) - your_y_value, bracket=[0, 10000], method='bisect')