在定积分范围内使用符号

时间:2019-04-22 19:26:03

标签: numpy scipy physics sympy

我想在我的定积分中使用极限(Ef-e * V)和Ef作为上下极限。其中Ef给出10电子伏特,e是电荷,我想用V表示ans 我该怎么办??? 请帮助


    from scipy.integrate import quad
    import sympy as sp
    import math

    e = 1.6 * (10 ** -19)
    L = 10 ** -9
    h = 6.626 * (10 ** -34)
    h_cut = 1.05 * (10 ** -34)
    m = 9.11 * (10 ** -31)
    V0 = 4.0*e  # in J
    EF = 10.0*e  # in J
    E = sp.Symbol('E')
    V = sp.Symbol('V')



    def f(E):
        j = (4 * E * (V0 - E)) / (4 * E * (V0 - E) + V0 ** 2 * ((2 * m * (V0 - E)) * ((L / h_cut) ** 2)))
        return j


    i, err = quad(f, EF - e * V, EF)
    print('i= ', i)
    I = (2 * e * i) / h
    print(I)
    ```
THE error is as follows:
Traceback (most recent call last):
  File "C:/Users/Subham/Desktop/Integration/integration.py", line 22, in <module>
    i, err = quad(f, EF - e * V, EF)
  File "C:\Users\Subham\AppData\Local\Programs\Python\Python37-32\lib\site-packages\scipy\integrate\quadpack.py", line 337, in quad
    flip, a, b = b < a, min(a, b), max(a, b)
  File "C:\Users\Subham\Desktop\Integration\venv\lib\site-packages\sympy\core\relational.py", line 304, in __nonzero__
    raise TypeError("cannot determine truth value of Relational")
TypeError: cannot determine truth value of Relational

1 个答案:

答案 0 :(得分:0)

由于您使用的是SymPy,因此可以在那里进行积分。但是,如果您使用有理数,那将是最好的选择,所以我们这样做

>>> gx = nsimplify(f(x), rational=True)

然后计算积分

>>> i = integrate(gx, (x,EF-e*V,EF))

并以您希望的精度显示它。这是每个数字2个sigfig的结果:

>>> nfloat(i, 2)
1.6e-19*V + 1.7e-17*log(1.9e-17 - 1.6e-19*V) + 6.5e-16