0

I'm trying to using sympy find the intersection of two functions. See below code.

import sympy

x = sympy.symbols('x')

f_x = x**-0.5

g_x = -1*sympy.log(0.1+x**-0.5, 10)

sol = sympy.solve(f_x - g_x, x)

num = float(sol[0])

print(num)

When I run this code, Python tells me there's no algebraic solution which is fine.

When I try to use nsolve, I have no luck either: "Cannot create mpf from x".

Can anyone assist me in using Python to solve this problem?

norok2
  • 25,683
  • 4
  • 73
  • 99

1 Answers1

1

If you upgrade to the recently released SymPy 1.11 then solve can solve this:

In [1]: import sympy
   ...: 
   ...: x = sympy.symbols('x')
   ...: 
   ...: f_x = x**-0.5
   ...: 
   ...: g_x = -1*sympy.log(0.1+x**-0.5, 10)

In [2]: solve(f_x - g_x, x)
Out[2]: [8.23999208447069]

If you use exact rational numbers then you can get an exact expression for that number in terms of the Lambert W function:

In [3]: solve(nsimplify(f_x - g_x), x)
Out[3]: 
⎡                   2               ⎤
⎢            100⋅log (10)           ⎥
⎢───────────────────────────────────⎥
⎢                                  2⎥
⎢⎛      ⎛10____        ⎞          ⎞ ⎥
⎣⎝- 10⋅W⎝╲╱ 10 ⋅log(10)⎠ + log(10)⎠ ⎦

In [4]: N(_[0])
Out[4]: 8.23999208447069

Your problem with nsolve is likely just that you are not using it correctly:

In [5]: nsolve(f_x - g_x, x, 1)
Out[5]: 8.23999208447069

Note the third argument which is an initial guess for the solution.

Oscar Benjamin
  • 12,649
  • 1
  • 12
  • 14
  • Updating to Sympy 1.11 worked. Using solve is perfect for my intents and purposes. Thank you Oscar for providing me with a solution. – Saul Seabrook Sep 07 '22 at 08:36