Code Vật Lý 2
Mô phỏng tần số âm thanh trong hiệu ứng Doppler
Phần code Matlab :
classdef vl2appdesigner < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
UIFigure matlab.ui.Figure
Panel matlab.ui.container.Panel
VTextArea matlab.ui.control.TextArea
VTextAreaLabel matlab.ui.control.Label
fTextArea matlab.ui.control.TextArea
fTextAreaLabel matlab.ui.control.Label
VmLabel matlab.ui.control.Label
VsLabel matlab.ui.control.Label
Button matlab.ui.control.Button
Vm_text matlab.ui.control.TextArea
VmSlider matlab.ui.control.Slider
Vs_text matlab.ui.control.TextArea
VsSlider matlab.ui.control.Slider
UIAxesApproaching matlab.ui.control.UIAxes
UIAxesReceding matlab.ui.control.UIAxes
end
% Callbacks that handle component events
methods (Access = private)
% Value changed function: VsSlider
function VsSliderValueChanged(app, event)
% Lấy giá trị từ slider
VsValue = app.VsSlider.Value;
% Gán giá trị sang TextArea (dạng string)
app.Vs_text.Value = num2str(VsValue);
end
% Value changed function: VmSlider
function VmSliderValueChanged(app, event)
VmValue = app.VmSlider.Value;
app.Vm_text.Value = num2str(VmValue);
end
% Button pushed function: Button
function ButtonPushed(app, event)
VmValue = app.VmSlider.Value;
VsValue = app.VsSlider.Value;
t = linspace(0, 0.02, 1000); % 20ms chia thành 1000 điểm
f = str2double(app.fTextArea.Value);
V = str2double(app.VTextArea.Value);
% Tính toán hiệu ứng Doppler
f_approaching = f * (V + VmValue) / (V - VsValue); % Nguồ
n
tiến lại gần
f_receding = f * (V - VmValue) / (V + VsValue); % Nguồ
n rời
xa
% Tạo tín hiệu âm thanh
y_approaching = sin(2*pi * f_approaching .* t);
y_receding = sin(2*pi * f_receding .* t);
% Vẽ biểu đồtrong App Designer (không dùng figure riêng)
cla(app.UIAxesApproaching); % Xóa đồthị cũ
plot(app.UIAxesApproaching, t, y_approaching, 'r');
title(app.UIAxesApproaching, sprintf('Nguồn tiến lại gầ
n - f =
%.2f Hz', f_approaching));
xlabel(app.UIAxesApproaching, 'Thời gian (s)');
ylabel(app.UIAxesApproaching, 'Biên độ');
grid(app.UIAxesApproaching, 'on');
cla(app.UIAxesReceding);
plot(app.UIAxesReceding, t, y_receding, 'g');
title(app.UIAxesReceding, sprintf('Nguồn rời xa - f = %.2f Hz',
f_receding));
xlabel(app.UIAxesReceding, 'Thời gian (s)');
ylabel(app.UIAxesReceding, 'Biên độ');
grid(app.UIAxesReceding, 'on');
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 480];
app.UIFigure.Name = 'MATLAB App';
% Create UIAxesReceding
app.UIAxesReceding = uiaxes(app.UIFigure);
title(app.UIAxesReceding, 'Rời xa')
xlabel(app.UIAxesReceding, 'X')
ylabel(app.UIAxesReceding, 'Y')
zlabel(app.UIAxesReceding, 'Z')
app.UIAxesReceding.Position = [255 79 335 173];
% Create UIAxesApproaching
app.UIAxesApproaching = uiaxes(app.UIFigure);
title(app.UIAxesApproaching, 'Tiến gần')
xlabel(app.UIAxesApproaching, 'X')
ylabel(app.UIAxesApproaching, 'Y')
zlabel(app.UIAxesApproaching, 'Z')
app.UIAxesApproaching.Position = [255 298 335 166];
% Create Panel
app.Panel = uipanel(app.UIFigure);
app.Panel.Title = 'Panel';
app.Panel.Position = [14 202 230 262];
% Create VsSlider
app.VsSlider = uislider(app.Panel);
app.VsSlider.Orientation = 'vertical';
app.VsSlider.ValueChangedFcn = createCallbackFcn(app,
@VsSliderValueChanged, true);
app.VsSlider.Tag = 'Vs';
app.VsSlider.Position = [23 117 3 63];
% Create Vs_text
app.Vs_text = uitextarea(app.Panel);
app.Vs_text.Tag = 'Vs_text';
app.Vs_text.HorizontalAlignment = 'center';
app.Vs_text.Position = [23 201 43 35];
app.Vs_text.Value = {'0'};
% Create VmSlider
app.VmSlider = uislider(app.Panel);
app.VmSlider.Orientation = 'vertical';
app.VmSlider.ValueChangedFcn = createCallbackFcn(app,
@VmSliderValueChanged, true);
app.VmSlider.Tag = 'Vm';
app.VmSlider.Position = [95 117 3 64];
% Create Vm_text
app.Vm_text = uitextarea(app.Panel);
app.Vm_text.HorizontalAlignment = 'center';
app.Vm_text.Position = [95 201 46 35];
app.Vm_text.Value = {'0'};
% Create Button
app.Button = uibutton(app.Panel, 'push');
app.Button.ButtonPushedFcn = createCallbackFcn(app,
@ButtonPushed, true);
app.Button.Position = [172 200 44 36];
% Create VsLabel
app.VsLabel = uilabel(app.Panel);
app.VsLabel.Position = [39 86 18 19];
app.VsLabel.Text = 'Vs';
% Create VmLabel
app.VmLabel = uilabel(app.Panel);
app.VmLabel.Position = [107 85 21 22];
app.VmLabel.Text = 'Vm';
% Create fTextAreaLabel
app.fTextAreaLabel = uilabel(app.Panel);
app.fTextAreaLabel.HorizontalAlignment = 'center';
app.fTextAreaLabel.Position = [127 18 42 37];
app.fTextAreaLabel.Text = 'f';
% Create fTextArea
app.fTextArea = uitextarea(app.Panel);
app.fTextArea.HorizontalAlignment = 'center';
app.fTextArea.Position = [172 18 44 40];
% Create VTextAreaLabel
app.VTextAreaLabel = uilabel(app.Panel);
app.VTextAreaLabel.HorizontalAlignment = 'center';
app.VTextAreaLabel.Position = [23 18 42 37];
app.VTextAreaLabel.Text = 'V';
% Create VTextArea
app.VTextArea = uitextarea(app.Panel);
app.VTextArea.Position = [68 18 44 40];
% 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 = vl2appdesigner
% 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