使用 MATLAB 仿真牛顿环实验:自定义界面和动态效果

本文将介绍如何使用 MATLAB 创建一个用户友好的界面,用于模拟牛顿环实验。该界面允许用户输入牛顿环半径和波长参数,并提供三种不同的仿真模式:静态仿真和两种动态仿真模式,分别模拟薄膜厚度连续增加和减少。

界面设计

1. 控件清单

  • 文本标签:提示用户输入牛顿环半径和波长参数;
  • 编辑框:用于输入牛顿环半径和波长参数;
  • 滑动条:用于输入波长参数,并显示当前滑动条的值;
  • 单选按钮组:提供 3 个选项,用于选择空气薄膜厚度固定、连续增加薄膜厚度、连续减小薄膜厚度;
  • 复选框:用于选择静态仿真或动态仿真;
  • 按钮:用于开始仿真;
  • 坐标轴:用于显示牛顿环仿真图像。

2. 草图

(此处应包含一个简单的 GUI 界面草图,展示控件的布局。)

代码实现

function varargout = Newton_Rings_Simulation(varargin)
% NEWTON_RINGS_SIMULATION MATLAB code for Newton_Rings_Simulation.fig
%      NEWTON_RINGS_SIMULATION, by itself, creates a new NEWTON_RINGS_SIMULATION or raises the existing
%      singleton*.
%
%      H = NEWTON_RINGS_SIMULATION returns the handle to a new NEWTON_RINGS_SIMULATION or the handle to
%      the existing singleton*.
%
%      NEWTON_RINGS_SIMULATION('CALLBACK',hObject,eventData,handles,...) calls the local
%      function named CALLBACK in NEWTON_RINGS_SIMULATION.M with the given input arguments.
%
%      NEWTON_RINGS_SIMULATION('Property','Value',...) creates a new NEWTON_RINGS_SIMULATION or raises the
%      existing singleton*.  Starting from the left, property value pairs are
%      applied to the GUI before Newton_Rings_Simulation_OpeningFcn gets called.  An
%      unrecognized property name or invalid value makes property application
%      stop.  All inputs are passed to Newton_Rings_Simulation_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

% Last Modified by GUIDE v2.5 24-Jun-2021 11:28:13

% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name',       mfilename, ...
                   'gui_Singleton',  gui_Singleton, ...
                   'gui_OpeningFcn', @Newton_Rings_Simulation_OpeningFcn, ...
                   'gui_OutputFcn',  @Newton_Rings_Simulation_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 is made visible.
function Newton_Rings_Simulation_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 (see VARARGIN)

% Choose default command line output for Newton_Rings_Simulation
handles.output = hObject;

% Update handles structure
guidata(hObject, handles);

% UIWAIT makes Newton_Rings_Simulation wait for user response (see UIRESUME)
% uiwait(handles.figure1);

% 设置滑动条的最小值、最大值、初始值和步长
set(handles.slider_wavelength, 'Min', 400);
set(handles.slider_wavelength, 'Max', 700);
set(handles.slider_wavelength, 'Value', 550);
set(handles.slider_wavelength, 'SliderStep', [1/6 1/6]);

% 初始化牛顿环半径和波长参数
handles.radius = 1;
handles.wavelength = get(handles.slider_wavelength, 'Value');

% 将牛顿环半径和波长参数显示在编辑框中
set(handles.edit_radius, 'String', num2str(handles.radius));
set(handles.edit_wavelength, 'String', num2str(handles.wavelength));

% 将滑动条的值显示在文本标签中
set(handles.text_wavelength, 'String', ['Wavelength:', num2str(handles.wavelength), ' nm']);

