-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdriver_p1.m
140 lines (115 loc) · 4.59 KB
/
driver_p1.m
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
% driver for Van der Pol ODE test problem:
% u' = v
% v' = (v - v*u^2)/ep - u
% where u(0) = 2, v(0) = 0, and ep = 0.2, integrated over
% the time interval [0,12].
%
% Daniel R. Reynolds
% Department of Mathematics
% Southern Methodist University
% March 2017
% All Rights Reserved
clear
% set problem parameters
global ep;
ep = 0.2;
fn = @(t,y) [y(2); y(2)*(1-y(1)^2)/ep - y(1)];
fe = @(t,y) [y(2); -y(1)];
fi = @(t,y) [0; y(2)*(1-y(1)^2)/ep];
Jn = @(t,y) [0, 1; -1-2*y(1)*y(2)/ep, (1-y(1)^2)/ep];
Ji = @(t,y) [0, 0; -2*y(1)*y(2)/ep, (1-y(1)^2)/ep];
Es = @EStab_p1;
Tf = 12;
tout = linspace(0,Tf,100);
hmin = 1e-6;
hmax = 1.0;
rtol = 1e-4;
atol = 1e-14*ones(2,1);
u0 = 2;
v0 = 0;
Y0 = [u0; v0];
% plot "true" solution
opts = odeset('RelTol',1e-12, 'AbsTol',atol,'InitialStep',hmin/10, 'MaxStep',hmax);
[t,Ytrue] = ode15s(fn, tout, Y0, opts);
figure()
plot(tout,Ytrue)
xlabel('t','FontSize',12), ylabel('y','FontSize',12)
title('Van der Pol test','FontSize',14)
set(gca,'FontSize',12)
print('-dpng','vanderPol')
% run with an embedded diagonally-implicit RK method
mname = 'TRBDF2-ESDIRK';
B = butcher(mname); s = numel(B(1,:))-1;
fprintf('\nRunning with DIRK integrator: %s (order = %i)\n',mname,B(s+1,1))
[t,Y,ns,nl,~] = solve_DIRK(fn, Jn, tout, Y0, B, rtol, atol, hmin, hmax, hmin);
err_max = max(max(abs(Y'-Ytrue)));
err_rms = sqrt(sum(sum((Y'-Ytrue).^2))/numel(Y));
fprintf('Accuracy/Work Results:\n')
fprintf(' maxerr = %.5e, rmserr = %.5e\n',err_max, err_rms);
fprintf(' steps = %i (stages = %i), linear solves = %i\n',ns,ns*s,nl);
% run with a non-embedded diagonally-implicit RK method
mname = 'Cooper4-ESDIRK';
B = butcher(mname); s = numel(B(1,:))-1;
fprintf('\nRunning with DIRK integrator: %s (order = %i)\n',mname,B(s+1,1))
[t,Y,ns,nl,~] = solve_DIRK(fn, Jn, tout, Y0, B, rtol, atol, hmin, hmax, hmin);
err_max = max(max(abs(Y'-Ytrue)));
err_rms = sqrt(sum(sum((Y'-Ytrue).^2))/numel(Y));
fprintf('Accuracy/Work Results:\n')
fprintf(' maxerr = %.5e, rmserr = %.5e\n',err_max, err_rms);
fprintf(' steps = %i (stages = %i), linear solves = %i\n',ns,ns*s,nl);
% run with a fully-implicit RK method
mname = 'RadauIIA-2-3-IRK';
B = butcher(mname); s = numel(B(1,:))-1;
fprintf('\nRunning with IRK integrator: %s (order = %i)\n',mname,B(s+1,1))
[t,Y,ns,nl,~] = solve_IRK(fn, Jn, tout, Y0, B, rtol, atol, hmin, hmax, hmin);
err_max = max(max(abs(Y'-Ytrue)));
err_rms = sqrt(sum(sum((Y'-Ytrue).^2))/numel(Y));
fprintf('Accuracy/Work Results:\n')
fprintf(' maxerr = %.5e, rmserr = %.5e\n',err_max, err_rms);
fprintf(' steps = %i (stages = %i), linear solves = %i\n',ns,ns*s,nl);
% run with an embedded additive RK method
mname1 = 'ARK3(2)4L[2]SA-ERK';
Be = butcher(mname1); s = numel(Be(1,:))-1;
mname2 = 'ARK3(2)4L[2]SA-ESDIRK';
Bi = butcher(mname2);
fprintf('\nRunning with ARK integrator: %s/%s (order = %i)\n',...
mname1,mname2,Be(s+1,1))
[t,Y,ns,nl] = solve_ARK(fe, fi, Ji, tout, Y0, Be, Bi, rtol, atol, hmin, hmax, hmin);
err_max = max(max(abs(Y'-Ytrue)));
err_rms = sqrt(sum(sum((Y'-Ytrue).^2))/numel(Y));
fprintf('Accuracy/Work Results:\n')
fprintf(' maxerr = %.5e, rmserr = %.5e\n',err_max, err_rms);
fprintf(' steps = %i (stages = %i), linear solves = %i\n',ns,ns*s,nl);
% run with an embedded explicit RK method
mname = 'Bogacki-Shampine-ERK';
B = butcher(mname); s = numel(B(1,:))-1;
fprintf('\nRunning with ERK integrator: %s (order = %i)\n',mname,B(s+1,1))
[t,Y,ns,~] = solve_ERK(fn, Es, tout, Y0, B, rtol, atol, hmin, hmax, hmin);
err_max = max(max(abs(Y'-Ytrue)));
err_rms = sqrt(sum(sum((Y'-Ytrue).^2))/numel(Y));
fprintf('Accuracy/Work Results:\n')
fprintf(' maxerr = %.5e, rmserr = %.5e\n',err_max, err_rms);
fprintf(' steps = %i (stages = %i)\n',ns,ns*s);
% run with a non-embedded explicit RK method
mname = 'ERK-4-4';
B = butcher(mname); s = numel(B(1,:))-1;
fprintf('\nRunning with ERK integrator: %s (order = %i)\n',mname,B(s+1,1))
[t,Y,ns,~] = solve_ERK(fn, Es, tout, Y0, B, rtol, atol, hmin, hmax, hmin);
err_max = max(max(abs(Y'-Ytrue)));
err_rms = sqrt(sum(sum((Y'-Ytrue).^2))/numel(Y));
fprintf('Accuracy/Work Results:\n')
fprintf(' maxerr = %.5e, rmserr = %.5e\n',err_max, err_rms);
fprintf(' steps = %i (stages = %i)\n',ns,ns*s);
% end of script
%------------------------- Utility routines -------------------------%
function dt = EStab_p1(t, y)
% compute the Jacobian ODE RHS
global ep;
J = [0, 1; -1-2*y(1)*y(2)/ep, (1-y(1)^2)/ep];
% determine the largest eigenvalue magnitude
lam = max(abs(eig(J)));
% assume explicit stability region includes Euler stability region
% (this assumes that the eigenvalues are in fact negative).
dt = 1/lam;
% end function
end