Repository navigation
Expand file tree
/
Copy pathadaptive_vs_fixed_error_step_plot.m
More file actions
112 lines (88 loc) · 3.69 KB
/
Copy pathadaptive_vs_fixed_error_step_plot.m
File metadata and controls
112 lines (88 loc) · 3.69 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
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];
% 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);
V_true = compute_planetary_motion(tspan(2),V0,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;
% fixed step version advances with the first row of B
BogackiFixed.A = Bogacki.A;
BogackiFixed.B = Bogacki.B(1,:)';
BogackiFixed.C = Bogacki.C;
error_desireds = logspace(-9,-2,8);
adapt_errors = zeros(length(error_desireds),1);
adapt_h = zeros(length(error_desireds),1);
for j = 1:length(error_desireds)
[~,X_list,h_avg] = explicit_RK_variable_step_integration(rate_func,tspan,V0,0.1,Bogacki,p,error_desireds(j));
adapt_errors(j) = norm(X_list(end,:)' - V_true);
adapt_h(j) = h_avg;
end
h_refs = logspace(log10(0.04),log10(2e-4),8);
fixed_errors = zeros(length(h_refs),1);
fixed_h = zeros(length(h_refs),1);
for j = 1:length(h_refs)
[~,X_list,h_avg] = explicit_RK_fixed_step_integration(rate_func,tspan,V0,h_refs(j),BogackiFixed);
fixed_errors(j) = norm(X_list(end,:)' - V_true);
fixed_h(j) = h_avg;
end
colors = lines(2);
figure('Color','w','Position',[100 100 1100 650])
loglog(fixed_h,fixed_errors,'o','Color',colors(1,:),'MarkerSize',8,'LineWidth',1.5,'DisplayName','Fixed step')
hold on
loglog(adapt_h,adapt_errors,'s','Color',colors(2,:),'MarkerSize',8,'LineWidth',1.5,'DisplayName','Adaptive step')
% slope and fit line for each integrator
fixed_fit = polyfit(log10(fixed_h),log10(fixed_errors),1);
loglog(fixed_h,10.^polyval(fixed_fit,log10(fixed_h)),'--','Color',colors(1,:),'LineWidth',1.5,...
'DisplayName',sprintf('Fixed step fit ($p=%.2f$)',fixed_fit(1)))
adapt_fit = polyfit(log10(adapt_h),log10(adapt_errors),1);
loglog(adapt_h,10.^polyval(adapt_fit,log10(adapt_h)),'--','Color',colors(2,:),'LineWidth',1.5,...
'DisplayName',sprintf('Adaptive step fit ($p=%.2f$)',adapt_fit(1)))
grid on
box on
set(gca,'FontSize',16,'TickLabelInterpreter','latex')
title('Global Truncation Error vs. Average Step Size, Bogacki-Shampine 3(2)','Interpreter','latex','FontSize',22)
xlabel('Average step size, $h_{avg}$','Interpreter','latex','FontSize',20)
ylabel('Global truncation error','Interpreter','latex','FontSize',20)
legend('Location','northwest','Interpreter','latex','FontSize',14)
exportgraphics(gcf,'adaptive_vs_fixed_error_step_plot.png','Resolution',300)
function [t_list,X_list,h_avg,num_evals] = explicit_RK_fixed_step_integration(rate_func_in,tspan,X0,h_ref,BT_struct)
n_steps = max(1,ceil((tspan(2) - tspan(1))/h_ref));
t_list = linspace(tspan(1),tspan(2),n_steps + 1)';
h_avg = (tspan(2) - tspan(1))/n_steps;
X_list = zeros(n_steps + 1,numel(X0));
X_list(1,:) = X0(:)';
num_evals = 0;
for n = 1:n_steps
[XB,evals] = explicit_RK_step(rate_func_in,t_list(n),X_list(n,:)',h_avg,BT_struct);
X_list(n + 1,:) = XB';
num_evals = num_evals + evals;
end
end
function [XB,num_evals] = explicit_RK_step(rate_func_in,t,XA,h,BT_struct)
XA = XA(:);
s = length(BT_struct.C);
K = zeros(numel(XA),s);
for i = 1:s
X_stage = XA + h*K*BT_struct.A(i,:)';
K(:,i) = rate_func_in(t + BT_struct.C(i)*h,X_stage);
end
XB = XA + h*K*BT_struct.B(:);
num_evals = s;
end
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