[编者注:]以MATLAB大作业选登的方式发布贴子,内容还是以通识课为特点,所以,今天是App Designer平台的物理光学经典实验演示。演示内容包括杨氏双缝、洛埃镜(点光源)、单缝夫琅和费衍射和光栅。把大作业内容总结归纳后再向deepSeek-R1提问,做到deepSeek-R1实现与大作业内容基本一致,便于对比,总结提高。今天的MATLAB大作业成果与deepSeek-R1的实现结果,完成质量都比较高,不分伯仲,值得参考。
一、大作业成果简介
1、程序启动后的界面
2、根据提示器中的描述,输入相关参数,调整波长,可得结果,第一个是杨氏双缝
3、杨氏双缝还有第二窗口
4、杨氏双缝改变(增加)波长后的结果
5、杨氏双缝改变(减小)波长后的结果
6、洛埃镜(点光源)的成果
7、单缝夫琅和费衍射
8、光栅
二、程序设计视图
三、程序代码视图
classdef Optics < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
Label_8 matlab.ui.control.Label
Label_7 matlab.ui.control.Label
Label_6 matlab.ui.control.Label
nmLabel matlab.ui.control.Label
Label_4 matlab.ui.control.Label
TextArea matlab.ui.control.TextArea
Label_3 matlab.ui.control.Label
EditField_3 matlab.ui.control.NumericEditField
Label_2 matlab.ui.control.Label
EditField_2 matlab.ui.control.NumericEditField
mLabel matlab.ui.control.Label
EditField matlab.ui.control.NumericEditField
Label matlab.ui.control.Label
ButtonGroup matlab.ui.container.ButtonGroup
Button_4 matlab.ui.control.RadioButton
Button_3 matlab.ui.control.RadioButton
Button_2 matlab.ui.control.RadioButton
Button matlab.ui.control.RadioButton
nmSlider matlab.ui.control.Slider
nmSliderLabel matlab.ui.control.Label
UIAxes matlab.ui.control.UIAxes
end
properties (Access = private)
R % Description
G % Description
B % Description
WaveLength % Description
end
% Callbacks that handle component events
methods (Access = private)
% Value changed function: nmSlider
function nmSliderValueChanged(app, event)
value = app.nmSlider.Value;
app.WaveLength = value;
app.Label_6.Text = num2str(value);
if 380<=value&&value<410
app.R=0.6-0.41*(410-value)/30;
app.G=0;
app.B=0.39+0.6*(410-value)/30;
elseif 410<=value&&value<440
app.R=0.19-0.19*(440-value)/30;
app.G=0;
app.B=1;
elseif 440<=value&&value<490
app.R=0;
app.G=1-(490-value)/50;
app.B=1;
elseif 490<=value&&value<510
app.R=0;
app.G=1;
app.B=(510-value)/20;
elseif 510<=value&&value<580
app.R=1-(580-value)/70;
app.G=1;
app.B=0;
elseif 580<=value&&value<640
app.R=1;
app.G=(640-value)/60;
app.B=0;
elseif 640<=value&&value<700
app.R=1;
app.G=0;
app.B=0;
else
app.R=0.35-0.65*(780-value)/80;
app.G=0;
app.B=0;
end
end
% Callback function: not associated with a component
function ButtonGroupSizeChanged(app, event)
end
% Selection changed function: ButtonGroup
function ButtonGroupSelectionChanged(app, event)
selectedButton = app.ButtonGroup.SelectedObject;
switch selectedButton.Text
case '杨氏双缝'
app.EditField.Visible = 'on';
app.EditField_2.Visible = 'on';
app.EditField_3.Visible = 'off';
app.TextArea.Value = '请依次输入双缝间距、成像位置';
v1=app.EditField.Value;
v2=app.EditField_2.Value;
k1=(v2-v1/2)/10;
k2=(v2+v1/2)/10;
T=10*app.nmSlider.Value*10^-6/v1;
plot(-1:0.01:1,(cos(pi*(-1:.01:1)/T)).^2,'r-','Parent',app.UIAxes);
plot(zeros(201),-10:0.1:10,'k-','LineWidth',2);
hold on
plot(10*ones(201),-10:0.1:10,'k-','LineWidth',3);
plot(0:10,polyval([k1 v1/2],0:10),'Color',[app.R app.G app.B]);
plot(0:10,polyval([k2 -v1/2],0:10),'Color',[app.R app.G app.B]);
hold off
case '洛埃镜(点光源)'
app.EditField.Visible = 'off';
app.EditField_2.Visible = 'on';
app.EditField_3.Visible = 'on';
app.TextArea.Value = '请依次输入点光源纵坐标、成像位置';
y=app.EditField_2.Value;
v=app.EditField_3.Value;
x=(10*y)/(v+y);
k1=(v-y)/10;
k2=(v+y)/10;
T=10*app.nmSlider.Value*10^-6/2/y;
plot(0:0.01:1,(sin(pi*(0:.01:1)/T)).^2,'r-','Parent',app.UIAxes);
plot(0:10,zeros(11),'k-','LineWidth',2);
hold on
plot(10*ones(201),-10:0.1:10,'k-','LineWidth',3);
plot(0:10,polyval([k1,y],0:10),'Color',[app.R app.G app.B]);
plot(x:0.01:10,polyval([k2 -y],x:0.01:10),'Color',[app.R app.G app.B]);
plot(0:0.01:x,polyval([-k2 y],0:0.01:x),'Color',[app.R app.G app.B]);
hold off
case '单缝夫琅和费衍射'
app.EditField.Visible = 'on';
app.EditField_2.Visible = 'on';
app.EditField_3.Visible = 'off';
app.TextArea.Value = '请依次输入缝宽、出射光束角(度)';
d=app.EditField.Value;
r=pi*app.EditField_2.Value/180;
wl=app.nmSlider.Value;
N=pi*d*sin(-30:.1:30)/wl;
plot(5*tan(-30:.1:30),(1/wl^2).*(sin(N)/N).^2,'r-','Parent',app.UIAxes);
plot(zeros(21),-10:10,'k-','LineWidth',2,'Parent',app.UIAxes);
hold on
plot(5*ones(201),-10:0.1:10,'k-','LineWidth',2);
plot(10*ones(201),-10:0.1:10,'k-','LineWidth',3);
for i=-d/2:0.5:d/2
plot(-5:0,i*ones(6),'Color',[app.R app.G app.B]);
plot(0:5,polyval([tan(r) i],0:5),'Color',[app.R app.G app.B]);
plot(5:10,polyval([-i/5 2*i+5*tan(r)],5:10),'Color',[app.R app.G app.B]);
end
hold off
case '光栅'
app.EditField.Visible = 'on';
app.EditField_2.Visible = 'on';
app.EditField_3.Visible = 'on';
app.TextArea.Value = '请依次输入光栅常数、缝宽、出射光束角';
d=app.EditField.Value;
b=app.EditField_2.Value;
wl=app.nmSlider.Value;
M=pi*b*sin(-30:.1:30)/wl;
N=app.EditField_3.Value;
BB=pi*d*sin(-30:.1:30)/wl;
plot(5*tan(-30:.1:30),(1/wl^2).*(sin(M)/M)^2.*(sin(N.*BB)./sin(BB)).^2,'r-','Parent',app.UIAxes);
end
end
end
% Component initialization
methods (Access = private)
% Create UIFigure and components
function createComponents(app)
% Create UIFigure and hide until all components are created
app.UIFigure = uifigure('Visible', 'off');
app.UIFigure.Position = [100 100 640 455];
app.UIFigure.Name = 'MATLAB App';
% Create UIAxes
app.UIAxes = uiaxes(app.UIFigure);
title(app.UIAxes, '像屏上光强分布')
xlabel(app.UIAxes, 'y')
ylabel(app.UIAxes, 'I')
zlabel(app.UIAxes, 'Z')
app.UIAxes.Position = [33 63 365 275];
% Create nmSliderLabel
app.nmSliderLabel = uilabel(app.UIFigure);
app.nmSliderLabel.HorizontalAlignment = 'right';
app.nmSliderLabel.Position = [414 236 49 22];
app.nmSliderLabel.Text = '波长/nm';
% Create nmSlider
app.nmSlider = uislider(app.UIFigure);
app.nmSlider.Limits = [380 780];
app.nmSlider.ValueChangedFcn = createCallbackFcn(app, @nmSliderValueChanged, true);
app.nmSlider.Position = [484 245 131 3];
app.nmSlider.Value = 400;
% Create ButtonGroup
app.ButtonGroup = uibuttongroup(app.UIFigure);
app.ButtonGroup.SelectionChangedFcn = createCallbackFcn(app, @ButtonGroupSelectionChanged, true);
app.ButtonGroup.Title = '挑选你的光学实验演示';
app.ButtonGroup.Position = [415 276 210 158];
% Create Button
app.Button = uiradiobutton(app.ButtonGroup);
app.Button.Text = '杨氏双缝';
app.Button.Position = [11 75 70 22];
app.Button.Value = true;
% Create Button_2
app.Button_2 = uiradiobutton(app.ButtonGroup);
app.Button_2.Text = '洛埃镜(点光源)';
app.Button_2.Position = [11 53 102 22];
% Create Button_3
app.Button_3 = uiradiobutton(app.ButtonGroup);
app.Button_3.Text = '单缝夫琅和费衍射';
app.Button_3.Position = [11 31 118 22];
% Create Button_4
app.Button_4 = uiradiobutton(app.ButtonGroup);
app.Button_4.Text = '光栅';
app.Button_4.Position = [11 9 46 22];
% Create Label
app.Label = uilabel(app.UIFigure);
app.Label.HorizontalAlignment = 'right';
app.Label.Visible = 'off';
app.Label.Position = [434 172 36 22];
app.Label.Text = '输入1';
% Create EditField
app.EditField = uieditfield(app.UIFigure, 'numeric');
app.EditField.Visible = 'off';
app.EditField.Position = [485 162 109 41];
app.EditField.Value = 12;
% Create mLabel
app.mLabel = uilabel(app.UIFigure);
app.mLabel.HorizontalAlignment = 'right';
app.mLabel.Visible = 'off';
app.mLabel.Position = [434 124 36 22];
app.mLabel.Text = '输入2';
% Create EditField_2
app.EditField_2 = uieditfield(app.UIFigure, 'numeric');
app.EditField_2.Visible = 'off';
app.EditField_2.Position = [485 114 109 41];
app.EditField_2.Value = 20;
% Create Label_2
app.Label_2 = uilabel(app.UIFigure);
app.Label_2.HorizontalAlignment = 'right';
app.Label_2.Visible = 'off';
app.Label_2.Position = [434 73 36 22];
app.Label_2.Text = '输入3';
% Create EditField_3
app.EditField_3 = uieditfield(app.UIFigure, 'numeric');
app.EditField_3.Visible = 'off';
app.EditField_3.Position = [485 63 109 41];
app.EditField_3.Value = 25;
% Create Label_3
app.Label_3 = uilabel(app.UIFigure);
app.Label_3.HorizontalAlignment = 'right';
app.Label_3.Position = [33 362 41 22];
app.Label_3.Text = '提示器';
% Create TextArea
app.TextArea = uitextarea(app.UIFigure);
app.TextArea.Position = [89 355 309 31];
% Create Label_4
app.Label_4 = uilabel(app.UIFigure);
app.Label_4.FontName = '楷体';
app.Label_4.FontSize = 24;
app.Label_4.FontWeight = 'bold';
app.Label_4.Position = [42 393 348 52];
app.Label_4.Text = '物理光学经典实验演示';
% Create nmLabel
app.nmLabel = uilabel(app.UIFigure);
app.nmLabel.HorizontalAlignment = 'right';
app.nmLabel.FontName = '楷体';
app.nmLabel.FontSize = 14;
app.nmLabel.Position = [432 7 96 42];
app.nmLabel.Text = '当前波长/nm:';
% Create Label_6
app.Label_6 = uilabel(app.UIFigure);
app.Label_6.HorizontalAlignment = 'center';
app.Label_6.FontSize = 18;
app.Label_6.Position = [527 7 98 42];
app.Label_6.Text = '---';
% Create Label_7
app.Label_7 = uilabel(app.UIFigure);
app.Label_7.FontName = '楷体';
app.Label_7.FontSize = 8;
app.Label_7.Position = [10 7 297 30];
app.Label_7.Text = {'注:1.光栅光线较为杂乱,难以表示,只有光强分布。'; '2.使用时必须首先拖动波长滑块。'};
% Create Label_8
app.Label_8 = uilabel(app.UIFigure);
app.Label_8.FontName = '等线';
app.Label_8.FontSize = 8;
app.Label_8.Position = [1 433 92 22];
app.Label_8.Text = '06022412蒋汉闻';
% Show the figure after all components are created
app.UIFigure.Visible = 'on';
end
end
% App creation and deletion
methods (Access = public)
% Construct app
function app = Optics
% Create UIFigure and components
createComponents(app)
% Register the app with App Designer
registerApp(app, app.UIFigure)
if nargout == 0
clear app
end
end
% Code that executes before app deletion
function delete(app)
% Delete UIFigure when app is deleted
delete(app.UIFigure)
end
end
end
四、代码形式代码视图
.rtcContent { padding: 30px; } .lineNode {font-size: 10pt; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-style: normal; font-weight: normal; }classdef Optics < matlab.apps.AppBase % Properties that correspond to app components properties (Access = public) UIFigure matlab.ui.Figure Label_8 matlab.ui.control.Label Label_7 matlab.ui.control.Label Label_6 matlab.ui.control.Label nmLabel matlab.ui.control.Label Label_4 matlab.ui.control.Label TextArea matlab.ui.control.TextArea Label_3 matlab.ui.control.Label EditField_3 matlab.ui.control.NumericEditField Label_2 matlab.ui.control.Label EditField_2 matlab.ui.control.NumericEditField mLabel matlab.ui.control.Label EditField matlab.ui.control.NumericEditField Label matlab.ui.control.Label ButtonGroup matlab.ui.container.ButtonGroup Button_4 matlab.ui.control.RadioButton Button_3 matlab.ui.control.RadioButton Button_2 matlab.ui.control.RadioButton Button matlab.ui.control.RadioButton nmSlider matlab.ui.control.Slider nmSliderLabel matlab.ui.control.Label UIAxes matlab.ui.control.UIAxes end properties (Access = private) R % Description G % Description B % Description WaveLength % Description end % Callbacks that handle component events methods (Access = private) % Value changed function: nmSlider function nmSliderValueChanged(app, event) value = app.nmSlider.Value; app.WaveLength = value; app.Label_6.Text = num2str(value); if 380<=value&&value<410 app.R=0.6-0.41*(410-value)/30; app.G=0; app.B=0.39+0.6*(410-value)/30; elseif 410<=value&&value<440 app.R=0.19-0.19*(440-value)/30; app.G=0; app.B=1; elseif 440<=value&&value<490 app.R=0; app.G=1-(490-value)/50; app.B=1; elseif 490<=value&&value<510 app.R=0; app.G=1; app.B=(510-value)/20; elseif 510<=value&&value<580 app.R=1-(580-value)/70; app.G=1; app.B=0; elseif 580<=value&&value<640 app.R=1; app.G=(640-value)/60; app.B=0; elseif 640<=value&&value<700 app.R=1; app.G=0; app.B=0; else app.R=0.35-0.65*(780-value)/80; app.G=0; app.B=0; end end % Callback function: not associated with a component function ButtonGroupSizeChanged(app, event) end % Selection changed function: ButtonGroup function ButtonGroupSelectionChanged(app, event) selectedButton = app.ButtonGroup.SelectedObject; switch selectedButton.Text case '杨氏双缝' app.EditField.Visible = 'on'; app.EditField_2.Visible = 'on'; app.EditField_3.Visible = 'off'; app.TextArea.Value = '请依次输入双缝间距、成像位置'; v1=app.EditField.Value; v2=app.EditField_2.Value; k1=(v2-v1/2)/10; k2=(v2+v1/2)/10; T=10*app.nmSlider.Value*10^-6/v1; plot(-1:0.01:1,(cos(pi*(-1:.01:1)/T)).^2,'r-','Parent',app.UIAxes); plot(zeros(201),-10:0.1:10,'k-','LineWidth',2); hold on plot(10*ones(201),-10:0.1:10,'k-','LineWidth',3); plot(0:10,polyval([k1 v1/2],0:10),'Color',[app.R app.G app.B]); plot(0:10,polyval([k2 -v1/2],0:10),'Color',[app.R app.G app.B]); hold off case '洛埃镜(点光源)' app.EditField.Visible = 'off'; app.EditField_2.Visible = 'on'; app.EditField_3.Visible = 'on'; app.TextArea.Value = '请依次输入点光源纵坐标、成像位置'; y=app.EditField_2.Value; v=app.EditField_3.Value; x=(10*y)/(v+y); k1=(v-y)/10; k2=(v+y)/10; T=10*app.nmSlider.Value*10^-6/2/y; plot(0:0.01:1,(sin(pi*(0:.01:1)/T)).^2,'r-','Parent',app.UIAxes); plot(0:10,zeros(11),'k-','LineWidth',2); hold on plot(10*ones(201),-10:0.1:10,'k-','LineWidth',3); plot(0:10,polyval([k1,y],0:10),'Color',[app.R app.G app.B]); plot(x:0.01:10,polyval([k2 -y],x:0.01:10),'Color',[app.R app.G app.B]); plot(0:0.01:x,polyval([-k2 y],0:0.01:x),'Color',[app.R app.G app.B]); hold off case '单缝夫琅和费衍射' app.EditField.Visible = 'on'; app.EditField_2.Visible = 'on'; app.EditField_3.Visible = 'off'; app.TextArea.Value = '请依次输入缝宽、出射光束角(度)'; d=app.EditField.Value; r=pi*app.EditField_2.Value/180; wl=app.nmSlider.Value; N=pi*d*sin(-30:.1:30)/wl; plot(5*tan(-30:.1:30),(1/wl^2).*(sin(N)/N).^2,'r-','Parent',app.UIAxes); plot(zeros(21),-10:10,'k-','LineWidth',2,'Parent',app.UIAxes); hold on plot(5*ones(201),-10:0.1:10,'k-','LineWidth',2); plot(10*ones(201),-10:0.1:10,'k-','LineWidth',3); for i=-d/2:0.5:d/2 plot(-5:0,i*ones(6),'Color',[app.R app.G app.B]); plot(0:5,polyval([tan(r) i],0:5),'Color',[app.R app.G app.B]); plot(5:10,polyval([-i/5 2*i+5*tan(r)],5:10),'Color',[app.R app.G app.B]); end hold off case '光栅' app.EditField.Visible = 'on'; app.EditField_2.Visible = 'on'; app.EditField_3.Visible = 'on'; app.TextArea.Value = '请依次输入光栅常数、缝宽、出射光束角'; d=app.EditField.Value; b=app.EditField_2.Value; wl=app.nmSlider.Value; M=pi*b*sin(-30:.1:30)/wl; N=app.EditField_3.Value; BB=pi*d*sin(-30:.1:30)/wl; plot(5*tan(-30:.1:30),(1/wl^2).*(sin(M)/M)^2.*(sin(N.*BB)./sin(BB)).^2,'r-','Parent',app.UIAxes); end end end % Component initialization methods (Access = private) % Create UIFigure and components function createComponents(app) % Create UIFigure and hide until all components are created app.UIFigure = uifigure('Visible', 'off'); app.UIFigure.Position = [100 100 640 455]; app.UIFigure.Name = 'MATLAB App'; % Create UIAxes app.UIAxes = uiaxes(app.UIFigure); title(app.UIAxes, '像屏上光强分布') xlabel(app.UIAxes, 'y') ylabel(app.UIAxes, 'I') zlabel(app.UIAxes, 'Z') app.UIAxes.Position = [33 63 365 275]; % Create nmSliderLabel app.nmSliderLabel = uilabel(app.UIFigure); app.nmSliderLabel.HorizontalAlignment = 'right'; app.nmSliderLabel.Position = [414 236 49 22]; app.nmSliderLabel.Text = '波长/nm'; % Create nmSlider app.nmSlider = uislider(app.UIFigure); app.nmSlider.Limits = [380 780]; app.nmSlider.ValueChangedFcn = createCallbackFcn(app, @nmSliderValueChanged, true); app.nmSlider.Position = [484 245 131 3]; app.nmSlider.Value = 400; % Create ButtonGroup app.ButtonGroup = uibuttongroup(app.UIFigure); app.ButtonGroup.SelectionChangedFcn = createCallbackFcn(app, @ButtonGroupSelectionChanged, true); app.ButtonGroup.Title = '挑选你的光学实验演示'; app.ButtonGroup.Position = [415 276 210 158]; % Create Button app.Button = uiradiobutton(app.ButtonGroup); app.Button.Text = '杨氏双缝'; app.Button.Position = [11 75 70 22]; app.Button.Value = true; % Create Button_2 app.Button_2 = uiradiobutton(app.ButtonGroup); app.Button_2.Text = '洛埃镜(点光源)'; app.Button_2.Position = [11 53 102 22]; % Create Button_3 app.Button_3 = uiradiobutton(app.ButtonGroup); app.Button_3.Text = '单缝夫琅和费衍射'; app.Button_3.Position = [11 31 118 22]; % Create Button_4 app.Button_4 = uiradiobutton(app.ButtonGroup); app.Button_4.Text = '光栅'; app.Button_4.Position = [11 9 46 22]; % Create Label app.Label = uilabel(app.UIFigure); app.Label.HorizontalAlignment = 'right'; app.Label.Visible = 'off'; app.Label.Position = [434 172 36 22]; app.Label.Text = '输入1'; % Create EditField app.EditField = uieditfield(app.UIFigure, 'numeric'); app.EditField.Visible = 'off'; app.EditField.Position = [485 162 109 41]; app.EditField.Value = 12; % Create mLabel app.mLabel = uilabel(app.UIFigure); app.mLabel.HorizontalAlignment = 'right'; app.mLabel.Visible = 'off'; app.mLabel.Position = [434 124 36 22]; app.mLabel.Text = '输入2'; % Create EditField_2 app.EditField_2 = uieditfield(app.UIFigure, 'numeric'); app.EditField_2.Visible = 'off'; app.EditField_2.Position = [485 114 109 41]; app.EditField_2.Value = 20; % Create Label_2 app.Label_2 = uilabel(app.UIFigure); app.Label_2.HorizontalAlignment = 'right'; app.Label_2.Visible = 'off'; app.Label_2.Position = [434 73 36 22]; app.Label_2.Text = '输入3'; % Create EditField_3 app.EditField_3 = uieditfield(app.UIFigure, 'numeric'); app.EditField_3.Visible = 'off'; app.EditField_3.Position = [485 63 109 41]; app.EditField_3.Value = 25; % Create Label_3 app.Label_3 = uilabel(app.UIFigure); app.Label_3.HorizontalAlignment = 'right'; app.Label_3.Position = [33 362 41 22]; app.Label_3.Text = '提示器'; % Create TextArea app.TextArea = uitextarea(app.UIFigure); app.TextArea.Position = [89 355 309 31]; % Create Label_4 app.Label_4 = uilabel(app.UIFigure); app.Label_4.FontName = '楷体'; app.Label_4.FontSize = 24; app.Label_4.FontWeight = 'bold'; app.Label_4.Position = [42 393 348 52]; app.Label_4.Text = '物理光学经典实验演示'; % Create nmLabel app.nmLabel = uilabel(app.UIFigure); app.nmLabel.HorizontalAlignment = 'right'; app.nmLabel.FontName = '楷体'; app.nmLabel.FontSize = 14; app.nmLabel.Position = [432 7 96 42]; app.nmLabel.Text = '当前波长/nm:'; % Create Label_6 app.Label_6 = uilabel(app.UIFigure); app.Label_6.HorizontalAlignment = 'center'; app.Label_6.FontSize = 18; app.Label_6.Position = [527 7 98 42]; app.Label_6.Text = '---'; % Create Label_7 app.Label_7 = uilabel(app.UIFigure); app.Label_7.FontName = '楷体'; app.Label_7.FontSize = 8; app.Label_7.Position = [10 7 297 30]; app.Label_7.Text = {'注:1.光栅光线较为杂乱,难以表示,只有光强分布。'; '2.使用时必须首先拖动波长滑块。'}; % Create Label_8 app.Label_8 = uilabel(app.UIFigure); app.Label_8.FontName = '等线'; app.Label_8.FontSize = 8; app.Label_8.Position = [1 433 92 22]; app.Label_8.Text = '06022412蒋汉闻'; % Show the figure after all components are created app.UIFigure.Visible = 'on'; end end % App creation and deletion methods (Access = public) % Construct app function app = Optics % Create UIFigure and components createComponents(app) % Register the app with App Designer registerApp(app, app.UIFigure) if nargout == 0 clear app end end % Code that executes before app deletion function delete(app) % Delete UIFigure when app is deleted delete(app.UIFigure) end endend
五、程序文件目录清单
六、deepSeek-R1实现
1、提问
帮我在matlab环境下使用App Designer工具写一个物理光学经典实验演示的程序,功能包括杨氏双缝、洛埃镜(点光源)、单缝夫琅和费衍射和光栅,要有输入相关参数的编辑框及提示,以及更改波长的滑块,然后通过坐标轴展示图像信息。
2、思考
3、答案
4、主要特点和使用说明
5、运行结果
1、程序启动后的运行结果,使用了startupFcn函数
2、更改参数和波长,通过updateParameters函数可及时看到结果变化。
6、程序代码
classdef OpticalExperiments < matlab.apps.AppBase
% Properties (UI Components)
properties (Access = public)
UIFigure matlab.ui.Figure
ExperimentDropDown matlab.ui.control.DropDown
ParametersPanel matlab.ui.container.Panel
dEditField matlab.ui.control.NumericEditField
aEditField matlab.ui.control.NumericEditField
LEditField matlab.ui.control.NumericEditField
NEditField matlab.ui.control.NumericEditField
WavelengthSlider matlab.ui.control.Slider
UIAxes matlab.ui.control.UIAxes
% 添加更多UI组件...
end
% 属性 (参数存储)
properties (Access = private)
lambda = 632.8e-9; % 默认波长 (632.8nm)
d = 0.1e-3; % 双缝间距/洛埃镜缝宽
a = 0.02e-3; % 单缝宽度
L = 1; % 屏幕距离 (m)
N = 5000; % 光栅刻线数
end
% 回调方法
methods (Access = private)
% 更新参数值
function updateParameters(app)
% 从UI组件获取最新参数值
app.lambda = app.WavelengthSlider.Value * 1e-9;
app.d = app.dEditField.Value * 1e-3;
app.a = app.aEditField.Value * 1e-3;
app.L = app.LEditField.Value;
app.N = app.NEditField.Value;
% 更新绘图
updatePlot(app);
end
% 更新实验绘图
function updatePlot(app)
cla(app.UIAxes);
% 根据选择的实验进行绘图
switch app.ExperimentDropDown.Value
case '杨氏双缝'
plotYoung(app);
case '洛埃镜'
plotLloyd(app);
case '单缝衍射'
plotSingleSlit(app);
case '光栅'
plotGrating(app);
end
end
% 杨氏双缝绘图函数
function plotYoung(app)
y = linspace(-0.02, 0.02, 1000); % 观察屏位置范围
phi = (2*pi/app.lambda) * (app.d*y)/app.L;
I = cos(phi/2).^2; % 干涉强度分布
plot(app.UIAxes, y*1000, I, 'LineWidth', 2);
title(app.UIAxes, '杨氏双缝干涉图样');
xlabel(app.UIAxes, '位置 (mm)');
ylabel(app.UIAxes, '相对光强');
axis(app.UIAxes, 'tight');
end
% 其他实验绘图函数(省略类似结构)...
% 创建UI组件
function createComponents(app)
% 创建主窗口
app.UIFigure = uifigure('Name', '光学实验演示');
app.UIFigure.Position = [100 100 800 600];
% 创建实验选择下拉菜单
app.ExperimentDropDown = uidropdown(app.UIFigure);
app.ExperimentDropDown.Items = {'杨氏双缝', '洛埃镜', '单缝衍射', '光栅'};
app.ExperimentDropDown.Position = [50 550 150 22];
app.ExperimentDropDown.ValueChangedFcn = @(~,~) app.updatePlot;
% 创建参数面板
app.ParametersPanel = uipanel(app.UIFigure);
app.ParametersPanel.Title = '实验参数';
app.ParametersPanel.Position = [50 350 250 180];
% 创建参数输入框
createParamField(app, 'dEditField', '缝间距/缝宽 (mm):', 0.1, [20 120 100 22]);
createParamField(app, 'aEditField', '单缝宽度 (mm):', 0.02, [20 80 100 22]);
createParamField(app, 'LEditField', '屏幕距离 (m):', 1, [20 40 100 22]);
createParamField(app, 'NEditField', '光栅刻线数:', 5000, [20 0 100 22]);
% 创建波长滑块
app.WavelengthSlider = uislider(app.UIFigure);
app.WavelengthSlider.Limits = [400 700];
app.WavelengthSlider.Value = 632.8;
app.WavelengthSlider.Position = [350 50 400 3];
app.WavelengthSlider.ValueChangedFcn = @(~,~) app.updateParameters;
% 创建坐标轴
app.UIAxes = uiaxes(app.UIFigure);
app.UIAxes.Position = [350 150 400 400];
end
% 辅助函数:创建参数输入框
function createParamField(app, fieldName, labelText, defaultValue, position)
uilabel(app.ParametersPanel, 'Text', labelText, 'Position', [position(1) position(2)+25 150 15]);
app.(fieldName) = uieditfield(app.ParametersPanel, 'numeric',...
'Value', defaultValue, 'Position', position);
app.(fieldName).ValueChangedFcn = @(~,~) app.updateParameters;
end
end
% App初始化方法
methods (Access = private)
% 创建UI
function startupFcn(app)
createComponents(app);
updatePlot(app);
end
end
% 公共方法
methods (Access = public)
% 构造函数
function app = OpticalExperiments
startupFcn(app);
registerApp(app, app.UIFigure);
app.UIFigure.Visible = 'on';
end
end
end
7、代码形式程序代码
.rtcContent { padding: 30px; } .lineNode {font-size: 10pt; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-style: normal; font-weight: normal; }classdef OpticalExperiments < matlab.apps.AppBase % Properties (UI Components) properties (Access = public) UIFigure matlab.ui.Figure ExperimentDropDown matlab.ui.control.DropDown ParametersPanel matlab.ui.container.Panel dEditField matlab.ui.control.NumericEditField aEditField matlab.ui.control.NumericEditField LEditField matlab.ui.control.NumericEditField NEditField matlab.ui.control.NumericEditField WavelengthSlider matlab.ui.control.Slider UIAxes matlab.ui.control.UIAxes % 添加更多UI组件... end % 属性 (参数存储) properties (Access = private) lambda = 632.8e-9; % 默认波长 (632.8nm) d = 0.1e-3; % 双缝间距/洛埃镜缝宽 a = 0.02e-3; % 单缝宽度 L = 1; % 屏幕距离 (m) N = 5000; % 光栅刻线数 end % 回调方法 methods (Access = private) % 更新参数值 function updateParameters(app) % 从UI组件获取最新参数值 app.lambda = app.WavelengthSlider.Value * 1e-9; app.d = app.dEditField.Value * 1e-3; app.a = app.aEditField.Value * 1e-3; app.L = app.LEditField.Value; app.N = app.NEditField.Value; % 更新绘图 updatePlot(app); end % 更新实验绘图 function updatePlot(app) cla(app.UIAxes); % 根据选择的实验进行绘图 switch app.ExperimentDropDown.Value case '杨氏双缝' plotYoung(app); case '洛埃镜' plotLloyd(app); case '单缝衍射' plotSingleSlit(app); case '光栅' plotGrating(app); end end % 杨氏双缝绘图函数 function plotYoung(app) y = linspace(-0.02, 0.02, 1000); % 观察屏位置范围 phi = (2*pi/app.lambda) * (app.d*y)/app.L; I = cos(phi/2).^2; % 干涉强度分布 plot(app.UIAxes, y*1000, I, 'LineWidth', 2); title(app.UIAxes, '杨氏双缝干涉图样'); xlabel(app.UIAxes, '位置 (mm)'); ylabel(app.UIAxes, '相对光强'); axis(app.UIAxes, 'tight'); end % 其他实验绘图函数(省略类似结构)... % 创建UI组件 function createComponents(app) % 创建主窗口 app.UIFigure = uifigure('Name', '光学实验演示'); app.UIFigure.Position = [100 100 800 600]; % 创建实验选择下拉菜单 app.ExperimentDropDown = uidropdown(app.UIFigure); app.ExperimentDropDown.Items = {'杨氏双缝', '洛埃镜', '单缝衍射', '光栅'}; app.ExperimentDropDown.Position = [50 550 150 22]; app.ExperimentDropDown.ValueChangedFcn = @(~,~) app.updatePlot; % 创建参数面板 app.ParametersPanel = uipanel(app.UIFigure); app.ParametersPanel.Title = '实验参数'; app.ParametersPanel.Position = [50 350 250 180]; % 创建参数输入框 createParamField(app, 'dEditField', '缝间距/缝宽 (mm):', 0.1, [20 120 100 22]); createParamField(app, 'aEditField', '单缝宽度 (mm):', 0.02, [20 80 100 22]); createParamField(app, 'LEditField', '屏幕距离 (m):', 1, [20 40 100 22]); createParamField(app, 'NEditField', '光栅刻线数:', 5000, [20 0 100 22]); % 创建波长滑块 app.WavelengthSlider = uislider(app.UIFigure); app.WavelengthSlider.Limits = [400 700]; app.WavelengthSlider.Value = 632.8; app.WavelengthSlider.Position = [350 50 400 3]; app.WavelengthSlider.ValueChangedFcn = @(~,~) app.updateParameters; % 创建坐标轴 app.UIAxes = uiaxes(app.UIFigure); app.UIAxes.Position = [350 150 400 400]; end % 辅助函数:创建参数输入框 function createParamField(app, fieldName, labelText, defaultValue, position) uilabel(app.ParametersPanel, 'Text', labelText, 'Position', [position(1) position(2)+25 150 15]); app.(fieldName) = uieditfield(app.ParametersPanel, 'numeric',... 'Value', defaultValue, 'Position', position); app.(fieldName).ValueChangedFcn = @(~,~) app.updateParameters; end end % App初始化方法 methods (Access = private) % 创建UI function startupFcn(app) createComponents(app); updatePlot(app); end end % 公共方法 methods (Access = public) % 构造函数 function app = OpticalExperiments startupFcn(app); registerApp(app, app.UIFigure); app.UIFigure.Visible = 'on'; end endend
最后,正如deepSeek-R1在实现中所提示的,这个程序需要继续补充另外三个实验的具体计算和绘图函数(plotLloyd, plotSingleSlit, plotGrating),也给出了典型的光学公式供参考。因为有大作业参考,这三个函数编者就不完善了。这次deepSeek给出的是傻瓜式的程序,所以,我就更加偷懒了,很快完成deepSeek-R1的实现。