光学仿真(python菲涅尔公式)

import matplotlib.pyplot as plt

from matplotlib.ticker import FuncFormatter


#数学公式包
""" 
np.pi
np.cos(此处输入的是弧度制)
np.sqrt()
np.sin()
平方是**
"""

import numpy as np


"""
def 函数名字(输入参数)
    return 
"""


#公式
def fresnel(theta, n1, n2):
    theta = theta*np.pi/180
    xTheta = np.cos(theta)
    mid = np.sqrt(1-(n1/n2*np.sin(theta))**2)   #中间变量
    rp = (n2*xTheta-n1*mid)/(n2*xTheta+n1*mid)  #p分量振幅反射率
    rs = (n1*xTheta-n2*mid)/(n1*xTheta+n2*mid)  #
    tp = 2*n1*xTheta/(n2*xTheta+n1*mid)
    ts = 2*n1*xTheta/(n1*xTheta+n2*mid)
    return rp, rs, tp, ts


#画图
def testFres1(n1=1,n2=1.45):         #默认n2为1.45
    theta = np.arange(0,90,0.1)+0j  #
    a = theta*np.pi/180
    rp,rs,tp,ts = fresnel(theta,n1,n2)

    fig = plt.figure(1)
    plt.subplot(1,2,1)
    plt.plot(theta,rp,'-',label='rp')
    plt.plot(theta,rs,'-.',label='rs')
    plt.plot(theta,np.abs(rp),'--',label='|rp|')
    plt.plot(theta,np.abs(rs),':',label='|rs|')
    plt.legend()

    plt.subplot(1,2,2)
    plt.plot(theta,tp,'-',label='tp')
    plt.plot(theta,ts,'-.',label='ts')
    plt.plot(theta,np.abs(tp),'--',label='|tp|')
    plt.plot(theta,np.abs(ts),':',label='|ts|')
    plt.legend()
    plt.show()


def testFres2(n1=1,n2=1.5):
    theta = np.arange(0,90,0.1)+0j
    a = theta*np.pi/180
    rp,rs,tp,ts = fresnel(theta,n1,n2)

    Rp = np.abs(rp)**2
    Rs = np.abs(rs)**2
    Rn = (Rp+Rs)/2

    fig = plt.figure(1)
    plt.subplot(1,2,1)
    plt.plot(theta,Rp,'-',label='R_p')
    #plt.plot(theta,Rs,'-.',label='R_s')
    plt.plot(theta,Rn,'-',label='R_n')
    plt.legend()

    plt.show()

def testFres3(n1=1.5,n2=1):
    theta = np.arange(0,90,0.1)+0j
    a = theta*np.pi/180
    rp,rs,tp,ts = fresnel(theta,n1,n2)

    Rp = np.abs(rp)**2
    Rs = np.abs(rs)**2
    Rn = (Rp+Rs)/2

    fig = plt.figure(2)
    plt.subplot(1,2,1)
    plt.plot(theta,Rp,'-',label='R_p')
    plt.plot(theta,Rs,'-.',label='R_s')
    plt.plot(theta,Rn,'-',label='R_n')
    plt.legend()

    plt.show()



def testFres():
    theta = np.arange(0,90,0.1)+0j  #
    a = theta*np.pi/180
    
    rp,rs,tp,ts = fresnel(theta,1,2)
    Rp = np.abs(rp)**2
    Rs = np.abs(rs)**2
    Rn = (Rp+Rs)/2

    plt.suptitle("202008010110-homework")

    plt.subplot(1,2,1)
    plt.title("n1 = 1, n2 = 2")
    plt.plot(theta,Rp,'-',label='R_p')
    plt.plot(theta,Rs,'-',label='R_s')
    plt.plot(theta,Rn,'-.',label='')

    plt.ylim(0,1)#y轴上限为1,下限为0
    def to_percent(temp, position):#百分比上下限
        return '%1.0f'%(100*temp) + '%'
    plt.gca().yaxis.set_major_formatter(FuncFormatter(to_percent))
    plt.legend()        #图例
    plt.xlabel("Incident angle (θ1/°)")
    plt.ylabel("Reflectivity (%)")
    #设置注释
    plt.annotate('Brewster angle (%.2f)'%(180*np.arctan(2)/np.pi), xy=(180*np.arctan(2)/np.pi, 0), xytext=(35, 0.6),
                xycoords='data',
                arrowprops=dict(facecolor='black',
                    arrowstyle = "->",)
                )


    rp,rs,tp,ts = fresnel(theta,2,1)
    Rp = np.abs(rp)**2
    Rs = np.abs(rs)**2
    Rn = (Rp+Rs)/2

    plt.subplot(1,2,2)
    plt.title("n1 = 2, n2 = 1")
    plt.plot(theta,Rp,'-',label='R_p')
    plt.plot(theta,Rs,'-',label='R_s')
    plt.plot(theta,Rn,'-.',label='')

    plt.ylim(0,1)#上限为1,下限为0
    def to_percent(temp, position):#百分比上下限
        return '%1.0f'%(100*temp) + '%'
    plt.gca().yaxis.set_major_formatter(FuncFormatter(to_percent))
    plt.legend()        #图例
    plt.xlabel("Incident angle (θ1/°)")
    plt.ylabel("Reflectivity (%)")
    #设置注释
    plt.annotate('Brewster angle(%.2f)'%(180*np.arctan(0.5)/np.pi), xy=(180*np.arctan(0.5)/np.pi, 0), xytext=(35, 0.6),
                xycoords='data',
                arrowprops=dict(facecolor='black',
                    arrowstyle = "->",)
                )
    plt.annotate('Critical angle(%.2f)'%(180*np.arcsin(0.5)/np.pi), xy=(180*np.arcsin(0.5)/np.pi, 0), xytext=(60, 0.4),
                xycoords='data',
                arrowprops=dict(facecolor='black',
                    arrowstyle = "->",)
                )
    

    plt.show()



#主函数
#testFres2()
#testFres3()
testFres()



(14条消息) python光学仿真之菲涅耳公式_微小冷的博客-CSDN博客_菲涅尔公式

(14条消息) Python学习笔记(4)——Matplotlib中的annotate(注解)的用法_赵赵赵颖的博客-CSDN博客_annotate

  • 2
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 6
    评论
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值