牛顿环实验MATLAB仿真界面设计
牛顿环实验MATLAB仿真界面设计
本项目使用MATLAB GUIDE开发了一个牛顿环实验仿真界面,用户可通过界面输入牛顿环半径和波长参数,并选择静态或动态仿真模式,观察牛顿环的变化。
界面设计
草图
[图片描述:牛顿环仿真界面草图]
控件清单
- 静态/动态仿真选择按钮组
- 牛顿环半径输入框
- 波长滑动条
- 波长显示文本框
- 薄膜厚度增加/减少按钮
- 开始仿真按钮
- 停止仿真按钮
- 牛顿环图像绘制区域
GUI界面
[图片描述:牛顿环仿真界面截图]
代码实现
function varargout = newton_rings_simulation_gui(varargin)
% NEWTON_RINGS_SIMULATION_GUI MATLAB code for newton_rings_simulation_gui.fig
% NEWTON_RINGS_SIMULATION_GUI, by itself, creates a new NEWTON_RINGS_SIMULATION_GUI or raises the existing
% singleton*.
%
% H = NEWTON_RINGS_SIMULATION_GUI returns the handle to a new NEWTON_RINGS_SIMULATION_GUI or the handle to
% the existing singleton*.
%
% NEWTON_RINGS_SIMULATION_GUI('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in NEWTON_RINGS_SIMULATION_GUI.M with the given input arguments.
%
% NEWTON_RINGS_SIMULATION_GUI('Property','Value',...) creates a new NEWTON_RINGS_SIMULATION_GUI or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before newton_rings_simulation_gui_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to newton_rings_simulation_gui_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help newton_rings_simulation_gui
% Last Modified by GUIDE v2.5 01-Dec-2021 12:09:42
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @newton_rings_simulation_gui_OpeningFcn, ...
'gui_OutputFcn', @newton_rings_simulation_gui_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before newton_rings_simulation_gui is made visible.
function newton_rings_simulation_gui_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to newton_rings_simulation_gui (see VARARGIN)
% Choose default command line output for newton_rings_simulation_gui
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes newton_rings_simulation_gui wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% Initialize simulation parameters
handles.radius = str2double(get(handles.radius_edit, 'String'));
handles.wavelength = get(handles.wavelength_slider, 'Value');
handles.thickness = 0;
% Initialize simulation flags
handles.is_static = true;
handles.is_running = false;
% Update handles structure
guidata(hObject, handles);
% Plot initial Newton rings
plot_newton_rings(handles);
% --- Outputs from this function are returned to the command line.
function varargout = newton_rings_simulation_gui_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on slider movement.
function wavelength_slider_Callback(hObject, eventdata, handles)
% hObject handle to wavelength_slider (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Update wavelength value
handles.wavelength = get(hObject, 'Value');
% Update wavelength display
set(handles.wavelength_text, 'String', sprintf('%.2f nm', handles.wavelength));
% Update handles structure
guidata(hObject, handles);
% Plot Newton rings
plot_newton_rings(handles);
% --- Executes during object creation, after setting all properties.
function wavelength_slider_CreateFcn(hObject, eventdata, handles)
% hObject handle to wavelength_slider (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Set slider properties
set(hObject, 'Min', 400, 'Max', 700, 'SliderStep', [1/300 1/100], 'Value', 550);
% Update wavelength display
set(handles.wavelength_text, 'String', sprintf('%.2f nm', get(hObject, 'Value')));
% --- Executes on button press in add_thickness_button.
function add_thickness_button_Callback(hObject, eventdata, handles)
% hObject handle to add_thickness_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Increase thickness by 1 nm
handles.thickness = handles.thickness + 1;
% Update handles structure
guidata(hObject, handles);
% Plot Newton rings
plot_newton_rings(handles);
% --- Executes on button press in subtract_thickness_button.
function subtract_thickness_button_Callback(hObject, eventdata, handles)
% hObject handle to subtract_thickness_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Decrease thickness by 1 nm
handles.thickness = handles.thickness - 1;
% Update thickness display
set(handles.thickness_text, 'String', sprintf('%d nm', handles.thickness));
% Update handles structure
guidata(hObject, handles);
% Plot Newton rings
plot_newton_rings(handles);
% --- Executes on button press in static_simulation_button.
function static_simulation_button_Callback(hObject, eventdata, handles)
% hObject handle to static_simulation_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Set simulation mode to static
handles.is_static = true;
% Update handles structure
guidata(hObject, handles);
% Plot Newton rings
plot_newton_rings(handles);
% --- Executes on button press in dynamic_increase_button.
function dynamic_increase_button_Callback(hObject, eventdata, handles)
% hObject handle to dynamic_increase_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Set simulation mode to dynamic increase
handles.is_static = false;
% Start simulation
start_simulation(hObject, handles);
% Update handles structure
guidata(hObject, handles);
% --- Executes on button press in dynamic_decrease_button.
function dynamic_decrease_button_Callback(hObject, eventdata, handles)
% hObject handle to dynamic_decrease_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Set simulation mode to dynamic decrease
handles.is_static = false;
% Start simulation
start_simulation(hObject, handles);
% Update handles structure
guidata(hObject, handles);
% --- Executes on button press in stop_simulation_button.
function stop_simulation_button_Callback(hObject, eventdata, handles)
% hObject handle to stop_simulation_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Stop simulation
handles.is_running = false;
% Update handles structure
guidata(hObject, handles);
% --- Executes on button press in start_simulation_button.
function start_simulation_button_Callback(hObject, eventdata, handles)
% hObject handle to start_simulation_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Set simulation mode to dynamic increase
handles.is_static = false;
% Start simulation
start_simulation(hObject, handles);
% Update handles structure
guidata(hObject, handles);
function radius_edit_Callback(hObject, eventdata, handles)
% hObject handle to radius_edit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Update radius value
handles.radius = str2double(get(hObject, 'String'));
% Update handles structure
guidata(hObject, handles);
% Plot Newton rings
plot_newton_rings(handles);
% --- Executes during object creation, after setting all properties.
function radius_edit_CreateFcn(hObject, eventdata, handles)
% hObject handle to radius_edit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Set default radius value
set(hObject, 'String', '10');
% --- Executes during object creation, after setting all properties.
function thickness_text_CreateFcn(hObject, eventdata, handles)
% hObject handle to thickness_text (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Set default thickness value
set(hObject, 'String', '0 nm');
function start_simulation(hObject, handles)
% Start dynamic simulation
% Set simulation flag to running
handles.is_running = true;
% Loop until simulation is stopped or maximum thickness is reached
while handles.is_running && handles.thickness <= 1000
% Update thickness display
set(handles.thickness_text, 'String', sprintf('%d nm', handles.thickness));
% Plot Newton rings
plot_newton_rings(handles);
% Increase or decrease thickness, depending on simulation mode
if handles.is_static
% Static simulation
pause(1);
else
% Dynamic simulation
if get(hObject, 'Tag') == "dynamic_increase_button"
handles.thickness = handles.thickness + 1;
else
handles.thickness = handles.thickness - 1;
end
end
% Update handles structure
guidata(hObject, handles);
end
function plot_newton_rings(handles)
% Plot Newton rings
% Calculate radius and thickness in meters
r = handles.radius / 1000;
t = handles.thickness / 1000000000;
% Calculate wavelength in meters
lambda = handles.wavelength / 1000000000;
% Calculate refractive index of air and glass
n_air = 1;
n_glass = 1.5;
% Calculate radius of curvature of lens
R = 2 * r;
% Calculate radius of nth bright ring
n = 1:200;
R_n = sqrt(n .* lambda .* R ./ (n_glass - n_air));
% Calculate radius of nth dark ring
R_n_prime = sqrt((n - 0.5) .* lambda .* R ./ (n_glass - n_air));
% Calculate distance from center of lens to rings
x_n = R ./ (n_glass - n_air) .* (t + (n_glass - n_air) .* R_n);
x_n_prime = R ./ (n_glass - n_air) .* (t + (n_glass - n_air) .* R_n_prime);
% Plot rings on axes
axes(handles.newton_rings_axes);
cla;
hold on;
plot(x_n, 'o-', 'LineWidth', 1.5);
plot(x_n_prime, 'o-', 'LineWidth', 1.5);
hold off;
axis equal;
axis off;
% --- Executes on button press in quit_button.
function quit_button_Callback(hObject, eventdata, handles)
% hObject handle to quit_button (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Close GUI
close(handles.figure1);
功能介绍
- 界面输入: 用户可以通过输入框输入牛顿环半径和通过滑动条输入波长参数。
- 仿真模式选择: 用户可以选择静态仿真或动态仿真模式。
- 静态仿真: 固定薄膜厚度,显示牛顿环图像。
- 动态仿真: 薄膜厚度连续增加或减少,实时显示牛顿环图像变化。
- 动态仿真控制: 用户可以使用“开始仿真”按钮启动动态仿真,使用“停止仿真”按钮停止动态仿真。
- 薄膜厚度控制: 用户可以使用“增加薄膜厚度”和“减少薄膜厚度”按钮手动调整薄膜厚度,并实时观察牛顿环的变化。
- 图像显示: 界面中提供一个区域用于绘制牛顿环图像。
运行步骤
- 将代码保存为newton_rings_simulation_gui.m文件。
- 在MATLAB中运行该文件。
- 在弹出的GUI界面中设置参数,选择仿真模式,并运行仿真。
总结
本项目使用MATLAB GUIDE开发了一个牛顿环实验仿真界面,方便用户进行牛顿环实验模拟,观察牛顿环的变化规律。用户可以通过界面设置不同的参数,观察不同条件下牛顿环的变化,加深对牛顿环实验的理解。
扩展
- 可以添加更多参数设置选项,例如:
- 光的入射角
- 透镜的材料
- 可以加入一些动画效果,使仿真更生动。
- 可以将仿真结果保存为图像或视频文件。
- 可以将GUI界面设计得更美观,更符合用户使用习惯。
原文地址: http://www.cveoy.top/t/topic/mOI6 著作权归作者所有。请勿转载和采集!