I'm trying to determine the entropy of adsorbed hydrogen gas, leaving the temperature "T" as a symbolic variable in Python using the sympy library, and am running into issues as a result
Originally, the process was completed numerically, which required my use of numpy to create a list of floating-point numbers. When I later decided to have the temperature be a variable, rather than a fixed value, I employed sympy to accomplish this. I get the following error as a result, relating to my use of a negative number within the exponent. Any advice would be appreciated. See code beloW:
import numpy as np
import sympy as sym
c = 3e8
h = 6.626e-34
k = 1.38e-23
T = sym.symbols('T')
v = np.array([3034.1, 3027, 1265.8, 1257.5, 840.6, 642.7])
f=v*c*100
Q = sym.exp(-(h * f)/(2 * k * T))/(1 - (sym.exp(-(h * f))/(k * T)))
print(Q)
You need to be clear when mixing numpy and sympy where you are using numpy arrays and where you are using symbolic expressions. Here is some code that does that:
import numpy as np
import sympy as sym
c = 3e8
h = 6.626e-34
k = 1.38e-23
T = sym.symbols('T')
# We will substitute values from v_arr in place of v_sym
v_sym = sym.symbols('v')
v_arr = np.array([3034.1, 3027, 1265.8, 1257.5, 840.6, 642.7])
# Make a symbolic expression involving v_sym:
f = v_sym*c*100
Q = sym.exp(-(h * f)/(2 * k * T))/(1 - (sym.exp(-(h * f))/(k * T)))
# Substitute values from the numpy array into the symbolic expression:
Q_arr = [Q.subs(v_sym, v_val) for v_val in v_arr]
print(Q_arr)
Someone who understands the code will immediately spot something: there is absolutely no benefit in using numpy here and v_arr
might as well be a list. You could also use a sympy Matrix instead of a numpy array but at least for the code as shown it does not offer any particular benefit.