Skip to content

Commit d6f2391

Browse files
authored
Poisson example using Literate (#822)
1 parent dc2dee8 commit d6f2391

File tree

3 files changed

+34
-28
lines changed

3 files changed

+34
-28
lines changed

docs/src/usage/equations.md

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -93,31 +93,3 @@ u(0.1)
9393

9494
Behind the scenes, `A\b` where `A` is an `Operator` is implemented via an adaptive QR factorization. That is, it is equivalent to `qr(A)\b`. (There is a subtlety here in space inferring: `A\b` can use both `A` and `b` to determine the domain space, while `qr(A)` only sees the operator `A`.) Note that `qr` adaptively caches a partial QR Factorization
9595
as it is applied to different right-hand sides, so the same operator can be inverted much more efficiently in subsequent problems.
96-
97-
## Partial differential equations
98-
99-
Partial differential operators are also supported. Here's an example
100-
of solving the Poisson equation with zero boundary conditions:
101-
102-
```julia
103-
d = Domain(-1..1)^2
104-
x,y = Fun(d)
105-
f = exp.(-10(x+0.3)^2-20(y-0.2)^2) # use broadcasting as exp(f) not implemented in 2D
106-
A = [Dirichlet(d);Δ] # Δ is an alias for Laplacian()
107-
@time u = A \ [zeros((d));f] # 4s for ~3k coefficients
108-
```
109-
110-
Using a QR Factorization reduces the cost of subsequent calls substantially:
111-
112-
```julia
113-
QR = qr(A)
114-
@time QR \ [zeros((d));f] # 4s
115-
g = exp.(-10(x+0.2)^2-20(y-0.1)^2)
116-
@time QR \ [zeros((d));g] # 0.09s
117-
```
118-
119-
Many PDEs have weak singularities at the corners, in which case it is beneficial to specify a tolerance to reduce the time:
120-
121-
```julia
122-
\(A, [zeros((d));f]; tolerance=1E-6)
123-
```

examples/PDE.jl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,38 @@
11
# # Solving PDEs
22

3+
# ## Poisson equation
4+
5+
# We solve the Poisson equation
6+
# ```math
7+
# Δu(x,y) = 0
8+
# ```
9+
# on the rectangle `-1..1 × -1..1`,
10+
# with zero boundary conditions:
11+
12+
using ApproxFun
13+
using LinearAlgebra
14+
15+
d = (-1..1)^2
16+
x,y = Fun(d)
17+
f = exp.(-10(x+0.3)^2-20(y-0.2)^2) # use broadcasting as exp(f) not implemented in 2D
18+
A = [Dirichlet(d); Laplacian()]
19+
u = A \ [zeros((d)); f];
20+
21+
# Using a QR Factorization reduces the cost of subsequent calls substantially
22+
23+
QR = qr(A)
24+
u = QR \ [zeros((d)); f];
25+
26+
import Plots
27+
Plots.plot(u; st=:surface, legend=false)
28+
29+
# Many PDEs have weak singularities at the corners,
30+
# in which case it is beneficial to specify a `tolerance` to reduce the time, as:
31+
32+
\(A, [zeros((d)); f]; tolerance=1E-6);
33+
34+
# ## Helmholtz Equation
35+
336
# We solve
437
# ```math
538
# (Δ + 100)u(x,y) = 0

examples/Sampling.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,4 @@ import Plots
1111
Plots.histogram(x; bins=100, normalize = :pdf,
1212
title = "Histogram", xlabel="value", ylabel="frequency",
1313
xlim = (-5,5), ylim=(0, Inf), legend = false)
14+
Plots.plot!(f/sum(f), linewidth=3)

0 commit comments

Comments
 (0)