From e98390e5b9fbef440981e4fdd1bd81b960f697d1 Mon Sep 17 00:00:00 2001 From: fhl43211 Date: Mon, 9 Nov 2015 21:12:48 -0800 Subject: [PATCH] Test for matrix D1xc Test for matrix D1xc u'(x) = -sin(x) with dirichlet BC --- test_sin_D1xc | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 test_sin_D1xc diff --git a/test_sin_D1xc b/test_sin_D1xc new file mode 100644 index 0000000..7638ffd --- /dev/null +++ b/test_sin_D1xc @@ -0,0 +1,56 @@ +function [pass] = test_sin_D1xc(plots,verbose) +% This test solves the problem with central discretization +%for a solution u(x) = cos(x) in [-pi/2,pi/2]. So then the +% result is u'(x) = -sin(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(-pi/2,pi/2, 100); +u = cos(x); +ux = -sin(x); +unum = D1xc*u'; +unum = unum'; + +if plots + subplot(1,2,1) + plot(x,ux,'r') + hold on + plot(x,unum) + xlim([-pi/2 pi/2]); + hold off + title('Numerical vs analytical solutions') + + subplot(1,2,2) + plot(x,abs(ux-unum)) + xlim([-pi/2 pi/2]); + title('Absolute error') +end + +err1 = max(abs(ux-unum)); +%finer mesh +[Ix,D1xx,D1xc,D1xb,D1xf] = diff_matrices1d(200, pi/200, 'd'); +x = linspace(-pi/2,pi/2, 200); +u = cos(x); +ux = -sin(x); +unum = D1xc*u'; +unum = unum'; +err2 = max(abs(ux-unum)); + + +if abs(log(err2/err1)/log(1/2)-1) > 0.1 + pass = false; + if verbose + sprintf('Matrix D1xc Test not passed: the error reduces at ratio %.4g for h goes down by 2',err1/err2) + end +else + pass = true; + if verbose + sprintf('Matrix D1xc Test passed succesfully: the error reduces at ratio %.4g for h goes down by 2',err1/err2) + end +end +end