我试图针对一定范围的电压值在三个不同的温度(具有三个不同的饱和电流)上循环肖克利二极管方程,然后将这三个数据插入列表中。
kb = 1.38E-23
q = 1.602E-19
voltage = np.arange(0.5, 1.02, 0.02)
T = np.array([269, 289.1, 294.5])
Is = np.array([1.707E-14, 6.877E-14, 1.4510E-13])
i269 = []
i284 = []
i294 = []
for i in range(1, len(voltage)):
for j in range(1, 3):
I = Is[j] * np.exp((voltage[i] * q) / (kb * T[j]))
i269.append(I[j[0]])
i284.append(I[j[1]])
i294.append(I[j[2]])
我知道我在这里使用的方法在语法上是不正确的,但是我已经以此方式编写它,以试图帮助我传达我要实现的目标。
我想先通过j = 0的电压循环,然后将I附加到i269中,然后再通过j = 1的电压循环,然后附加到i284等中。
任何帮助将不胜感激。 谢谢
答案 0 :(得分:0)
请注意,使用当前方法I
不能建立索引,因为您只是为其分配了一个值。您首先需要将其定义为列表,然后为其索引以将其值分配给i269
,i284
或i294
:
kb = 1.38E-23
q = 1.602E-19
voltage = np.arange(0.5, 1.02, 0.02)
T = np.array([269, 289.1, 294.5])
Is = np.array([1.707E-14, 6.877E-14, 1.4510E-13])
I = [0 for _ in range(len(T))]
i269 = []
i284 = []
i294 = []
for i in range(len(voltage)):
for j in range(3):
I[j] = Is[j] * np.exp((voltage[i] * q) / (kb * T[j]))
i269.append(I[0])
i284.append(I[1])
i294.append(I[2])
但是,可以利用broadcasting并将上述向量矢量化为:
i269, i284, i294 = Is[:,None] * np.exp((voltage * q) / (kb * T)[:,None])
让我们检查结果并查看两种方法的计时:
kb = 1.38E-23
q = 1.602E-19
voltage = np.arange(0.5, 1.02, 0.02)
T = np.array([269, 289.1, 294.5])
Is = np.array([1.707E-14, 6.877E-14, 1.4510E-13])
def current_approach(kb, q, voltage, T, Is):
I = [0 for _ in range(len(T))]
i269 = []
i284 = []
i294 = []
for i in range(len(voltage)):
for j in range(3):
I[j] = Is[j] * np.exp((voltage[i] * q) / (kb * T[j]))
i269.append(I[0])
i284.append(I[1])
i294.append(I[2])
return i269, i284, i294
def vect_approach(kb, q, voltage, T, Is):
i269, i284, i294 = Is[:,None] * np.exp((voltage * q) / (kb * T)[:,None])
return i269, i284, i294
np.allclose(current_approach(kb, q, voltage, T, Is),
vect_approach(kb, q, voltage, T, Is))
# True
%timeit current_approach(kb, q, voltage, T, Is)
# 201 µs ± 3.46 µs per loop (mean ± std. dev. of 7 runs, 10000 loops each)
%timeit vect_approach(kb, q, voltage, T, Is)
# 30.9 ns ± 0.647 ns per loop (mean ± std. dev. of 7 runs, 10000000 loops each)
采用矢量化方法,可大大提高 6,500x
的速度!