-
Notifications
You must be signed in to change notification settings - Fork 5
/
tutorial_03_finite_differences_solution.jl
86 lines (74 loc) · 1.94 KB
/
tutorial_03_finite_differences_solution.jl
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
using PyPlot
using LinearAlgebra
function laplacian(x)
n = length(x)
x = [0;x;1]
d1 = 1.0./(x[2:end] .- x[1:end-1])
d2 = 2.0./(x[3:end] .- x[1:end-2])
return Tridiagonal(
@.( d2[2:end] * d1[2:end-1] ),
@.( -d2 * (d1[1:end-1] + d1[2:end]) ),
@.( d2[1:end-1] * d1[2:end-1] )
)
end
using Test
function test_laplacian()
@testset ("laplacian") begin
@test laplacian([0.1,0.2]) ≈ [
-200 100
200/9 -25
]
end
end
function plot_solution()
# Problem parameters
n = 10
p = 2 # Power for grid biasing
f = x -> 0.25 * x^(-3/2)
u = x -> sqrt(x) - x
clf()
# Plot reference solution
xx = LinRange(0,1,1000)
plot(xx, u.(xx), "k-", label="exact solution")
# Plot finite difference solutions
for (grid,x) = (
("uniform", LinRange(0,1,n+2)[2:end-1]),
("adaptive", LinRange(0,1,n+2)[2:end-1].^p),
)
Δ = laplacian(x)
ũ = -Δ\f.(x)
plot([0;x;1], [0;ũ;0], "-o", label="$grid grid")
end
# Add finishing touches to the plot
xlabel(L"x")
legend()
display(gcf())
end
function convergence()
# Problem parameters
n = 2 .^ (1:14)
p = 2 # Power for grid biasing
f = x -> 0.25 * x^(-3/2)
u = x -> sqrt(x) - x
clf()
# Plot reference lines
loglog(n, n.^-(1/2), "k:", label=L"O(n^{-1/2})")
loglog(n, n.^-2, "k--", label=L"O(n^{-2})")
# Plot the convergence both for uniform and adaptive grids
for (grid,xfun) = (
("uniform", n->LinRange(0,1,n+2)[2:end-1]),
("adaptive", n->LinRange(0,1,n+2)[2:end-1].^p),
)
errors = [begin
x = xfun(n)
Δ = laplacian(x)
ũ = -Δ\f.(x)
norm(ũ .- u.(x), Inf)
end for n in n]
loglog(n, errors, label="$grid grid")
end
# Add finishing touches to the plot
xlabel(L"n")
legend(frameon=false)
display(gcf())
end