annotate diracDiscr.m @ 652:be941bb0a11a feature/d1_staggered

Add staggered 1D variable coefficient. Convergence study working.
author Martin Almquist <malmquist@stanford.edu>
date Mon, 04 Dec 2017 11:11:03 -0800
parents 4ee7d15bd8e6
children 5264ce57b573
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
645
a5307adc6177 Add diracDiscr.m. Noticed bug if source is on grid point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
1 function ret = diracDiscr(x_0in , x , m_order, s_order, H)
a5307adc6177 Add diracDiscr.m. Noticed bug if source is on grid point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
2
651
4ee7d15bd8e6 Fix roundoff bug in diracDiscr and improve test.
Martin Almquist <malmquist@stanford.edu>
parents: 649
diff changeset
3 m = length(x);
4ee7d15bd8e6 Fix roundoff bug in diracDiscr and improve test.
Martin Almquist <malmquist@stanford.edu>
parents: 649
diff changeset
4
649
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
5 % Return zeros if x0 is outside grid
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
6 if(x_0in < x(1) || x_0in > x(end) )
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
7
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
8 ret = zeros(size(x));
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
9
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
10 else
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
11
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
12 fnorm = diag(H);
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
13 eta = abs(x-x_0in);
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
14 h = x(2)-x(1);
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
15 tot = m_order+s_order;
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
16 S = [];
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
17 M = [];
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
18 poss = find(tot*h/2 >= eta);
651
4ee7d15bd8e6 Fix roundoff bug in diracDiscr and improve test.
Martin Almquist <malmquist@stanford.edu>
parents: 649
diff changeset
19
649
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
20 % Ensure that poss is not too long
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
21 if length(poss) == (tot + 2)
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
22 poss = poss(2:end-1);
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
23 elseif length(poss) == (tot + 1)
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
24 poss = poss(1:end-1);
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
25 end
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
26
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
27 % Use first tot grid points
651
4ee7d15bd8e6 Fix roundoff bug in diracDiscr and improve test.
Martin Almquist <malmquist@stanford.edu>
parents: 649
diff changeset
28 if length(poss)<tot && x_0in < x(1) + ceil(tot/2)*h;
649
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
29 index=1:tot;
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
30 pol=(x(1:tot)-x(1))/(x(tot)-x(1));
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
31 x_0=(x_0in-x(1))/(x(tot)-x(1));
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
32 norm=fnorm(1:tot)/h;
646
0990765e3e4d Fix bug in diracDiscr
Martin Almquist <malmquist@stanford.edu>
parents: 645
diff changeset
33
649
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
34 % Use last tot grid points
651
4ee7d15bd8e6 Fix roundoff bug in diracDiscr and improve test.
Martin Almquist <malmquist@stanford.edu>
parents: 649
diff changeset
35 elseif length(poss)<tot && x_0in > x(end) - ceil(tot/2)*h;
649
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
36 index = length(x)-tot+1:length(x);
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
37 pol = (x(end-tot+1:end)-x(end-tot+1))/(x(end)-x(end-tot+1));
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
38 norm = fnorm(end-tot+1:end)/h;
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
39 x_0 = (x_0in-x(end-tot+1))/(x(end)-x(end-tot+1));
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
40
651
4ee7d15bd8e6 Fix roundoff bug in diracDiscr and improve test.
Martin Almquist <malmquist@stanford.edu>
parents: 649
diff changeset
41 % Interior, compensate for round-off errors.
4ee7d15bd8e6 Fix roundoff bug in diracDiscr and improve test.
Martin Almquist <malmquist@stanford.edu>
parents: 649
diff changeset
42 elseif length(poss) < tot
4ee7d15bd8e6 Fix roundoff bug in diracDiscr and improve test.
Martin Almquist <malmquist@stanford.edu>
parents: 649
diff changeset
43 if poss(end)<m
4ee7d15bd8e6 Fix roundoff bug in diracDiscr and improve test.
Martin Almquist <malmquist@stanford.edu>
parents: 649
diff changeset
44 poss = [poss; poss(end)+1];
4ee7d15bd8e6 Fix roundoff bug in diracDiscr and improve test.
Martin Almquist <malmquist@stanford.edu>
parents: 649
diff changeset
45 else
4ee7d15bd8e6 Fix roundoff bug in diracDiscr and improve test.
Martin Almquist <malmquist@stanford.edu>
parents: 649
diff changeset
46 poss = [poss(1)-1; poss];
4ee7d15bd8e6 Fix roundoff bug in diracDiscr and improve test.
Martin Almquist <malmquist@stanford.edu>
parents: 649
diff changeset
47 end
4ee7d15bd8e6 Fix roundoff bug in diracDiscr and improve test.
Martin Almquist <malmquist@stanford.edu>
parents: 649
diff changeset
48 pol = (x(poss)-x(poss(1)))/(x(poss(end))-x(poss(1)));
4ee7d15bd8e6 Fix roundoff bug in diracDiscr and improve test.
Martin Almquist <malmquist@stanford.edu>
parents: 649
diff changeset
49 x_0 = (x_0in-x(poss(1)))/(x(poss(end))-x(poss(1)));
4ee7d15bd8e6 Fix roundoff bug in diracDiscr and improve test.
Martin Almquist <malmquist@stanford.edu>
parents: 649
diff changeset
50 norm = fnorm(poss)/h;
4ee7d15bd8e6 Fix roundoff bug in diracDiscr and improve test.
Martin Almquist <malmquist@stanford.edu>
parents: 649
diff changeset
51 index = poss;
4ee7d15bd8e6 Fix roundoff bug in diracDiscr and improve test.
Martin Almquist <malmquist@stanford.edu>
parents: 649
diff changeset
52
649
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
53 % Interior
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
54 else
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
55 pol = (x(poss)-x(poss(1)))/(x(poss(end))-x(poss(1)));
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
56 x_0 = (x_0in-x(poss(1)))/(x(poss(end))-x(poss(1)));
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
57 norm = fnorm(poss)/h;
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
58 index = poss;
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
59 end
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
60
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
61 h_pol = pol(2)-pol(1);
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
62 b = zeros(m_order+s_order,1);
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
63
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
64 for i = 1:m_order
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
65 b(i,1) = x_0^(i-1);
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
66 end
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
67
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
68 for i = 1:(m_order+s_order)
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
69 for j = 1:m_order
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
70 M(j,i) = pol(i)^(j-1)*h_pol*norm(i);
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
71 end
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
72 end
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
73
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
74 for i = 1:(m_order+s_order)
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
75 for j = 1:s_order
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
76 S(j,i) = (-1)^(i-1)*pol(i)^(j-1);
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
77 end
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
78 end
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
79
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
80 A = [M;S];
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
81
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
82 d = A\b;
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
83 ret = x*0;
1bdbe026abbc Make diracDiscr return zeros if x0 is outside grid.
Martin Almquist <malmquist@stanford.edu>
parents: 648
diff changeset
84 ret(index) = d/h*h_pol;
646
0990765e3e4d Fix bug in diracDiscr
Martin Almquist <malmquist@stanford.edu>
parents: 645
diff changeset
85 end
645
a5307adc6177 Add diracDiscr.m. Noticed bug if source is on grid point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
86
a5307adc6177 Add diracDiscr.m. Noticed bug if source is on grid point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
87 end
a5307adc6177 Add diracDiscr.m. Noticed bug if source is on grid point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
88
a5307adc6177 Add diracDiscr.m. Noticed bug if source is on grid point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
89
a5307adc6177 Add diracDiscr.m. Noticed bug if source is on grid point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
90
a5307adc6177 Add diracDiscr.m. Noticed bug if source is on grid point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
91
a5307adc6177 Add diracDiscr.m. Noticed bug if source is on grid point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
92
a5307adc6177 Add diracDiscr.m. Noticed bug if source is on grid point.
Martin Almquist <malmquist@stanford.edu>
parents:
diff changeset
93