Con lắc đơn

Video minh hoạ

Code chương trình

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
function Conlacdon
clc
close all
clear all

%% CONSTANTS
g = 9.81;

%% INPUT DATA
L= 1;
phi = pi/3;
dt = 0.01;
tmax = 5*2*pi*sqrt(L/g);

%% Xu ly
t = 0;
x = L*sin(phi);
y = -L*cos(phi);
omega = 0;

t_array = t;
phi_array = phi/pi*180;
omega_array = omega;

%% FIGURE
fig1 = figure('name','Conlacdon','color','black','numbertitle','off');
set(gcf,'Units','normalized');
set(gcf,'Position',[0 0.1 0.5 0.8]);
hold on
fig_daytreo = plot([0 x],[0 y],'y','linewidth',2);
fig_quanang = plot([x y],'ro','MarkerSize',30,'markerfacecolor','r');
ht = title(sprintf('t = %0.2f s',t));
set(gca,'color','k')

axis equal
axis(1.3*[-L L -L L]);

fig_phi = figure('name','Goc lech','color','white','numbertitle','off');
set(gcf,'Units','normalized');
set(gcf,'Position',[0.5 0.1 0.3 0.38]);
graph_phi = plot(t_array,phi_array,'b','linewidth',2);
xlabel('t [s]');
ylabel('\phi [degree]');

fig_omega = figure('name','Van toc goc','color','white','numbertitle','off');
set(gcf,'Units','normalized');
set(gcf,'Position',[0.5 0.52 0.3 0.38]);
graph_omega = plot(t_array,omega_array,'b','linewidth',2);
xlabel('t [s]');
ylabel('\omega [rad/s]');

%% CALCULATION

while t<tmax
t = t+dt;
t_array = [t_array t];

epsilon = -g/L*sin(phi);
omega = omega+epsilon*dt;
phi = phi+omega*dt+0.5*epsilon*dt*dt;
x = L*sin(phi);
y = -L*cos(phi);

phi_array = [phi_array phi/pi*180];
omega_array = [omega_array omega];

figure(fig1);
set(fig_daytreo,'xdata',[0 x],'ydata',[0 y]);
set(fig_quanang,'xdata',x,'ydata',y);
set(ht,'string',sprintf('t = %0.2f s',t));

figure(fig_phi);
set(graph_phi,'xdata',t_array,'ydata',phi_array);

figure(fig_omega);
set(graph_omega,'xdata',t_array,'ydata',omega_array);

pause(0.002);
end

end