solving an equation-system with nsolve, including the upper gamma function

72 views
Skip to first unread message

Max Winkler

unread,
Feb 13, 2017, 11:49:38 AM2/13/17
to sympy

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 defined

Aaron Meurer

unread,
Feb 13, 2017, 3:12:23 PM2/13/17
to sy...@googlegroups.com
It looks like lambdify doesn't know how to turn uppergamma into mpmath
functions, since mpmath doesn't have uppergamma directly. Looks like
uppergamma.evalf uses mpmath.gammainc(z, a, inf), so it should be
using that. You can workaround this by passing the modules argument to
nsolve:

nsolve(..., modules=['mpmath', {'uppergamma': lambda s, x:
mpmath.gammainc(s, x, mpmath.inf)}])

I opened an issue for this
https://github.com/sympy/sympy/issues/12173. It should be easy to fix,
if you want to give it a shot.

Aaron Meurer
> --
> You received this message because you are subscribed to the Google Groups
> "sympy" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to sympy+un...@googlegroups.com.
> To post to this group, send email to sy...@googlegroups.com.
> Visit this group at https://groups.google.com/group/sympy.
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/sympy/75a1cba4-72ce-4318-bb7d-513268ac065b%40googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
Message has been deleted

Max Winkler

unread,
Feb 15, 2017, 12:03:13 PM2/15/17
to sympy
Thank you, it works fine now. Sorry for my late answer, I was quite busy in the last days. 
Reply all
Reply to author
Forward
0 new messages