Skip to content

Commit 66cf653

Browse files
authored
use Literate in usage examples (#818)
1 parent c46997c commit 66cf653

File tree

6 files changed

+90
-95
lines changed

6 files changed

+90
-95
lines changed

docs/make.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ for (example, included) in [
2020
("Sampling.jl", String[]),
2121
("Periodic.jl", ["Periodic1.jl"]),
2222
("Eigenvalue.jl", String[]),
23-
("NonlinearBVP.jl", ["NonlinearBVP1.jl"])
23+
("NonlinearBVP.jl", ["NonlinearBVP1.jl"]),
24+
("system_of_eqn.jl", String[])
2425
]
2526
filename = joinpath(example_dir, example)
2627
Literate.markdown(filename, output_dir, documenter=true,
@@ -44,6 +45,7 @@ makedocs(
4445
],
4546
"Examples" => [
4647
"generated/ODE.md",
48+
"generated/system_of_eqn.md",
4749
"generated/PDE.md",
4850
"generated/Sampling.md",
4951
"generated/Periodic.md",

docs/src/assets/Harmonic_eigs.pdf

-122 KB
Binary file not shown.

docs/src/usage/equations.md

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -89,69 +89,6 @@ u = [Dirichlet();
8989
u(0.1)
9090
```
9191

92-
## Eigenvalue Problems
93-
94-
In analogy to linear algebra, many differential equations may be posed as eigenvalue problems. That is, for some differential operator ``\mathop{L}``, there are a family of functions ``\mathop{u}_i(x)`` such that
95-
96-
```math
97-
\mathop{L} \mathop{u}_i(x) = λ_i \mathop{u}_i(x),
98-
```
99-
100-
where ``λ_i`` is the ``i^{th}`` eigenvalue of the ``L`` and has a corresponding *eigenfunction* ``\mathop{u}_i(x)``. A classic eigenvalue problem is known as the quantum harmonic oscillator where
101-
102-
```math
103-
\mathop{L} = -\frac{1}{2}\frac{\mathop{d}^2}{\mathop{dx}^2} + \frac{1}{2} x^2,
104-
```
105-
106-
and one demands that ``\mathop{u}(∞) = \mathop{u}(-∞) = 0``. Because we expect the solutions to be exponentially suppressed for large ``x``, we can approximate this with Dirichlet boundary conditions at a 'reasonably large' ``x`` without much difference.
107-
108-
We can express this in ApproxFun as the following:
109-
110-
```julia
111-
x = Fun(-8..8)
112-
L = -𝒟^2/2 + x^2/2
113-
S = space(x)
114-
B = Dirichlet(S)
115-
λ, v = ApproxFun.eigs(B, L, 500,tolerance=1E-10)
116-
```
117-
118-
Note that boundary conditions must be specified in the call to `eigs`. Plotting the first ``20`` eigenfunctions offset vertically by their eigenvalue, we see
119-
120-
![harmonic_eigs](../assets/Harmonic_eigs.pdf)
121-
122-
If the solutions are not relatively constant near the boundary then one should push the boundaries further out.
123-
124-
For problems with different contraints or boundary conditions, `B` can be any zero functional constraint, e.g., `DefiniteIntegral()`.
125-
126-
## Systems of equations
127-
128-
Systems of equations can be handled by creating a matrix of operators and functionals. For example, we can solve the system
129-
130-
```math
131-
\begin{gathered}
132-
\mathop{u}'' - \mathop{u} + 2 \mathop{v} = \mathop{e}^x, \\
133-
\mathop{u} + \mathop{v}' + \mathop{v} = \cos{x}, \\
134-
\mathop{u}(-1) = \mathop{u}'(-1) = \mathop{v}(-1) = 0
135-
\end{gathered}
136-
```
137-
138-
using the following code:
139-
140-
```@repl using-pkgs
141-
x = Fun(); B = Evaluation(Chebyshev(),-1);
142-
A = [B 0;
143-
B*𝒟 0;
144-
0 B;
145-
𝒟^2-I 2I;
146-
I 𝒟+I];
147-
u,v = A \ [0;0;0;exp(x);cos(x)];
148-
u(-1),u'(-1),v(-1)
149-
norm(u''-u+2v-exp(x))
150-
norm(u+v'+v-cos(x))
151-
```
152-
153-
In this example, the automatic space detection failed and so we needed to specify explicitly that the domain space for `B` is `Chebyshev()`.
154-
15592
## QR Factorization
15693

15794
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
@@ -184,24 +121,3 @@ Many PDEs have weak singularities at the corners, in which case it is beneficial
184121
```julia
185122
\(A, [zeros((d));f]; tolerance=1E-6)
186123
```
187-
188-
## Nonlinear equations
189-
190-
There is preliminary support for nonlinear equations, via Newton iteration in function space. Here is a simple two-point boundary value problem:
191-
192-
```math
193-
\begin{gathered}
194-
ϵ \mathop{u}'' + 6(1-x^2) \mathop{u}' + \mathop{u}^2=1, \\
195-
\mathop{u}(-1) = \mathop{u}(1) = 0.
196-
\end{gathered}
197-
```
198-
199-
This can be solved as follows:
200-
201-
```@repl using-pkgs
202-
ϵ = 1/70; x = Fun();
203-
N = u -> [u(-1); u(1); ϵ*u''+6*(1-x^2)*u'+u^2-1];
204-
u0 = x; u = newton(N,u0);
205-
u(-1), u(1)
206-
norm(ϵ*u''+6*(1-x^2)*u'+u^2-1)
207-
```

examples/Eigenvalue.jl

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,52 @@
1-
# # Self-adjoint Eigenvalue Problem
1+
# # Eigenvalue problem
2+
3+
# ## Standard eigenvalue problem
4+
5+
# In analogy to linear algebra, many differential equations may be posed as eigenvalue problems.
6+
# That is, for some differential operator ``\mathop{L}``, there are a family of functions
7+
# ``\mathop{u}_i(x)`` such that
8+
# ```math
9+
# \mathop{L} \mathop{u}_i(x) = λ_i \mathop{u}_i(x),
10+
# ```
11+
# where ``λ_i`` is the ``i^{th}`` eigenvalue of the ``L`` and has a corresponding
12+
# *eigenfunction* ``\mathop{u}_i(x)``.
13+
# A classic eigenvalue problem is known as the quantum harmonic oscillator where
14+
# ```math
15+
# \mathop{L} = -\frac{1}{2}\frac{\mathop{d}^2}{\mathop{dx}^2} + \frac{1}{2} x^2,
16+
# ```
17+
# and one demands that ``\mathop{u}(∞) = \mathop{u}(-∞) = 0``.
18+
# Because we expect the solutions to be exponentially suppressed for large ``x``,
19+
# we can approximate this with Dirichlet boundary conditions at a 'reasonably large' ``x``
20+
# without much difference.
21+
22+
# We can express this in ApproxFun as the following:
23+
using ApproxFun
24+
using LinearAlgebra
25+
26+
x = Fun(-8..8)
27+
V = x^2/2
28+
L = -𝒟^2/2 + V
29+
S = space(x)
30+
B = Dirichlet(S)
31+
λ, v = ApproxFun.eigs(B, L, 500,tolerance=1E-10);
32+
33+
# Note that boundary conditions must be specified in the call to `eigs`.
34+
# Plotting the first ``20`` eigenfunctions offset vertically by their eigenvalue, we see
35+
36+
import Plots
37+
p = Plots.plot(V; legend=false, ylim=(-Inf, λ[22]))
38+
for k=1:20
39+
Plots.plot!(real(v[k]/norm(v[k]) + λ[k]))
40+
end
41+
p
42+
43+
# If the solutions are not relatively constant near the boundary then one should push
44+
# the boundaries further out.
45+
46+
# For problems with different contraints or boundary conditions,
47+
# `B` can be any zero functional constraint, e.g., `DefiniteIntegral()`.
48+
49+
# ## Self-adjoint Eigenvalue Problem
250
# Ref:
351
# [J. L. Aurentz & R. M. Slevinsky (2019), arXiv:1903.08538](https://arxiv.org/abs/1903.08538)
452

@@ -8,13 +56,10 @@
856
# ```
957
# where ``u(\pm 10) = 0``, ``V(x) = ωx^2 + x^4``, and ``ω = 25``.
1058

11-
using ApproxFun
12-
using LinearAlgebra
13-
1459
# Define parameters
1560
ω = 25.0
1661
d = -10..10;
17-
S = Legendre(d)
62+
S = Legendre(d) # Equivalently, Ultraspherical(0.5, d)
1863
NS = NormalizedPolynomialSpace(S) # NormalizedLegendre
1964
D1 = Conversion(S, NS)
2065
D2 = Conversion(NS, S);

examples/NonlinearBVP.jl

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
# # Nonlinear Boundary Value Problem
22

3-
# ## Non-linear differential equation
3+
# There is preliminary support for nonlinear equations, via Newton iteration in function space.
4+
# Here is a simple two-point boundary value problem:
5+
46
# We solve
57
# ```math
68
# Du = 0.001u^{\prime\prime} + 6(1-x^2)u^{\prime} + u^2 - 1 = 0,
79
# ```
810
# subject to the boundary conditions ``u(-1)=1`` and ``u(1)=-0.5``.
9-
# We collectively express the system as
10-
# ```math
11-
# Nu = [u(-1)-1,\,u(1)+0.5,\,Du] = [0,0,0].
12-
# ```
11+
1312
include("NonlinearBVP1.jl")
1413

1514
# We plot the solution

examples/system_of_eqn.jl

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# # Systems of equations
2+
3+
# Systems of equations can be handled by creating a matrix of operators and functionals.
4+
# For example, we can solve the system
5+
6+
# ```math
7+
# \begin{gathered}
8+
# \mathop{u}'' - \mathop{u} + 2 \mathop{v} = \mathop{e}^x, \\
9+
# \mathop{u} + \mathop{v}' + \mathop{v} = \cos{x}, \\
10+
# \mathop{u}(-1) = \mathop{u}'(-1) = \mathop{v}(-1) = 0
11+
# \end{gathered}
12+
# ```
13+
14+
# using the following code:
15+
16+
using ApproxFun
17+
using LinearAlgebra
18+
19+
x = Fun();
20+
B = Evaluation(Chebyshev(),-1);
21+
A = [B 0;
22+
B*𝒟 0;
23+
0 B;
24+
𝒟^2-I 2I;
25+
I 𝒟+I];
26+
u,v = A \ [0;0;0;exp(x);cos(x)];
27+
28+
import Plots
29+
Plots.plot(u, label="u", xlabel="x", legend=:topleft)
30+
Plots.plot!(v, label="v")
31+
32+
# In this example, the automatic space detection failed and so we needed
33+
# to specify explicitly that the domain space for `B` is `Chebyshev()`.

0 commit comments

Comments
 (0)