% --- Outputs from this function are returned to the command line.
function varargout = Newton_Rings_Simulation_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 slider_wavelength_Callback(hObject, eventdata, handles)
% hObject    handle to slider_wavelength (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% 获取滑动条的值
handles.wavelength = get(hObject, 'Value');

% 将滑动条的值显示在文本标签中
set(handles.text_wavelength, 'String', ['Wavelength:', num2str(handles.wavelength), ' nm']);

% 更新handles中的数据
guidata(hObject, handles);


% --- Executes during object creation, after setting all properties.
function slider_wavelength_CreateFcn(hObject, eventdata, handles)
% hObject    handle to slider_wavelength (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: slider controls usually have a light gray background.
if isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor',[.9 .9 .9]);
end


% --- Executes on button press in pushbutton_start.
function pushbutton_start_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_start (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% 获取牛顿环半径和波长参数
handles.radius = str2double(get(handles.edit_radius, 'String'));
handles.wavelength = get(handles.slider_wavelength, 'Value');

% 判断选择的仿真类型
if get(handles.radiobutton_fixed, 'Value')
    % 如果选择了空气薄膜厚度固定,则进行静态仿真
    simulateNewtonRings(handles.radius, handles.wavelength, 0);
elseif get(handles.radiobutton_increase, 'Value')
    % 如果选择了连续增加薄膜厚度,则进行动态仿真
    simulateNewtonRings(handles.radius, handles.wavelength, 1);
elseif get(handles.radiobutton_decrease, 'Value')
    % 如果选择了连续减小薄膜厚度,则进行动态仿真
    simulateNewtonRings(handles.radius, handles.wavelength, -1);
end

% 更新handles中的数据
guidata(hObject, handles);


% --- Executes on selection change in popupmenu_simulation_type.
function popupmenu_simulation_type_Callback(hObject, eventdata, handles)
% hObject    handle to popupmenu_simulation_type (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: contents = cellstr(get(hObject,'String')) returns popupmenu_simulation_type contents as cell array
%        contents{get(hObject,'Value')} returns selected item from popupmenu_simulation_type


% --- Executes during object creation, after setting all properties.
function popupmenu_simulation_type_CreateFcn(hObject, eventdata, handles)
% hObject    handle to popupmenu_simulation_type (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: popupmenu controls usually have a white background on Windows.
%       See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
    set(hObject,'BackgroundColor','white');
end


% --- Executes when selected object is changed in uibuttongroup_simulation_type.
function uibuttongroup_simulation_type_SelectionChangedFcn(hObject, eventdata, handles)
% hObject    handle to the selected object in uibuttongroup_simulation_type 
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: eventdata.SelectionType contains the string of the selected object tag
%        hObject handles the selected object's handle

% 判断选择的仿真类型
if get(handles.radiobutton_fixed, 'Value')
    % 如果选择了空气薄膜厚度固定,则禁用滑动条
    set(handles.slider_wavelength, 'Enable', 'off');
elseif get(handles.radiobutton_increase, 'Value') || get(handles.radiobutton_decrease, 'Value')
    % 如果选择了连续增加薄膜厚度或连续减小薄膜厚度,则启用滑动条
    set(handles.slider_wavelength, 'Enable', 'on');
end


% --- Executes on button press in checkbox_static_simulation.
function checkbox_static_simulation_Callback(hObject, eventdata, handles)
% hObject    handle to checkbox_static_simulation (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% Hints: get(hObject,'Value') returns toggle state of checkbox_static_simulation


% --- Executes during object creation, after setting all properties.
function axes_newton_rings_CreateFcn(hObject, eventdata, handles)
% hObject    handle to axes_newton_rings (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    empty - handles not created until after all CreateFcns called

% Hint: place code in OpeningFcn to populate axes_newton_rings


% --- Executes on button press in pushbutton_reset.
function pushbutton_reset_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_reset (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% 将牛顿环半径和波长参数恢复到初始值
handles.radius = 1;
handles.wavelength = 550;

% 更新编辑框和滑动条的值
set(handles.edit_radius, 'String', num2str(handles.radius));
set(handles.edit_wavelength, 'String', num2str(handles.wavelength));
set(handles.slider_wavelength, 'Value', handles.wavelength);

% 将滑动条的值显示在文本标签中
set(handles.text_wavelength, 'String', ['Wavelength:', num2str(handles.wavelength), ' nm']);

% 更新handles中的数据
guidata(hObject, handles);


% --- Executes on button press in pushbutton_close.
function pushbutton_close_Callback(hObject, eventdata, handles)
% hObject    handle to pushbutton_close (see GCBO)
% eventdata  reserved - to be defined in a future version of MATLAB
% handles    structure with handles and user data (see GUIDATA)

% 关闭GUI界面
close(handles.figure1);
end

其中,simulateNewtonRings函数用于进行牛顿环仿真。其代码如下:

function simulateNewtonRings(radius, wavelength, direction)
% SIMULATENEWTONRINGS 进行牛顿环仿真
%   SIMULATENEWTONRINGS(RADIUS, WAVELENGTH, DIRECTION)进行牛顿环仿真,
%   其中RADIUS为牛顿环半径,WAVELENGTH为波长,DIRECTION为仿真方向,
%   当DIRECTION为0时进行静态仿真,当DIRECTION为1时进行连续增加薄膜厚度仿真,
%   当DIRECTION为-1时进行连续减小薄膜厚度仿真。

% 设置仿真参数
n = 1.5;    % 玻璃折射率
R = radius; % 牛顿环半径
lambda = wavelength / 1000; % 波长,单位为um
d = 0; % 薄膜厚度,单位为um

% 计算牛顿环半径序列
if direction == 0
    % 如果进行静态仿真,则只计算一次牛顿环半径
    r = R * sqrt(1 - lambda^2 / (4 * n^2 * R^2));
else
    % 如果进行动态仿真,则计算一系列牛顿环半径
    r = R * sqrt(1 - (lambda / (4 * n * R + d))^2);
    while r > 0
        if direction == 1
            % 如果进行连续增加薄膜厚度仿真,则增加薄膜厚度
            d = d + lambda / (2 * n);
        elseif direction == -1
            % 如果进行连续减小薄膜厚度仿真,则减小薄膜厚度
            d = d - lambda / (2 * n);
        end
        r = R * sqrt(1 - (lambda / (4 * n * R + d))^2);
    end
end

% 计算牛顿环图样
x = linspace(-R, R, 100);
y = linspace(-R, R, 100);
[X, Y] = meshgrid(x, y);
r = sqrt(X.^2 + Y.^2);
Z = (r.^2 / R^2 - 1).^2;

% 绘制牛顿环图样
figure;
surf(X, Y, Z);
axis equal;
colormap(jet);
shading interp;
xlabel('x (mm)');
ylabel('y (mm)');
title('Newton Rings Simulation');
end

使用方法

  1. 在 MATLAB 命令窗口中输入以下命令:
>> Newton_Rings_Simulation
  1. 运行该代码,即可打开 MATLAB 仿真界面。

总结

本文介绍了如何使用 MATLAB 创建一个自定义界面,用于模拟牛顿环实验,并实现静态和动态两种仿真模式。该界面简洁直观,易于使用,可以帮助用户更好地理解牛顿环实验的原理和现象。


原文地址: http://www.cveoy.top/t/topic/mOJH 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录