forked from cbm755/fdmatrices
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_sin.m
46 lines (41 loc) · 930 Bytes
/
test_sin.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
function [pass] = test_sin(plots,verbose)
% This test solves the problem for a solution u(x) = sin(x) in [0,pi]. So then the
% result is u''(x) = -sin(x) = -u(x).
if nargin == 0
plots = false;
verbose = false;
elseif nargin == 1
verbose = false;
end
[Ix,D1xx,D1xc,D1xb,D1xf] = diff_matrices1d(100, pi/100, 'd');
x = linspace(0,pi, 100);
u = sin(x);
uxx = -sin(x);
unum = D1xx*u';
unum = unum';
if plots
subplot(1,2,1)
plot(x,uxx,'r')
hold on
plot(x,unum)
xlim([0 pi]);
hold off
title('Numerical vs analytical solutions')
subplot(1,2,2)
plot(x,abs(uxx-unum))
xlim([0 pi]);
title('Absolute error')
end
err = sum(abs(uxx-unum))*pi/100;
if err > 0.1
pass = false;
if verbose
sprintf('Test not passed: the error was %.4g',err)
end
else
pass = true;
if verbose
sprintf('Test passed succesfully: the error was %.4g',err)
end
end
end