MATLAB 仿真界面:牛顿环实验可视化工具
function Newton_Rings_GUI % 创建 GUI 窗口 hFig = figure('Name', '牛顿环实验仿真', ... 'NumberTitle', 'off', ... 'Units', 'normalized', ... 'Position', [0.2, 0.2, 0.6, 0.6], ... 'Color', [0.8, 0.8, 0.8]);
% 创建半径输入框 hRadiusPanel = uipanel(hFig, ... 'Title', '输入参数', ... 'Units', 'normalized', ... 'Position', [0.05, 0.7, 0.4, 0.25], ... 'BackgroundColor', [0.9, 0.9, 0.9]); hRadiusText = uicontrol(hRadiusPanel, ... 'Style', 'text', ... 'String', '牛顿环半径 (mm):', ... 'Units', 'normalized', ... 'Position', [0.05, 0.6, 0.5, 0.3], ... 'HorizontalAlignment', 'left', ... 'FontSize', 12); hRadiusEdit = uicontrol(hRadiusPanel, ... 'Style', 'edit', ... 'String', '10', ... 'Units', 'normalized', ... 'Position', [0.6, 0.6, 0.3, 0.3], ... 'FontSize', 12); hWavelengthText = uicontrol(hRadiusPanel, ... 'Style', 'text', ... 'String', '波长 (nm):', ... 'Units', 'normalized', ... 'Position', [0.05, 0.2, 0.4, 0.3], ... 'HorizontalAlignment', 'left', ... 'FontSize', 12); hWavelengthSlider = uicontrol(hRadiusPanel, ... 'Style', 'slider', ... 'Units', 'normalized', ... 'Position', [0.6, 0.2, 0.3, 0.3], ... 'Value', 500, ... 'Min', 400, ... 'Max', 700, ... 'SliderStep', [1/300, 10/300], ... 'Callback', @updateWavelengthSlider); hWavelengthEdit = uicontrol(hRadiusPanel, ... 'Style', 'edit', ... 'Units', 'normalized', ... 'Position', [0.6, 0.5, 0.3, 0.2], ... 'String', '500', ... 'FontSize', 12, ... 'Callback', @updateWavelengthEdit);
% 创建实验选项 hOptionPanel = uipanel(hFig, ... 'Title', '实验选项', ... 'Units', 'normalized', ... 'Position', [0.05, 0.4, 0.4, 0.25], ... 'BackgroundColor', [0.9, 0.9, 0.9]); hStaticRadio = uicontrol(hOptionPanel, ... 'Style', 'radiobutton', ... 'String', '静态实验', ... 'Units', 'normalized', ... 'Position', [0.05, 0.6, 0.3, 0.3], ... 'Value', 1, ... 'FontSize', 12); hIncreaseRadio = uicontrol(hOptionPanel, ... 'Style', 'radiobutton', ... 'String', '连续增加厚度', ... 'Units', 'normalized', ... 'Position', [0.05, 0.2, 0.4, 0.3], ... 'FontSize', 12); hDecreaseRadio = uicontrol(hOptionPanel, ... 'Style', 'radiobutton', ... 'String', '连续减小厚度', ... 'Units', 'normalized', ... 'Position', [0.5, 0.2, 0.4, 0.3], ... 'FontSize', 12);
% 创建仿真按钮 hSimulateButton = uicontrol(hFig, ... 'Style', 'pushbutton', ... 'String', '仿真', ... 'Units', 'normalized', ... 'Position', [0.05, 0.2, 0.4, 0.15], ... 'FontSize', 12, ... 'Callback', @simulateExperiment);
% 创建图像显示区域 hAxes = axes(hFig, ... 'Units', 'normalized', ... 'Position', [0.55, 0.1, 0.4, 0.8], ... 'Box', 'on');
% 更新波长滑条和编辑框 function updateWavelengthSlider(source, eventdata) sliderValue = get(hWavelengthSlider, 'Value'); set(hWavelengthEdit, 'String', num2str(sliderValue)); end
function updateWavelengthEdit(source, eventdata)
editValue = str2double(get(hWavelengthEdit, 'String'));
if isnan(editValue) || editValue < 400 || editValue > 700
set(hWavelengthEdit, 'String', num2str(get(hWavelengthSlider, 'Value')));
else
set(hWavelengthSlider, 'Value', editValue);
end
end
% 仿真实验 function simulateExperiment(source, eventdata) % 获取半径和波长参数 radius = str2double(get(hRadiusEdit, 'String')); wavelength = get(hWavelengthSlider, 'Value');
% 获取实验选项
isStatic = get(hStaticRadio, 'Value');
isIncrease = get(hIncreaseRadio, 'Value');
isDecrease = get(hDecreaseRadio, 'Value');
% 生成网格
[x, y] = meshgrid(-radius:0.1:radius, -radius:0.1:radius);
r = sqrt(x.^2 + y.^2);
% 计算相位差
if isStatic
phase = 2*pi*r/wavelength;
elseif isIncrease
phase = 2*pi*r/wavelength - pi/2*mod(r, wavelength/2)/(wavelength/2);
else
phase = 2*pi*r/wavelength + pi/2*mod(r, wavelength/2)/(wavelength/2);
end
% 计算干涉图
intensity = 1 + cos(phase);
% 显示干涉图
imagesc(x(1,:), y(:,1), intensity);
colormap gray;
axis equal tight;
xlabel('x (mm)');
ylabel('y (mm)');
title('牛顿环实验仿真');
end
end
原文地址: https://www.cveoy.top/t/topic/mOdr 著作权归作者所有。请勿转载和采集!