Repository navigation
Expand file tree
/
Copy pathstep_clustering_plot.m
More file actions
67 lines (49 loc) · 1.96 KB
/
Copy pathstep_clustering_plot.m
File metadata and controls
67 lines (49 loc) · 1.96 KB
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
clear
clc
orbit_params.m_sun = 1;
orbit_params.m_planet = 1;
orbit_params.G = 40;
% eccentric orbit initial condition
V0 = [8; 0; 0; 1];
error_desired = 1e-3;
% orbital period from Kepler's third law, simulate three periods
mu = orbit_params.G*orbit_params.m_sun;
E = 0.5*norm(V0(3:4))^2 - mu/norm(V0(1:2));
a_orbit = -mu/(2*E);
T = 2*pi*sqrt(a_orbit^3/mu);
tspan = [0, 3*T];
rate_func = @(t,V) gravity_rate_func(t,V,orbit_params);
Bogacki.A = [0,0,0,0; 1/2,0,0,0; 0,3/4,0,0; 2/9,1/3,4/9,0];
Bogacki.B = [2/9,1/3,4/9,0; 7/24,1/4,1/3,1/8];
Bogacki.C = [0; 1/2; 3/4; 1];
p = 3;
[t_list,X_list] = explicit_RK_variable_step_integration(rate_func,tspan,V0,0.1,Bogacki,p,error_desired);
% exact solution on a fine grid
t_exact = linspace(tspan(1),tspan(2),2000)';
V_exact = compute_planetary_motion(t_exact,V0,orbit_params);
comp_labels = {'$x(t)$','$y(t)$'};
figure('Color','w','Position',[100 100 1100 800])
tl = tiledlayout(2,1,'TileSpacing','compact');
ax = gobjects(2,1);
for c = 1:2
ax(c) = nexttile;
hold(ax(c),'on')
plot(ax(c),t_exact,V_exact(:,c),'k-','LineWidth',2,'DisplayName','Exact solution')
plot(ax(c),t_list,X_list(:,c),'ro-','MarkerFaceColor','k','MarkerEdgeColor','k',...
'MarkerSize',3,'LineWidth',1,'DisplayName','Adaptive Bogacki-Shampine 3(2)')
grid(ax(c),'on')
box(ax(c),'on')
set(ax(c),'FontSize',16,'TickLabelInterpreter','latex')
ylabel(ax(c),comp_labels{c},'Interpreter','latex','FontSize',20)
end
set(ax(1),'XTickLabel',[])
title(tl,'Time-Step Clustering of Adaptive Integration, Planetary Motion','Interpreter','latex','FontSize',22)
xlabel(tl,'Time, $t$','Interpreter','latex','FontSize',20)
lgd = legend(ax(1),'Orientation','horizontal','Interpreter','latex','FontSize',14);
lgd.Layout.Tile = 'south';
exportgraphics(gcf,'step_clustering_plot.png','Resolution',300)
function dVdt = gravity_rate_func(t,V,orbit_params)
r = V(1:2);
mu = orbit_params.G*orbit_params.m_sun;
dVdt = [V(3:4); -mu*r/norm(r)^3];
end