加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
main.py 3.80 KB
一键复制 编辑 原始数据 按行查看 历史
Yuncheng-Xie 提交于 2023-10-11 22:04 . 修改
import tkinter as tk
from tkinter import ttk
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
import matplotlib.pyplot as plt
import numpy as np
screen_distance = 1 # Distance from slits to screen (in meters)
# Create a grid of points on the screen
screen_width = 1e-2 # Width of the screen (in meters)
num_points = 10000
screen_positions = np.linspace(-screen_width / 2, screen_width / 2, num_points)
ymax = 1e-2
# Function to calculate interference pattern
def calculate_interference_pattern(d, theta, wavelength):
# Calculate the intensity at each point on the screen
x_values = screen_positions
y_values = np.linspace(0, ymax, 50)
X, Y = np.meshgrid(x_values, y_values)
path_difference = (d * np.sin(theta)) + screen_positions * d / screen_distance
intensity = (1 + np.cos(2 * np.pi * path_difference / wavelength)) ** 2
return screen_positions, intensity
# Function to update the plot
def update_plot(value):
wavelength = float(wl_entry.get())
print(wavelength)
d = float(d_entry.get())
theta = float(theta_entry.get())
theta = np.deg2rad(theta)
x, I = calculate_interference_pattern(d, np.deg2rad(theta), wavelength)
ax.clear()
# ax.plot(x, I, '.')
I = np.tile(I, (10,1))
image = ax.imshow(I, extent=(-screen_width, screen_width, -ymax, ymax),aspect='auto', origin='lower', cmap='coolwarm') # cmap参数指定了使用的颜色映射,这里使用了'viridis'
ax.set_xlabel("Screen Position (meters)")
ax.set_ylabel("Intensity")
ax.set_yticks([])
d0 = d / 0.02 * 0.8
dd = 0.05
x1 = [-0.5, -d0-dd]
y1 = [0, 0]
x2 = [-d0, d0]
x3 = [d0+dd, 0.5]
lightx1 = [-d0-dd/2, -d0-dd/2+0.5*np.tan(theta)]
lighty1 = [0, 0.5]
lightx2 = [d0+dd/2, d0+dd/2+0.5*np.tan(theta)]
ax2.clear()
ax2.plot(x1, y1, c='black')
ax2.plot(x2, y1, c='black')
ax2.plot(x3, y1, c='black')
ax2.plot(lightx1, lighty1, c='red')
ax2.plot(lightx2, lighty1, c='red')
ax2.set_yticks([])
ax2.set_xticks([])
ax2.set_ylim(-0.2,0.6)
canvas.draw()
# Create the main window
root = tk.Tk()
root.title("Two-Slit Interference")
root.geometry('1000x1000')
# Create and pack a frame for the controls
control_frame = ttk.Frame(root)
control_frame.pack(padx=10, pady=10)
# Create and pack labels and entry widgets for parameters
# ttk.Label(control_frame, text="Distance between Slits (meters):").grid(row=0, column=0)
# d_entry = ttk.Entry(control_frame)
# d_entry.grid(row=0, column=1)
# d_entry.insert(0, "0.001") # Default value
wl_entry= tk.Scale(root, from_=400e-9, to=760e-9, resolution=10e-9, length=200, sliderlength=20, label='Wave Length', orient=tk.HORIZONTAL, command=update_plot)
wl_entry.pack()
wl_entry.set(value=600e-9)
d_entry = tk.Scale(root, from_=1e-4, to=1e-2, resolution=1e-4, length=200, sliderlength=20, label='Distance between Slits (meters)', orient=tk.HORIZONTAL, command=update_plot)
d_entry.pack()
d_entry.set(value=1e-3)
theta_entry = tk.Scale(root, from_=0, to=30, resolution=1, length=200, sliderlength=20, label='Incident Angle (degrees)', orient=tk.HORIZONTAL, command=update_plot)
theta_entry.pack()
theta_entry.set(value=0)
# ttk.Label(control_frame, text="Incident Angle (degrees):").grid(row=1, column=0)
# theta_entry = ttk.Entry(control_frame)
# theta_entry.grid(row=1, column=1)
# theta_entry.insert(0, "0") # Default value
# Create and pack a button to update the plot
# update_button = ttk.Button(control_frame, text="Update Plot", command=update_plot)
# update_button.grid(row=2, columnspan=2)
# Create a Matplotlib figure and plot
fig = plt.figure()
ax = fig.add_subplot(212)
ax2 = fig.add_subplot(211)
canvas = FigureCanvasTkAgg(fig, master=root)
canvas_widget = canvas.get_tk_widget()
canvas_widget.pack()
# Initialize the plot
update_plot(0)
# Start the Tkinter main loop
root.mainloop()
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化