-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbashforth_test.m
More file actions
81 lines (62 loc) · 1.67 KB
/
bashforth_test.m
File metadata and controls
81 lines (62 loc) · 1.67 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
clear
close all
clc
set(groot,'defaultAxesTickLabelInterpreter','latex');
set(groot,'defaulttextinterpreter','latex');
set(groot,'defaultLegendInterpreter','latex');
%% model problem
% exponential for checking stability
lambda = -1+1i;
f = @(t, y) lambda*y;
y0 = 1;
% polynomial for checking order (highest exact solution is order)
% m = 4;
% f = @(t, y) m*t.^(m-1);
% y0 = 1;
% r theta orbit IC = (rdot thetadot r theta)
% f = @(t, y) [y(3).*y(2).^2 - 1./y(3).^2; -2./y(3) .*y(1).*y(2); y(1); y(2)];
% y0 = [0; 1.2; 1; 0];
% x y orbit IC = (vx vy rx ry)
% f = @(t, y) [-y(3)./((y(3).^2 + y(4).^2).^(3/2)); -y(4)./((y(3).^2 + y(4).^2).^(3/2)); y(1); y(2)];
% y0 = [0; 1.2; 1; 0];
tspan = [0 10];
h = 0.1;
%% stencil and start
stencil = [0 1 3 6 10 14 18 22 26 30 33 35 36];
%stencil = [0 1 3 5 6];
tstart = -(0:max(stencil))*h;
opts = odeset('reltol', 1e-12);
[t2, y2] = ode45(@(t, y) f(-t, y), -tstart, y0);
start45 = ode45(@(t, y) f(-t, y), [0 (max(stencil)*h)], y0, opts);
ystart = deval(start45, -tstart);
%% test integrator
tic
[t, y] = bashforth(f, tspan, h, stencil, ystart);
toc
%% plots
% for checking stability
figure
hold on
grid on
grid minor
fplot(@(t) real(exp(lambda*t)), tspan)
plot(t, real(y), 'marker', '.')
title("$h\lambda$ = " + h*lambda)
% % for checking order with f = @(t, y) m*t.^(m-1);
% figure
% hold on
% grid on
% grid minor
% set(gca, 'Yscale', 'log')
% plot(t, abs(y - t.^(m)))
% ylim([1e-17 1e17])
% % for plotting orbits
% figure
% hold on
% axis equal
% grid on
% grid minor
% % plot(y(3,:).*cos(y(4,:)), y(3,:).*sin(y(4,:)))
% plot(y(3,:), y(4,:))
% xlim([-2 2])
% ylim([-2 2])