-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathidc_test_lobatto.m
127 lines (98 loc) · 2.99 KB
/
idc_test_lobatto.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
% testing idc in code on vector ode
clear;
plot_str{1} = 'ko-'; % 1st order
plot_str{2} = 'b^-'; % 2nd
plot_str{3} = 'r*-'; % 3rd
plot_str{4} = 'cx-'; % 4th
plot_str{5} = 'gd-'; % 5th
plot_str{6} = 'ys-'; % 6th
plot_str{7} = 'k+--'; % 7th
plot_str{8} = 'b<--'; %8th
%ode = @scalar_ode ;
%tspan = [0 1]; % initial and final time
%y0 = 1; % initial condition
%opts.exact = 0.5; %exact solution if it exists
%ti = tspan(1);
%tf = tspan(2);
% ode = @vector_ode ;
% tspan = [0 1]; % initial and final time
% y0 = [1;1]; % initial condition
% ti = tspan(1);
% tf = tspan(2);
% opts.exact = [exp(tf)*(cos(tf^2/2)+sin(tf^2/2));
% exp(tf)*(cos(tf^2/2)-sin(tf^2/2))]; %exact solution if it exists
ode = @nbody ;
tspan = [0 1e-3]; % initial and final time
% initial condition
nplus = 100;
nminus = 100;
xip = ([1:nplus]'-0.5)/nplus;
xim = ([1:nminus]'-0.5)/nminus;
y0 = [ xip;
zeros(nplus,1);...
xim;
sin(6*pi*xim)];
ti = tspan(1);
tf = tspan(2);
% testing sdc/idc
opts.dc = 2;
PRINTOUTPUT=1;
%%%%%%%%%%%%%%%%%%%%%%%%%%%
disp('IDC, Gauss--Lobatto');
opts.grid = 4; % gauss--legendre
figure(7), clf
opts.pred = 2; % RK2 - Midpoint
opts.corr = 2; % RK2
expected_order = [ 2 4 6];
quad = [2 3 4];
for k = 1:3
opts.levels = k; % number of quadrature points needed
opts.nquad = quad(k); % number of levels (predictor + (levels-1)
% correctors
% convergence study
Nk = 4;
m = length(y0);
y_store = zeros(m,Nk);
N_store = zeros(1,Nk);
for kk = 1:Nk
N = 2^(kk+5); % number of large (equi-spaced) time interavls
dt = (tf - ti)/N; % grid spacing, large intervals
tspan = [0:N]*dt;
sol = deferred_correction(ode,tspan,y0,opts);
y_store(:,kk) = sol(:,end);
N_store(1,kk) = N;
end
if PRINTOUTPUT, fprintf('\n'); end
if isfield(opts,'exact')
% exact solution exists
err_store = zeros(1,Nk);
for kk = 1:Nk
err_store(kk) = max(abs(y_store(:,kk)-opts.exact));
if PRINTOUTPUT
fprintf('(%d,%g)\n',N_store(kk),err_store(kk));
end
end
loglog(N_store,err_store,plot_str{k});
p = polyfit(log(N_store),log(err_store),1);
rate = p(1);
legend_str{k} = sprintf('slope=%4.1f',abs(rate));
else
err_store = zeros(1,Nk-1);
for kk = 1:(Nk - 1)
err_store(kk) = max(abs(y_store(kk+1)-y_store(kk)));
if PRINTOUTPUT
fprintf('(%d,%g)\n',N_store(kk),err_store(kk));
end
end
loglog(N_store(1:end-1),err_store,plot_str{k});
p = polyfit(log(N_store(1:end-1)),log(err_store),1);
rate = p(1);
legend_str{k} = sprintf('slope=%4.1f',abs(rate));
end
xlabel('number of intervals');
ylabel('absolute error');
title('SDC, Gauss--Legendre, RK2')
set(gca,'FontSize',24)
hold on
end
legend(legend_str,'Location','NorthEastOutside');