python仿真模拟牛顿环界面搭建

通过python实现牛顿环仿真模拟

import tkinter as tk
from tkinter import messagebox
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib import gridspec


class InterferenceApp(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("牛顿环实验")
        self.geometry("1600x1800")
        self.input_frame = tk.Frame(self)
        self.input_frame.pack(side=tk.LEFT, fill=tk.Y, expand=False)

        tk.Label(self.input_frame, text='波长(mm):', font=(None, 20)).pack(side=tk.TOP, padx=10, pady=10)
        self.lam_entry = tk.Entry(self.input_frame, font=(None, 20))
        self.lam_entry.pack(side=tk.TOP, padx=10, pady=10)

        tk.Label(self.input_frame, text='曲率半径(mm):', font=(None, 20)).pack(side=tk.TOP, padx=10, pady=10)
        self.R_entry = tk.Entry(self.input_frame, font=(None, 20))
        self.R_entry.pack(side=tk.TOP, padx=10, pady=10)

        self.calculate_button = tk.Button(self.input_frame, text="运行", font=(None, 20), command=self.calculate_interference)
        self.calculate_button.pack(side=tk.TOP, padx=10, pady=10)
        self.figure_frame = tk.Frame(self)
        self.figure_frame.pack(fill=tk.BOTH, expand=True)
        self.fig = plt.figure()
        self.grid = gridspec.GridSpec(2, 1, height_ratios=[3, 1])
        self.ax = self.fig.add_subplot(self.grid[0])
        self.ax1 = self.fig.add_subplot(self.grid[1])
        self.canvas = FigureCanvasTkAgg(self.fig, master=self.figure_frame)
        self.canvas.draw()
        self.canvas.get_tk_widget().pack(side=tk.TOP, fill=tk.BOTH, expand=True)

    def calculate_interference(self):
        try:
            lam = float(self.lam_entry.get()) * 1e-6  # m
            R = float(self.R_entry.get()) * 1e-6  # m

            ym = np.sqrt(10 * lam * R)
            xs = np.linspace(-ym, ym, 1001)
            ys = np.linspace(-ym, ym, 1001)
            X, Y = np.meshgrid(xs, ys)
            r = np.sqrt(X ** 2 + Y ** 2)
            I = (np.cos(np.pi * (r ** 2 / R + lam / 2) / lam)) ** 2
            B = (I / 4.0) * 255
            self.ax.clear()
            self.ax.pcolormesh(X, Y, B, shading='gouraud', cmap='gray')

            self.ax.set_title('Interference Pattern')
            self.ax.set_xlabel('X (mm)')
            self.ax.set_ylabel('Y (mm)')

            self.ax1.plot(r, B)
            self.ax1.set_title('Intensity Distribution')
            self.ax1.set_xlabel('Distance to Center (mm)')
            self.ax1.set_ylabel('Grayscale Intensity')

            self.canvas.draw()

        except ValueError:
            messagebox.showerror("Error", "Invalid input. Please enter numbers for wavelength and radius of curvature.")


if __name__ == "__main__":
    app = InterferenceApp()
    app.mainloop()

效果展示:

  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值