Ji Chul
Ji Chul

Reputation: 17

Sympy runs forever trying to solve this equation involving a sigmoid-like function

My Jupiter notebook is running and running. It doesn't give me the answer. I don't know the reason. Anything you could do for me would be highly appreciated.

import sympy as sp

X, T = sp.symbols('X, T')
K = 100000*sp.exp(-33.78*(T-298)/T)
eq1 = sp.Eq(X, K/(1+K))
eq2 = sp.Eq(X**2, 0.0025*(T-300))
R = sp.solve((eq1,eq2), (X, T))
print(R)

Upvotes: 0

Views: 181

Answers (2)

smichr
smichr

Reputation: 19093

If you take the time to make the plot you may also use the coordinate of the intersection as your initial guess to nsolve:

>>> from sympy import nsolve
>>> nsolve((eq1, eq2), (X, T), (.6, 400))
Matrix([
[0.59998683688158],
[443.993681772465]])

Upvotes: 0

JohanC
JohanC

Reputation: 80409

Here is a possible approach for tackling the equations. First, make a plot of both curves. The second curve can be written as a square root, if we take into account that negative outcomes also would apply.

Matplotlib and numpy can handle this. For small values of T the argument of exp gets too high and causes overflow for the first equation. But the second equation clearly needs T to be ≥ 300 to avoid roots of negative numbers. So, the curves can start at T == 300.

from matplotlib import pyplot as plt
import numpy as np

T = np.linspace(300, 1000, 1000)
K = 100000 * np.exp(-33.78 * (T - 298) / T)
plt.plot(T, K / (1 + K), label='$K/(1+K)$')
plt.plot(T, np.sqrt(0.0025 * (T - 300)), label='$\\sqrt{0.0025(T-300)}$')
plt.legend()
plt.show()

Which plots:

resulting plot

The orange curve starts at 0 for T == 300 and keeps growing. The blue curve is sigmoid like, starting near 1, making a turn roughly for T between 400 and 500 and then stays close to 0.

This suggests an equality around T == 440, which can be plugged into sympy's numerical solver nsolve:

import sympy as sp

X, T = sp.symbols('X, T')
K = 100000 * sp.exp(-33.78 * (T - 298) / T)

sp.nsolve(sp.Eq(K / (1 + K), sp.sqrt(0.0025 * (T - 300))), T, 440)

Result: 443.993681772465

Upvotes: 2

Related Questions