Alexander Whatley Alexander Whatley - 1 year ago 132
Python Question

Solve for roots in given interval using scipy.optimize

I have the function

f1 = lambda x: 1 - 1.12 * (x ** 0.5) * ((1-x) ** 0.02)
, and I wish to solve for its roots in the interval (0, 1). I have tried using the
to do this, but both methods run into issues. Based on some experimentation, I got that the roots of this equation are approximately equal to 0.86322414 and 0.9961936895432034 (we know there are at most 2 roots because the function has one inflection point in this interval):

f1 = lambda x: 1 - 1.12 * (x ** 0.5) * ((1-x) ** 0.02)
print(fsolve(f1, 0.5))
print(brentq(f1, 0.99, 0.999))


[ 0.86322414]

The issue here is that in order for brentq to work, the values of the function must be of opposite signs at the specified endpoints. Furthermore, when I started fsolve at values of
close to 1, I got runtime warning messages:

print(fsolve(f1, 0.97))
print(fsolve(f1, 0.98))


[ 0.97]
[ 0.98]
C:/Users/Alexander/Google Drive/Programming/Projects/Root Finding/ RuntimeWarning: invalid value encountered in power

C:\Users\Alexander\Anaconda3\lib\site-packages\scipy\optimize\ RuntimeWarning: The iteration is not making good progress, as measured by the
improvement from the last ten iterations.

Does anyone if there is a more systematic way to solve for roots of this equation, and why fsolve is not working on
x = 0.97, 0.98

Answer Source

If you take the derivative of your function and set it equal to 0, after a little algebra you'll find that the derivative is 0 at x0 = 0.5/0.52. (In a calculus class, this point is called a critical point, not an inflection point.) The function has a minimum at this point, and the value there is negative. The values at x=0 and x=1 are positive, so you can use [0, x0] and [x0, 1] as bracketing intervals in brentq:

In [17]: from scipy.optimize import brentq

In [18]: f1 = lambda x: 1 - 1.12 * (x ** 0.5) * ((1-x) ** 0.02)

In [19]: x0 = 0.5/0.52

In [20]: brentq(f1, 0, x0)
Out[20]: 0.8632241390303161

In [21]: brentq(f1, x0, 1)
Out[21]: 0.9961936895432096
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download