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
1
u/NomadNella Jan 29 '17
You have an extra comma in your second uppergamma function when defining F_plus_conv. Since Python does not evaluate code until it is executed you would not see the error until you hit the print statement.
2
u/NomadNella Jan 29 '17
If you put four spaces at the beginning of every code line it makes your post more readably. You can do this with multiple lines all at once by highlighting then then clicking on the <> button.