[编者注:]网友后台私信让编者出一期关于通信方面的内容,这位粉丝是通信工程专业的学生,希望能用DeepSeek帮助学习通信原理课程实验,所以今天在2023年秋季学期大作业中先找一个关于电磁波的,东大的4系以前叫无线电系,后来改为信息科学与工程学院,拥有电磁场与微波技术和信号与信息处理等4个国家二级重点学科。大作业运行成果是动态的,所以,配有视频演示,详细内容见大作业和DeepSeek-R1实现。
一、程序说明
本程序设计初衷是模拟电磁波较理想的情况下的入射和反射以及透射现象。
程序模拟了电磁波在线型介质中正入射的情况,由于该现象主要由相对介电常数和相对磁导率决定,所以不妨将一侧取为真空。
er1=ur1=1
gen1 = sqrt(ur1*er1);
gen2 = sqrt(ur2*er2);
gen11 = sqrt(ur1/er1);
gen22 = sqrt(ur2/er2);
R = (gen22-gen11)/(gen22+gen11); %反射系数
T = 2*gen22/(gen22+gen11); %透射系数
蓝色线:入射波;
红色线:反射波;
黑色线:叠加波;
推荐数值:
相对介电常数 :8
相对磁导率 :1.2
波长:3
波速:0.04
相对介电常数 :1
相对磁导率 :1
波长:3
波速:0.04
对应 不折射
借助此程序,同学们可以清晰感受电磁波的透射和反射过程,可以大致了解电磁波的传播。
二、程序运行情况
1、启动程序后的界面
2、使用滑块调整相对介电常数、相对磁导率、波长、波速等四参数,可观察电磁波的入射和反射以及透射现象。
以下是修改四种参数后各自的运行情况
三、大作业设计视图
四、大作业程序代码视图
classdef diancibo1 < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
GridLayout matlab.ui.container.GridLayout
LeftPanel matlab.ui.container.Panel
Button matlab.ui.control.Button
Slider_4 matlab.ui.control.Slider
Slider_4Label matlab.ui.control.Label
Slider_3 matlab.ui.control.Slider
Slider_3Label matlab.ui.control.Label
Slider_2 matlab.ui.control.Slider
Slider_2Label matlab.ui.control.Label
Slider matlab.ui.control.Slider
Label matlab.ui.control.Label
RightPanel matlab.ui.container.Panel
UIAxes matlab.ui.control.UIAxes
end
% Properties that correspond to apps with auto-reflow
properties (Access = private)
onePanelWidth = 576;
end
% Callbacks that handle component events
methods (Access = private)
% Button pushed function: Button
function ButtonPushed(app, event)
sudu = app.Slider_4.Value; %sudu用于控制图像速度,且与图像的分辨率有关,建议大小为0.01~0.05
bo =app.Slider_3.Value; %bo用于控制图像显示的波长,建议大小为1~5
% % 请注意:sudu*bo最好小一些,否则会导致图像分辨率过低,出现图像bug
ur1 = 1;
er1 = 1;
ur2 = app.Slider.Value;
er2 = app.Slider_2.Value;
w = 2*pi; %角频率,可以改变周期和震荡速度,f = 1, v0=3
k0 = w/bo;
w0 = w*sudu; %后面乘上的数要和x,y的分度保持一致,用于控制图像向右的速度
gen1 = sqrt(ur1*er1);
gen2 = sqrt(ur2*er2); %用于求β
gen11 = sqrt(ur1/er1);
gen22 = sqrt(ur2/er2);
x = -3:sudu*bo/gen1:0;
y = 0:sudu*bo/gen2:3;
po1=0; %用于入射波的数据点计数
po2=0; %用于反射波和驻波的数据点计数
po3=0;
k1 = gen1*k0; %即β1
k2 = gen2*k0; %即β2
R = (gen22-gen11)/(gen22+gen11); %反射系数
T = 2*gen22/(gen22+gen11); %透射系数
for t=1:1500 %循环次数,即动画持续时间
Ei = cos(w0*t-k1*x-pi/2); %计算入射波
Er = R*cos(w0*t+k1*x-pi/2); %计算反射波,折射系数为R
Et = T*cos(w0*t-k2*y-pi/2); %计算透射波,折射系数为T
Ez = Ei + Er; %计算合成驻波
plot(app.UIAxes,[-4,4],[0,0],'k','LineWidth',2); %画y参考轴线,不画的话图像会出问题
hold (app.UIAxes,"on");
plot(app.UIAxes,[0,0],[-3,3],'k','LineWidth',2); %画x参考轴线
axis(app.UIAxes,[-3,3,-3,3]); %固定视角
if t<length(x)
if po1<length(x)
po1=po1+1;
for a=t:length(x)
Ei(a)=0;
end
end
end%通过循环将入射波的传播方式模拟出来,没传到的地方置零
plot(app.UIAxes,x, Ei,'b','LineWidth',1); %绘制入射波
if t>length(x)
if po2<length(x)
po2=po2+1;
for b=1:length(x)-po2
Er(b)=0;
Ez(b)=0;
end
end
plot(app.UIAxes,x, Er,'r','LineWidth',1); %绘制反射波
plot(app.UIAxes,x, Ez,'k','LineWidth',2); %绘制合成驻波
if po3<length(y)
po3=po3+1;
for c=t-length(x):length(y)
Et(c)=0;
end
end
plot(app.UIAxes,y, Et,'k','LineWidth',2); %绘制透射波
end
xlabel(app.UIAxes,'x','FontSize',10) %x标签
ylabel(app.UIAxes,'y','FontSize',10) %z标签
hold (app.UIAxes,"off");
title(app.UIAxes,'均匀平面波正入射','FontSize',10,'color','k');
text(app.UIAxes,1,2.2,'\it理想介质','FontSize',20);
text(app.UIAxes,-2,-2.2,'\itE_y','FontSize',20);
text(app.UIAxes,-2,2.2,'\it真空','FontSize',20);
drawnow;
%更新画面,形成动画
end
end
% Changes arrangement of the app based on UIFigure width
function updateAppLayout(app, event)
currentFigureWidth = app.UIFigure.Position(3);
if(currentFigureWidth <= app.onePanelWidth)
% Change to a 2x1 grid
app.GridLayout.RowHeight = {480, 480};
app.GridLayout.ColumnWidth = {'1x'};
app.RightPanel.Layout.Row = 2;
app.RightPanel.Layout.Column = 1;
else
% Change to a 1x2 grid
app.GridLayout.RowHeight = {'1x'};
app.GridLayout.ColumnWidth = {131, '1x'};
app.RightPanel.Layout.Row = 1;
app.RightPanel.Layout.Column = 2;
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.AutoResizeChildren = 'off';
app.UIFigure.Position = [100 100 640 480];
app.UIFigure.Name = 'MATLAB App';
app.UIFigure.SizeChangedFcn = createCallbackFcn(app, @updateAppLayout, true);
% Create GridLayout
app.GridLayout = uigridlayout(app.UIFigure);
app.GridLayout.ColumnWidth = {131, '1x'};
app.GridLayout.RowHeight = {'1x'};
app.GridLayout.ColumnSpacing = 0;
app.GridLayout.RowSpacing = 0;
app.GridLayout.Padding = [0 0 0 0];
app.GridLayout.Scrollable = 'on';
% Create LeftPanel
app.LeftPanel = uipanel(app.GridLayout);
app.LeftPanel.Layout.Row = 1;
app.LeftPanel.Layout.Column = 1;
% Create Label
app.Label = uilabel(app.LeftPanel);
app.Label.HorizontalAlignment = 'right';
app.Label.Position = [23 417 86 22];
app.Label.Text = '相对介电常数';
% Create Slider
app.Slider = uislider(app.LeftPanel);
app.Slider.Limits = [0 9];
app.Slider.Position = [12 409 108 3];
% Create Slider_2Label
app.Slider_2Label = uilabel(app.LeftPanel);
app.Slider_2Label.HorizontalAlignment = 'right';
app.Slider_2Label.Position = [24 334 65 22];
app.Slider_2Label.Text = '相对磁导率';
% Create Slider_2
app.Slider_2 = uislider(app.LeftPanel);
app.Slider_2.Limits = [0 3];
app.Slider_2.Position = [16 332 104 3];
% Create Slider_3Label
app.Slider_3Label = uilabel(app.LeftPanel);
app.Slider_3Label.HorizontalAlignment = 'right';
app.Slider_3Label.Position = [49 260 29 22];
app.Slider_3Label.Text = '波长';
% Create Slider_3
app.Slider_3 = uislider(app.LeftPanel);
app.Slider_3.Limits = [0 5];
app.Slider_3.Position = [14 248 102 3];
% Create Slider_4Label
app.Slider_4Label = uilabel(app.LeftPanel);
app.Slider_4Label.HorizontalAlignment = 'right';
app.Slider_4Label.Position = [56 182 29 22];
app.Slider_4Label.Text = '波速';
% Create Slider_4
app.Slider_4 = uislider(app.LeftPanel);
app.Slider_4.Limits = [0 0.05];
app.Slider_4.Position = [16 165 95 3];
% Create Button
app.Button = uibutton(app.LeftPanel, 'push');
app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true);
app.Button.Position = [21 80 100 23];
app.Button.Text = '入射光';
% Create RightPanel
app.RightPanel = uipanel(app.GridLayout);
app.RightPanel.Title = '4';
app.RightPanel.Layout.Row = 1;
app.RightPanel.Layout.Column = 2;
% Create UIAxes
app.UIAxes = uiaxes(app.RightPanel);
title(app.UIAxes, 'Title')
xlabel(app.UIAxes, 'X')
ylabel(app.UIAxes, 'Y')
zlabel(app.UIAxes, 'Z')
app.UIAxes.Position = [6 114 497 314];
% 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 = diancibo1
% 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 diancibo1 < matlab.apps.AppBase % Properties that correspond to app components properties (Access = public) UIFigure matlab.ui.Figure GridLayout matlab.ui.container.GridLayout LeftPanel matlab.ui.container.Panel Button matlab.ui.control.Button Slider_4 matlab.ui.control.Slider Slider_4Label matlab.ui.control.Label Slider_3 matlab.ui.control.Slider Slider_3Label matlab.ui.control.Label Slider_2 matlab.ui.control.Slider Slider_2Label matlab.ui.control.Label Slider matlab.ui.control.Slider Label matlab.ui.control.Label RightPanel matlab.ui.container.Panel UIAxes matlab.ui.control.UIAxes end % Properties that correspond to apps with auto-reflow properties (Access = private) onePanelWidth = 576; end % Callbacks that handle component events methods (Access = private) % Button pushed function: Button function ButtonPushed(app, event) sudu = app.Slider_4.Value; %sudu用于控制图像速度,且与图像的分辨率有关,建议大小为0.01~0.05 bo =app.Slider_3.Value; %bo用于控制图像显示的波长,建议大小为1~5 % % 请注意:sudu*bo最好小一些,否则会导致图像分辨率过低,出现图像bug ur1 = 1; er1 = 1; ur2 = app.Slider.Value; er2 = app.Slider_2.Value; w = 2*pi; %角频率,可以改变周期和震荡速录,f = 1, v0=3 k0 = w/bo; w0 = w*sudu; %后面乘上的数要和x,y的分度保持一致,用于控制图像向右的速度 gen1 = sqrt(ur1*er1); gen2 = sqrt(ur2*er2); %用于求β gen11 = sqrt(ur1/er1); gen22 = sqrt(ur2/er2); x = -3:sudu*bo/gen1:0; y = 0:sudu*bo/gen2:3; po1=0; %用于入射波的数据点计数 po2=0; %用于反射波和驻波的数据点计数 po3=0; k1 = gen1*k0; %即β1 k2 = gen2*k0; %即β2 R = (gen22-gen11)/(gen22+gen11); %反射系数 T = 2*gen22/(gen22+gen11); %透射系数 for t=1:1500 %循环次数,即动画持续时间 Ei = cos(w0*t-k1*x-pi/2); %计算入射波 Er = R*cos(w0*t+k1*x-pi/2); %计算反射波,折射系数为R Et = T*cos(w0*t-k2*y-pi/2); %计算透射波,折射系数为T Ez = Ei + Er; %计算合成驻波 plot(app.UIAxes,[-4,4],[0,0],'k','LineWidth',2); %画y参考轴线,不画的话图像会出问题 hold (app.UIAxes,"on"); plot(app.UIAxes,[0,0],[-3,3],'k','LineWidth',2); %画x参考轴线 axis(app.UIAxes,[-3,3,-3,3]); %固定视角 if t<length(x) if po1<length(x) po1=po1+1; for a=t:length(x) Ei(a)=0; end end end %通过循环将入射波的传播方式模拟出来,没传到的地方置零 plot(app.UIAxes,x, Ei,'b','LineWidth',1); %绘制入射波 if t>length(x) if po2<length(x) po2=po2+1; for b=1:length(x)-po2 Er(b)=0; Ez(b)=0; end end plot(app.UIAxes,x, Er,'r','LineWidth',1); %绘制反射波 plot(app.UIAxes,x, Ez,'k','LineWidth',2); %绘制合成驻波 if po3<length(y) po3=po3+1; for c=t-length(x):length(y) Et(c)=0; end end plot(app.UIAxes,y, Et,'k','LineWidth',2); %绘制透射波 end xlabel(app.UIAxes,'x','FontSize',10) %x标签 ylabel(app.UIAxes,'y','FontSize',10) %z标签 hold (app.UIAxes,"off"); title(app.UIAxes,'均匀平面波正入射','FontSize',10,'color','k'); text(app.UIAxes,1,2.2,'\it理想介质','FontSize',20); text(app.UIAxes,-2,-2.2,'\itE_y','FontSize',20); text(app.UIAxes,-2,2.2,'\it真空','FontSize',20); drawnow; %更新画面,形成动画 end end % Changes arrangement of the app based on UIFigure width function updateAppLayout(app, event) currentFigureWidth = app.UIFigure.Position(3); if(currentFigureWidth <= app.onePanelWidth) % Change to a 2x1 grid app.GridLayout.RowHeight = {480, 480}; app.GridLayout.ColumnWidth = {'1x'}; app.RightPanel.Layout.Row = 2; app.RightPanel.Layout.Column = 1; else % Change to a 1x2 grid app.GridLayout.RowHeight = {'1x'}; app.GridLayout.ColumnWidth = {131, '1x'}; app.RightPanel.Layout.Row = 1; app.RightPanel.Layout.Column = 2; 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.AutoResizeChildren = 'off'; app.UIFigure.Position = [100 100 640 480]; app.UIFigure.Name = 'MATLAB App'; app.UIFigure.SizeChangedFcn = createCallbackFcn(app, @updateAppLayout, true); % Create GridLayout app.GridLayout = uigridlayout(app.UIFigure); app.GridLayout.ColumnWidth = {131, '1x'}; app.GridLayout.RowHeight = {'1x'}; app.GridLayout.ColumnSpacing = 0; app.GridLayout.RowSpacing = 0; app.GridLayout.Padding = [0 0 0 0]; app.GridLayout.Scrollable = 'on'; % Create LeftPanel app.LeftPanel = uipanel(app.GridLayout); app.LeftPanel.Layout.Row = 1; app.LeftPanel.Layout.Column = 1; % Create Label app.Label = uilabel(app.LeftPanel); app.Label.HorizontalAlignment = 'right'; app.Label.Position = [23 417 86 22]; app.Label.Text = '相对介电常数'; % Create Slider app.Slider = uislider(app.LeftPanel); app.Slider.Limits = [0 9]; app.Slider.Position = [12 409 108 3]; % Create Slider_2Label app.Slider_2Label = uilabel(app.LeftPanel); app.Slider_2Label.HorizontalAlignment = 'right'; app.Slider_2Label.Position = [24 334 65 22]; app.Slider_2Label.Text = '相对磁导率'; % Create Slider_2 app.Slider_2 = uislider(app.LeftPanel); app.Slider_2.Limits = [0 3]; app.Slider_2.Position = [16 332 104 3]; % Create Slider_3Label app.Slider_3Label = uilabel(app.LeftPanel); app.Slider_3Label.HorizontalAlignment = 'right'; app.Slider_3Label.Position = [49 260 29 22]; app.Slider_3Label.Text = '波长'; % Create Slider_3 app.Slider_3 = uislider(app.LeftPanel); app.Slider_3.Limits = [0 5]; app.Slider_3.Position = [14 248 102 3]; % Create Slider_4Label app.Slider_4Label = uilabel(app.LeftPanel); app.Slider_4Label.HorizontalAlignment = 'right'; app.Slider_4Label.Position = [56 182 29 22]; app.Slider_4Label.Text = '波速'; % Create Slider_4 app.Slider_4 = uislider(app.LeftPanel); app.Slider_4.Limits = [0 0.05]; app.Slider_4.Position = [16 165 95 3]; % Create Button app.Button = uibutton(app.LeftPanel, 'push'); app.Button.ButtonPushedFcn = createCallbackFcn(app, @ButtonPushed, true); app.Button.Position = [21 80 100 23]; app.Button.Text = '入射光'; % Create RightPanel app.RightPanel = uipanel(app.GridLayout); app.RightPanel.Title = '4'; app.RightPanel.Layout.Row = 1; app.RightPanel.Layout.Column = 2; % Create UIAxes app.UIAxes = uiaxes(app.RightPanel); title(app.UIAxes, 'Title') xlabel(app.UIAxes, 'X') ylabel(app.UIAxes, 'Y') zlabel(app.UIAxes, 'Z') app.UIAxes.Position = [6 114 497 314]; % 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 = diancibo1 % 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、回答
大作业内容比较比较多,让DeepSeek最终给了一个可直接运行的脚本m文件,同时,为了节省篇幅,略去了思考过程。
3、程序代码(WaveSimulationScript.m)
function WaveSimulationScript
% 创建主窗口
fig = figure('Name', '电磁波传播模拟器',...
'Position', [100 100 600 600],...
'NumberTitle', 'off',...
'CloseRequestFcn', @closeRequest);
% 初始化参数
ur2 = 1; % 相对磁导率
er2 = 1; % 相对介电常数
lambda = 1; % 波长
v = 0.04; % 波速
x = linspace(-10, 10, 1000); % 坐标轴数据
timerObj = []; % 定时器对象
% 创建UI组件
ax = axes('Parent', fig, 'Position', [0.1 0.4 0.8 0.5]);
ax.XLim = [-10 10];
ax.YLim = [-2 2];
title(ax, '电磁波传播模拟');
xlabel(ax, '位置');
ylabel(ax, '电场强度');
grid(ax, 'on');
% 创建开始/停止按钮
startBtn = uicontrol('Style', 'pushbutton',...
'Position', [50 200 100 22],...
'String', '开始',...
'Callback', @startStopCallback);
% 创建滑块组件
[ur2Slider, ur2Label] = createSlider(fig, 'ur2', '相对磁导率', 1, 10, 180, 8);
[er2Slider, er2Label] = createSlider(fig, 'er2', '相对介电常数', 1, 5, 140, 1.2);
[lambdaSlider, lambdaLabel] = createSlider(fig, 'lambda', '波长', 1, 10, 100, 3);
[vSlider, vLabel] = createSlider(fig, 'v', '波速', 0.01, 0.1, 60, 0.04);
% 启动计时器
tic;
% 嵌套回调函数
function startStopCallback(~,~)
if isempty(timerObj) || ~isvalid(timerObj)
timerObj = timer(...
'ExecutionMode', 'fixedRate',...
'Period', 0.1,...
'TimerFcn', @updatePlot);
end
if strcmp(timerObj.Running, 'off')
start(timerObj);
startBtn.String = '停止';
else
stop(timerObj);
startBtn.String = '开始';
end
end
function updatePlot(~,~)
t = toc;
% 计算入射波
E_incident = sin(2*pi*(x/lambda - v*t));
% 计算反射和透射参数
Z1 = 377; % 真空波阻抗
Z2 = Z1*sqrt(ur2/er2);
Gamma = (Z2 - Z1)/(Z2 + Z1); % 反射系数
Tau = 1 + Gamma; % 透射系数
% 计算区域划分
left_region = x < 0;
right_region = x >= 0;
% 计算总场
E_total = zeros(size(x));
E_total(left_region) = E_incident(left_region) + Gamma*sin(2*pi*(-x(left_region)/lambda - v*t));
E_total(right_region) = Tau*sin(2*pi*(x(right_region)/lambda - v*sqrt(ur2*er2)*t));
% 更新绘图
cla(ax);
hold(ax, 'on');
plot(ax, x, E_incident, 'r--', 'DisplayName', '入射波');
plot(ax, x(left_region), E_total(left_region), 'k:', 'DisplayName', '反射波');
plot(ax, x(right_region), E_total(right_region), 'g-.', 'DisplayName', '透射波');
hold(ax, 'off');
legend(ax, 'Location', 'northeast');
end
function closeRequest(~,~)
% 清理定时器
if ~isempty(timerObj) && isvalid(timerObj)
stop(timerObj);
delete(timerObj);
end
delete(fig);
end
% 修改后的滑块创建函数(嵌套函数)
function [slider, label] = createSlider(parent, tag, text, minVal, maxVal, yPos, initVal)
label = uicontrol('Parent', parent,...
'Style', 'text',...
'String', text,...
'Position', [50 yPos 100 20]);
slider = uicontrol('Parent', parent,...
'Style', 'slider',...
'Min', minVal,...
'Max', maxVal,...
'Value', initVal,...
'Position', [150 yPos 200 20],...
'Tag', tag,...
'Callback', @sliderCallback);
valueLabel = uicontrol('Parent', parent,...
'Style', 'text',...
'String', num2str(initVal, '%.2f'),...
'Position', [360 yPos 60 20]);
function sliderCallback(src,~)
value = src.Value;
valueLabel.String = num2str(value, '%.2f');
% 更新对应参数
switch src.Tag
case 'ur2'
ur2 = value;
case 'er2'
er2 = value;
case 'lambda'
lambda = value;
case 'v'
v = value;
end
end
end
end
4、代码形式的脚本内容
.rtcContent { padding: 30px; } .lineNode {font-size: 10pt; font-family: Menlo, Monaco, Consolas, "Courier New", monospace; font-style: normal; font-weight: normal; }function WaveSimulationScript% 创建主窗口fig = figure('Name', '电磁波传播模拟器',... 'Position', [100 100 600 600],... 'NumberTitle', 'off',... 'CloseRequestFcn', @closeRequest);% 初始化参数ur2 = 1; % 相对磁导率er2 = 1; % 相对介电常数lambda = 1; % 波长v = 0.04; % 波速x = linspace(-10, 10, 1000); % 坐标轴数据timerObj = []; % 定时器对象% 创建UI组件ax = axes('Parent', fig, 'Position', [0.1 0.4 0.8 0.5]);ax.XLim = [-10 10];ax.YLim = [-2 2];title(ax, '电磁波传播模拟');xlabel(ax, '位置');ylabel(ax, '电场强度');grid(ax, 'on');% 创建开始/停止按钮startBtn = uicontrol('Style', 'pushbutton',... 'Position', [50 200 100 22],... 'String', '开始',... 'Callback', @startStopCallback);% 创建滑块组件[ur2Slider, ur2Label] = createSlider(fig, 'ur2', '相对磁导率', 1, 10, 180, 8);[er2Slider, er2Label] = createSlider(fig, 'er2', '相对介电常数', 1, 5, 140, 1.2);[lambdaSlider, lambdaLabel] = createSlider(fig, 'lambda', '波长', 1, 10, 100, 3);[vSlider, vLabel] = createSlider(fig, 'v', '波速', 0.01, 0.1, 60, 0.04);% 启动计时器tic;% 嵌套回调函数 function startStopCallback(~,~) if isempty(timerObj) || ~isvalid(timerObj) timerObj = timer(... 'ExecutionMode', 'fixedRate',... 'Period', 0.1,... 'TimerFcn', @updatePlot); end if strcmp(timerObj.Running, 'off') start(timerObj); startBtn.String = '停止'; else stop(timerObj); startBtn.String = '开始'; end end function updatePlot(~,~) t = toc; % 计算入射波 E_incident = sin(2*pi*(x/lambda - v*t)); % 计算反射和透射参数 Z1 = 377; % 真空波阻抗 Z2 = Z1*sqrt(ur2/er2); Gamma = (Z2 - Z1)/(Z2 + Z1); % 反射系数 Tau = 1 + Gamma; % 透射系数 % 计算区域划分 left_region = x < 0; right_region = x >= 0; % 计算总场 E_total = zeros(size(x)); E_total(left_region) = E_incident(left_region) + Gamma*sin(2*pi*(-x(left_region)/lambda - v*t)); E_total(right_region) = Tau*sin(2*pi*(x(right_region)/lambda - v*sqrt(ur2*er2)*t)); % 更新绘图 cla(ax); hold(ax, 'on'); plot(ax, x, E_incident, 'r--', 'DisplayName', '入射波'); plot(ax, x(left_region), E_total(left_region), 'k:', 'DisplayName', '反射波'); plot(ax, x(right_region), E_total(right_region), 'g-.', 'DisplayName', '透射波'); hold(ax, 'off'); legend(ax, 'Location', 'northeast'); end function closeRequest(~,~) % 清理定时器 if ~isempty(timerObj) && isvalid(timerObj) stop(timerObj); delete(timerObj); end delete(fig); end% 修改后的滑块创建函数(嵌套函数) function [slider, label] = createSlider(parent, tag, text, minVal, maxVal, yPos, initVal) label = uicontrol('Parent', parent,... 'Style', 'text',... 'String', text,... 'Position', [50 yPos 100 20]); slider = uicontrol('Parent', parent,... 'Style', 'slider',... 'Min', minVal,... 'Max', maxVal,... 'Value', initVal,... 'Position', [150 yPos 200 20],... 'Tag', tag,... 'Callback', @sliderCallback); valueLabel = uicontrol('Parent', parent,... 'Style', 'text',... 'String', num2str(initVal, '%.2f'),... 'Position', [360 yPos 60 20]); function sliderCallback(src,~) value = src.Value; valueLabel.String = num2str(value, '%.2f'); % 更新对应参数 switch src.Tag case 'ur2' ur2 = value; case 'er2' er2 = value; case 'lambda' lambda = value; case 'v' v = value; end end endend
5、程序运行情况
(1)启动后的界面
(2)按DeepSeek给的缺省参数设置,开始(开始按钮变为停止),程序开始动态演示
(3)可调整任一滑动条参数,直接变更输出结果
(4)连续调节参数的动态演示录屏
领取专属 10元无门槛券
私享最新 技术干货