-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtest_periodic.m
61 lines (55 loc) · 1.54 KB
/
test_periodic.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
function [pass] = test_periodic(plots,verbose)
% This test solves the problem for a solution u(x) = sin(2x) in [0,pi], with
% periodic BCs u(0) = u(pi). The PDE is u''+u = -3sin(2*x). A Poisson
% problem u'' = f might be problematic because with periodic BCs for any
% solution u, u + c is also a solution.
%
if nargin == 0
plots = false;
verbose = false;
elseif nargin == 1
verbose = false;
end
N = 100;
h = pi/N;
[Ix,D1xx,D1xc,D1xb,D1xf] = diff_matrices1d(N, pi/N);
x = 0:h:pi-h; % grid point at x = 0 to x = pi-h;
f = -3*sin(2*x);
u_num = (D1xx+eye(N,N))\f';
u_e = sin(2*x)';
if plots
subplot(1,2,1)
plot(x,u_e,'r')
hold on
plot(x,u_num)
xlim([0 pi]);
hold off
title('Coarse Numerical vs analytical solutions')
subplot(1,2,2)
plot(x,abs(u_e-u_num))
xlim([0 pi]);
title('Absolute error')
end
err_coarse = max(abs(u_e-u_num));
%fine grid;
r = 2; % 2 times refinement
N = r*N;
h = pi/N;
[Ix,D1xx,D1xc,D1xb,D1xf] = diff_matrices1d(N, pi/N);
x = 0:h:pi-h; % grid point at x = 0 to x = pi-h;
f = -3*sin(2*x);
u_num = (D1xx+eye(N,N))\f';
u_e = sin(2*x)';
err_fine = max(abs(u_e-u_num));
if abs(log(err_coarse/err_fine)/log(2) - 2) > 0.1 % arbitrary margin for order of convergence
pass = false;
if verbose
sprintf('Test not passed: the error was reduced by factor %.4g for a %.1g finer grid',err_coarse/err_fine,r)
end
else
pass = true;
if verbose
sprintf('Test passed succesfully: the error was reduced by factor %.4g for a %.1g finer grid',err_coarse/err_fine,r)
end
end
end