Python矩阵绘图中的递归错误
我正在使用Python解决一些光学多层问题 我在试图解决这个问题时遇到了这个错误,因为我是初学者,调试这个问题对我来说太难了。任何意见和帮助都将不胜感激 问题是找到| 1 | 2 |…N…的反射率和透射率|1 | 2 |结构。 我有一个由材料1和2组成的单元。总结构为单元的N=5 我用传递矩阵法计算了总透过率和反射率 我在下面附上了我的代码Python矩阵绘图中的递归错误,python,matrix,Python,Matrix,我正在使用Python解决一些光学多层问题 我在试图解决这个问题时遇到了这个错误,因为我是初学者,调试这个问题对我来说太难了。任何意见和帮助都将不胜感激 问题是找到| 1 | 2 |…N…的反射率和透射率|1 | 2 |结构。 我有一个由材料1和2组成的单元。总结构为单元的N=5 我用传递矩阵法计算了总透过率和反射率 我在下面附上了我的代码 import numpy as np import sympy as sm from sympy import symbols from sympy.plo
import numpy as np
import sympy as sm
from sympy import symbols
from sympy.plotting import plot
# 111
d1, d2, lam, n1, n2 = symbols('d1, d2, lam, n1, n2', real=True, positive=True)
d1 = 100e-9
d2 = 120e-9
n1 = 1.7
n2 = 1.4
N = 5
r12, t12 = (n1 - n2) / (n1 + n2), (2 * n1) / (n1 + n2)
r21, t21 = (n2 - n1) / (n2 + n1), (2 * n2) / (n2 + n1)
psi1 = 2 * np.pi * n1 * d1 / (lam * 1e-9)
psi2 = 2 * np.pi * n2 * d2 / (lam * 1e-9)
# Transfer Matrix at the 1st Border (without 1/t12)
I12 = sm.Matrix([[1, r12], [r12, 1]])
print(f'I12 Matrix:\n{I12}')
# Transfer Matrix in the Slab
L1 = sm.Matrix([[sm.exp(psi1 * -sm.I), 0], [0, sm.exp(psi1 * sm.I)]])
print(f'L1 Matrix:\n{L1}')
# Transfer Matrix at the 2nd Border (without 1/t23)
I21 = sm.Matrix([[1, r21], [r21, 1]])
print(f'I23 Matrix:\n{I21}')
# Transfer Matrix in the Slab
L2 = sm.Matrix([[sm.exp(psi2 * -sm.I), 0], [0, sm.exp(psi2 * sm.I)]])
print(f'L2 Matrix:\n{L2}')
# Total Transfer Matrix: I12 X L2 X I23
coeff = (1 / t12) * (1 / t21)
M1 = I21 * L2
print(f'M1 Matrix:\n{M1}')
M2 = L1 * M1
print(f'M2 Matrix:\n{M2}')
M3 = I12 * M2
print(f'M3 Matrix:\n{M2}')
Munit = coeff * M3
print(f'Munit Matrix:\n{Munit}')
for i in range(N):
if i == 0:
Mt = Munit
else:
Mt = Mt * Munit
print(f'{i+1}:{Mt}\n')
t = 1 / Mt[0, 0]
r = Mt[1, 0] / Mt[0, 0]
print(f'r and t:\n{r} {t}')
T = n2 / n1 * abs(t) ** 2
R = abs(r) ** 2
print(f'R and T:\n{R} {T}')
graph = plot(T, (lam, 300, 800), label='T', line_color='b', Show=False)
graph.extend(plot(R, (lam, 300, 800), label='R', line_color='r', Show=False))
graph.xlabel = 'wavelength in nm'
graph.yLabel = 'Transmittance and Reflectance'
graph.legend = True
graph.show()
此程序显示如下错误消息:
Traceback (most recent call last):
File "HW5_4.py", line 68, in <module>
RecursionError: maximum recursion depth exceeded while calling a Python object
Process finished with exit code 1
如果有人知道如何将绘图的Y轴发送到左侧,将X轴标签发送到底部中心,请告诉我
谢谢大家帮我做这件事 在绘图之前,你需要检查T、R和lam中的内容,可能是一些奇怪的东西阻止了绘图的有效完成。@Frederik.L、R和T非常长,就像一百行一样。那么,这是否是由于计算时间过长造成的?
import numpy as np
import sympy as sm
from sympy import symbols
from sympy.plotting import plot
# 222
d, lam, n1, n2, n3 = symbols('d, lam, n1, n2, n3', real=True, positive=True)
d = 5e-6
n1 = 1
n2 = 2.6
n3 = 1.45
r12, t12 = (n1 - n2) / (n1 + n2), (2 * n1) / (n1 + n2)
r23, t23 = (n2 - n3) / (n2 + n3), (2 * n2) / (n2 + n3)
psi = 2 * np.pi * n2 * d / (lam * 1e-9)
# Transfer Matrix at the 1st Border (without 1/t12)
I12 = sm.Matrix([[1, r12], [r12, 1]])
print(f'I12 Matrix:\n{I12}')
# Transfer Matrix in the Slab
L2 = sm.Matrix([[sm.exp(psi * -sm.I), 0], [0, sm.exp(psi * sm.I)]])
print(f'L2 Matrix:\n{L2}')
# Transfer Matrix at the 2nd Border (without 1/t23)
I23 = sm.Matrix([[1, r23], [r23, 1]])
print(f'I23 Matrix:\n{I23}')
# Total Transfer Matrix: I12 X L2 X I23
coeff = (1 / t12) * (1 / t23)
M1 = L2 * I23
print(f'M1 Matrix:\n{M1}')
M2 = I12 * M1
print(f'M2 Matrix:\n{M2}')
Mt = coeff * M2
print(f'Mt Matrix:\n{Mt}')
t = 1 / Mt[0, 0]
r = Mt[1, 0] / Mt[0, 0]
print(f'r and t:\n{t} {r}')
T = n3 / n1 * abs(t) ** 2
R = abs(r) ** 2
graph = plot(T, (lam, 500, 600), label='T', line_color='b', Show=False)
graph.extend(plot(R, (lam, 500, 600), label='R', line_color='r', Show=False))
graph.xlabel = 'wavelength in nm'
graph.yLabel = 'Transmittance and Reflectance'
graph.legend = True
graph.show()