r/IPython • u/die_grinsekatze • Jan 29 '17
sympy: solving an equation-system with nsolve, including the upper gamma function
Hi, I'm trying to solve an equation-system with nsolve. One of the equations includes the upper gamma function, which has one of the variables as lower integral limit. When I run this I end up with an error massage :(
from sympy import *
p0, T0, n, g, a, F_s_1, F_s_2, F_i, k1, k2, D, t, t0, t_rc = symbols("p0 T0 n g a F_s_1 F_s_2 F_i k1 k2 D t t0 t_rc")
ß = a*(g - 1)/g
o = 5.67 * 10**-8
F_plus_conv = o * T0**4 * exp(D*t) * (exp(-D*t0) + 1/((D*t0)**(4*ß/n)) * (uppergamma(1 + 4*ß/n, D*t) - uppergamma(1 + 4*ß/n,D*t0)))
F_plus_rad = F_s_1/2 * (1 + D/k1 + (1 - D/k1) * exp(-k1*t)) + F_s_2/2 * (1 + D/k2 + (1 - D/k2) * exp(-k2*t)) + F_i/2 * (2 + D*t)
F_plus_t_rc = Eq(F_plus_conv, F_plus_rad)
T_t_rc = Eq(o * T0**4 * (t_rc/t0)**(4*ß/n), F_s_1/2* (1 + D/k1 + (k1/D - D/k1) * exp(-k1 * t_rc)) + F_s_2/2* (1 + D/k2 + (k2/D - D/k2) * exp(-k2 * t_rc)) + F_i/2 * ( 1+ D * t_rc))
titan = [(n, 0.75), (g, 1.4), (a, 0.77), (F_s_1, 1.5), (F_s_2, 1.1), (F_i, 0), (k1, 120), (k2, 0.2), (t, t_rc), (D, 1.6), (T0, 94), (p0, 1.5)]
print(nsolve((T_t_rc.subs(titan), F_plus_t_rc.subs(titan)), (t_rc, t0),(4.8,5.3)))Error msg:
Traceback (most recent call last): File "/home/.../nsolve_uppergammma_problem.py", line 14, in <module> print(nsolve((T_t_rc.subs(titan), F_plus_t_rc.subs(titan)), (t_rc, t0),(4.8,5.3)))
File "/usr/lib/python3.6/site-packages/sympy/solvers/solvers.py", line 2772, in nsolve x = findroot(f, x0, J=J, *kwargs)
File "/usr/lib/python3.6/site-packages/mpmath/calculus/optimization.py", line 928, in findroot fx = f(x0)
File "<string>", line 1, in <lambda> NameError: name 'uppergamma' is not definedHas anyone an idea, why this happens? When I try just an numeric solution for e.g uppergamma(3,x) = 7, nsolve works.
2
Upvotes
View all comments
Show parent comment
2
u/die_grinsekatze Feb 15 '17
Lambdify doesn't know, how to turn sympy's uppergamma function into an own function, because upper incomplete gammafunction is not explicit included. So you must use the equivalent of mpmath. Uppergamma.evalf uses mpmath.gammainc(z, a, inf) so I added the modules argument to nsolve:
For me it worked perfectly.