-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathdemo_06_bvp.m
55 lines (38 loc) · 1.16 KB
/
demo_06_bvp.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
%% solve a BVP on x \in [0, 1]
% Resulting error is 2nd-order (numerical evidence).
% But this does not follow directly from 2nd-order LTE.
h = 1/8;
x = (h:h:(1-h))'; % interior pts
m = length(x);
e = ones(1, m)';
Dxx = spdiags([e -2*e e], [-1 0 1], m, m);
%full(Dxx)
Dxx = Dxx/h^2;
% RHS function
%f = exp(x) .* sin(4*pi*x);
f = sin(2*pi*x);
% exact soln for comparing/plotting
uex = -1/(4*pi^2)*sin(2*pi*x);
xfine = linspace(0, 1, 512);
uexfine = -1/(4*pi^2)*sin(2*pi*xfine);
% solve Dxx*U = F for U
u = Dxx \ f;
figure(1); clf;
plot(xfine, uexfine, 'r-', 'linewidth', 3);
hold on;
plot(x, uex, 'ro', 'linewidth', 2);
plot(x, u, 'kx-', 'linewidth', 2);
xlabel('x'); ylabel('u');
legend('exact soln', 'sampled exact', 'num soln');
err = max(abs(u - uex))
figure(2); clf;
plot(x, u - uex, 'kx', 'linewidth', 3);
xlabel('x'); ylabel('error');
legend('error vector')
grid on
%% Can also look at stability
% two-norm of the matrix inverse: seems bounded while changing h...
norm_inv = norm(inv(full(Dxx)),2)
% ... because the smallest-in-magnitude eigenvalue of Dxx if
% bounded away from zero (in fact close to pi^2 as in lecture)
min_eigval = min(abs(eig(full(Dxx))))