-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathheat2.jl
35 lines (29 loc) · 915 Bytes
/
heat2.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
using DiffEqOperators, DifferentialEquations, Plots
# # Heat Equation
# This example demonstrates how to combine `OrdinaryDiffEq` with `DiffEqOperators` to solve a time-dependent PDE.
# We consider the heat equation on the unit interval, with Dirichlet boundary conditions:
# ∂ₜu = Δu
# u(x=0,t) = a
# u(x=1,t) = b
# u(x, t=0) = u₀(x)
#
# For `a = b = 0` and `u₀(x) = sin(2πx)` a solution is given by:
u_analytic(x, t) = sin(2*π*x) * exp(-t*(2*π)^2)
nknots = 100
h = 1.0/(nknots+1)
knots = range(h, step=h, length=nknots)
ord_deriv = 2
ord_approx = 2
const Δ = CenteredDifference(ord_deriv, ord_approx, h, nknots)
const bc = Dirichlet0BC(Float64)
t0 = 0.0
t1 = 0.03
u0 = u_analytic.(knots, t0)
step(u,p,t) = Δ*bc*u
prob = ODEProblem(step, u0, (t0, t1))
alg = KenCarp4()
sol = solve(prob, alg)
plot(knots,sol(0.0),label="0.0")
plot!(knots,sol(0.1))
plot!(knots,sol(0.2))
plot!(knots,sol(0.3))