Skip to content

Commit bef98ce

Browse files
authored
Add heat equation example (#876)
1 parent b092b4c commit bef98ce

File tree

4 files changed

+57
-14
lines changed

4 files changed

+57
-14
lines changed

docs/make.jl

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,16 @@ function replace_includes(str, included)
1515
return str
1616
end
1717

18-
for (example, included) in [
19-
("ODE.jl", ["ODE_BVP.jl", "ODE_increaseprec.jl"]),
20-
("PDE.jl", ["PDE_Poisson.jl", "PDE_Helmholtz.jl"]),
21-
("Sampling.jl", ["Sampling1.jl"]),
22-
("Periodic.jl", ["Periodic1.jl"]),
23-
("Eigenvalue.jl", ["Eigenvalue_standard.jl", "Eigenvalue_symmetric.jl"]),
24-
("NonlinearBVP.jl", ["NonlinearBVP1.jl", "NonlinearBVP2.jl"]),
25-
("system_of_eqn.jl", ["System1.jl"])
26-
]
18+
function get_included_files(filename)
19+
v = [l for l in eachline(joinpath(example_dir, filename)) if contains(l, "include")]
20+
strip.(getindex.(split.(getindex.(split.(v, "include("), 2), ")"), 1), '\"')
21+
end
22+
23+
file_with_includes(filename) = (filename, get_included_files(filename))
24+
25+
for (example, included) in map(file_with_includes,
26+
["ODE.jl", "PDE.jl", "Sampling.jl", "Periodic.jl",
27+
"Eigenvalue.jl", "NonlinearBVP.jl", "system_of_eqn.jl"])
2728
filename = joinpath(example_dir, example)
2829
Literate.markdown(filename, output_dir, documenter=true,
2930
preprocess = str -> replace_includes(str, included))
@@ -32,7 +33,7 @@ end
3233
makedocs(
3334
format = Documenter.HTML(),
3435
sitename = "ApproxFun.jl",
35-
authors = "Sheehan Olver",
36+
authors = "Sheehan Olver, Jishnu Bhattacharya and contributors",
3637
pages = Any[
3738
"Home" => "index.md",
3839
"Usage" => Any[

docs/src/library.md

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ Circle
1313
Curve
1414
```
1515

16-
```@docs
17-
ApproxFunFourier.Disk
18-
```
19-
2016
```@docs
2117
Segment
2218
```

examples/PDE.jl

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,14 @@ include("PDE_Helmholtz.jl")
1515
# Plot the solution
1616
import Plots
1717
Plots.plot(u; st=:surface, legend=false)
18+
19+
include("PDE_Heat.jl")
20+
21+
# Plot the solution
22+
import Plots
23+
xplot = -1:0.02:1
24+
p = Plots.plot(xplot, u.(xplot, 0), label="t=0", legend=true, linewidth=2)
25+
for t in [0.05, 0.1, 0.2, 0.5, 0.8]
26+
Plots.plot!(xplot, u.(xplot, t), label="t=$t")
27+
end
28+
p

examples/PDE_Heat.jl

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# ## Heat equation
2+
3+
# We solve the heat equation
4+
# ```math
5+
# \frac{\partial}{\partial t} u(x,t) = \frac{\partial^2}{\partial x^2} u(x,t)
6+
# ```
7+
# on the spatial domain `-1..1` from ``t=0`` to ``t=1``,
8+
# with zero boundary conditions, and the initial condition
9+
# ``u(x,0) = u_0(x)``, where we choose ``u_0(x)`` to be a sharp Gaussian.
10+
11+
using ApproxFun
12+
using LinearAlgebra
13+
14+
dx = -1..1;
15+
dt = 0..1;
16+
17+
# We construct a 2D domain that is the tensor product of x and t
18+
d = dx × dt;
19+
20+
# The initial condition
21+
u0 = Fun(x->exp(-x^2/(2*0.2^2)), dx);
22+
23+
# We define the derivatives on the 2D domain
24+
Dx = Derivative(d,[1,0]);
25+
Dt = Derivative(d,[0,1]);
26+
27+
# The operator along with the initial and the boundary conditions
28+
A = [Dt - Dx^2; Ildirichlet(dt); bvp(dx)I];
29+
30+
# We collect the initial condition along with zeros for the two boundary conditions and the equation
31+
rhs = [0.0; u0; 0.0; 0.0];
32+
33+
# Solve the equation with an arbitrary tolerance.
34+
# A lower tolerance will increase accuracy at the expense of execution time
35+
u = \(A, rhs, tolerance=1e-4);

0 commit comments

Comments
 (0)