Search code examples
pythontypeerrorsympydifferential-equations

TypeError: unsupported operand type(s) for *: 'float' and 'Equality'


I am getting an error of

TypeError: unsupported operand type(s) for *: 'float' and 'Equality' at "Jn=(q*E*muN*N_at_0)".

How can I perform multiplication of <class 'sympy.core.relational.Equality'> with <class 'float'>?

import sympy as sp
from sympy import *
import math
x=sp.Symbol('x')
n=sp.Function('n')(x)
p=sp.Function('p')(x)
t=200*10**(-7)
q=1.60217662*10**(-19)
k=1.38065*10**(-23)
T=300.00
Vbi=1
V=0
alpha=5.7*10**4
N0=1.71*10**17
G=alpha*N0
E=(V-Vbi)/t
muN=1
muP=1
Vt=k*T/q
Dn=muN*Vt
Dp=muP*Vt
Nc=2.2*10**(20)
Nv=Nc
Nd=3*10**(19)
Na=10**(18)
Eg=1.55
n_t = Nc*math.exp(-Eg/Vt)
p_0 = Nc*math.exp(-Eg/Vt)
Nsol=sp.Eq(Dn*n.diff(x,x)+(muN*E)*n.diff(x)+G,0)    
Psol=sp.Eq(Dp*p.diff(x,x)-(muP*E)*p.diff(x)+G,0)
ics1={n.subs(x,0):Nd, n.subs(x,t):n_t}
ics2={p.subs(x,0):p_0, p.subs(x,t):Na}
ivN=sp.dsolve(Nsol, ics=ics1)
ivP=sp.dsolve(Psol, ics=ics2)
N_at_0=ivN.subs(x,0)
P_at_0=ivP.subs(x,0)
Jn=(q*E*muN*N_at_0)
Jp=(q*E*muP*P_at_0)

Error:

TypeError                                 Traceback (most recent call last)
<ipython-input-27-3634a48ae130> in <module>()
     35 N_at_0=ivN.subs(x,0)
     36 P_at_0=ivP.subs(x,0)
---> 37 Jn=(q*E*muN*N_at_0)
     38 Jp=(q*E*muP*P_at_0)

Solution

  • I like to see intermediate results, especially in sympy calculations. It's easier to read and understand. Otherwise I have to run the code myself (not always possible).

    In [4]: n_t = Nc*math.exp(-Eg/Vt)
       ...: p_0 = Nc*math.exp(-Eg/Vt)
    In [5]: n_t
    Out[5]: 2.0118139286850867e-06
    
    In [6]: Nsol=sp.Eq(Dn*n.diff(x,x)+(muN*E)*n.diff(x)+G,0)
       ...: Psol=sp.Eq(Dp*p.diff(x,x)-(muP*E)*p.diff(x)+G,0) 
    In [7]: Nsol
    Out[7]: 
                                              2                      
              d                              d                       
    - 50000.0⋅──(n(x)) + 0.0258520187368606⋅───(n(x)) + 9.747e+21 = 0
              dx                              2                      
                                            dx                       
    
    In [8]: ics1={n.subs(x,0):Nd, n.subs(x,t):n_t}
       ...: ics2={p.subs(x,0):p_0, p.subs(x,t):Na}
    In [9]: ics1
    Out[9]: {n(0): 30000000000000000000, n(2.0e-5): 2.0118139286850867e-06}
    
    In [10]: ivN=sp.dsolve(Nsol, ics=ics1)
        ...: ivP=sp.dsolve(Psol, ics=ics2)
    In [11]: ivN
    Out[11]: 
                                            1934084.9358394⋅x          
    n(x) = 1.9494e+17⋅x - 476.291693838697⋅ℯ                  + 3.0e+19
    
    In [12]: N_at_0=ivN.subs(x,0)
        ...: P_at_0=ivP.subs(x,0)
    In [13]: N_at_0
    Out[13]: n(0) = 3.0e+19
    
    In [14]: type(_)
    Out[14]: sympy.core.relational.Equality
    

    So N_at_0 is a Equality object - which is what the error complains about:

    In [15]: Jn=(q*E*muN*N_at_0)
    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <ipython-input-15-28d29a3734cf> in <module>
    ----> 1 Jn=(q*E*muN*N_at_0)
    
    TypeError: unsupported operand type(s) for *: 'float' and 'Equality'
    

    But we can get the numeric part of the Eq with .rhs:

    In [24]: N_at_0.rhs
    Out[24]: 3.00000000000000e+19
    
    In [25]: Jn=(q*E*muN*N_at_0.rhs)
    
    In [26]: Jn
    Out[26]: -240326.493000000