试图在那个等式中求解B

时间:2018-05-24 16:52:09

标签: python numpy sympy solver

我试图在那个长表达式中解决变量B.它给了我一个错误,它无法转换为浮点数。我不确定这究竟意味着什么以及我需要做些什么来修复它并获得B的值。我是Python的新手,所以任何帮助都会受到赞赏。

代码:

import matplotlib.pyplot as plt
import numpy as np
import math
from sympy.solvers import solve
from sympy import Symbol 

W = 1*(10**(-4)) 
mu = 300  
Cox = 6.906*(10**-6)
Leff = 1*(10**(-4))
Vg1 = 0.5
Vg2 = 1.0
Vg3 = 1.5
Vg4 = 2.0
Vth = 0.0259
tsi = 5 * (10 ** -7)
Vg = [0.5, 1, 1.5, 2]
Esi = 11.7
tox = 1.5 * (10 ** -7)
tsi = 5 * (10 ** -7)
ni = 1.5 * (10 ** 10)
q = 1.602 * (10 ** -19)

B = Symbol('B')

solve((Vg1/2*Vth) - math.log((2/tsi)*(np.sqrt((2*Esi*Vth)/(ni * q)))) - 
math.log(B) + math.log(np.cos(B)) - 2*11.7*(tox)/(3.9 * tsi), B)

错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-3-003cda8dedbd> in <module>()
 18 B = Symbol('B')
 19 
 ---> 20 solve((Vg1/2*Vth) - math.log((2/tsi)*(math.sqrt((2*Esi*Vth)/(ni * 
  q)))) - math.log(B) + math.log(math.cos(B)) - 2*11.7*(tox)/(3.9 * tsi), B)

~\Anaconda3\lib\site-packages\sympy\core\expr.py in __float__(self)
237         if result.is_number and result.as_real_imag()[1]:
238             raise TypeError("can't convert complex to float")
 --> 239         raise TypeError("can't convert expression to float")
240 
241     def __complex__(self):

TypeError: can't convert expression to float

1 个答案:

答案 0 :(得分:0)

根据@jdehesa的建议使用sympy表达式:

from sympy.solvers import nsolve
from sympy import Symbol, log, sqrt, cos

W = 1*(10**(-4)) 
mu = 300  
Cox = 6.906*(10**-6)
Leff = 1*(10**(-4))
Vg1 = 0.5
Vg2 = 1.0
Vg3 = 1.5
Vg4 = 2.0
Vth = 0.0259
tsi = 5 * (10 ** -7)
Vg = [0.5, 1, 1.5, 2]
Esi = 11.7
tox = 1.5 * (10 ** -7)
tsi = 5 * (10 ** -7)
ni = 1.5 * (10 ** 10)
q = 1.602 * (10 ** -19)

B = Symbol('B')

nsolve((Vg1/2*Vth) - sympy.log((2/tsi)*(sympy.sqrt((2*Esi*Vth)/(ni * q)))) - sympy.log(B) + sympy.log(sympy.cos(B)) - 2*11.7*(tox)/(3.9 * tsi), 1)

您尝试解决的表达式是非线性的,因此您应尝试使用nsolve以数字方式求解。

注意:第二个参数是对结果应该接近的猜测。

1我得到了一个复杂的结果:

-17910.2176436326 - 37.1544916945789*I