using Plots, BenchmarkTools
Poisson Equation
\[ \frac{\partial^2 u}{\partial x^2} = b \qquad x \in [0,1] \]
We solve only interior points: the endpoints are set to zero.
\[ u(0) = u(1) = 0, \qquad b = \sin(2\pi x) \]
function plot_solution(x, u)
plot([0;x;1],[0;u;0], label="computed")
scatter!([0;x;1],-sin.(2π*[0;x;1])/(4π^2),label="exact")
end
plot_solution (generic function with 1 method)
= 0.05
Δx = Δx:Δx:1-Δx
x = length(x) N
19
= zeros(N,N)
A for i in 1:N, j in 1:N
abs(i-j) <= 1 && (A[i,j] +=1)
==j && (A[i,j] -=3)
iend
= sin.(2π*x) * Δx^2
B = A \ B u
19-element Vector{Float64}:
-0.007892189393343806
-0.015011836300750243
-0.020662020077425496
-0.024289661368163386
-0.025539661368163387
-0.024289661368163386
-0.0206620200774255
-0.015011836300750247
-0.007892189393343808
-1.5770213417970973e-18
0.007892189393343803
0.015011836300750236
0.02066202007742549
0.024289661368163375
0.025539661368163376
0.02428966136816338
0.020662020077425493
0.015011836300750241
0.007892189393343805
plot_solution(x, u)
SparseArrays
using SparseArrays
= 0.05
Δx = Δx:Δx:1-Δx
x = length(x)
N = sin.(2π*x) * Δx^2 B
19-element Vector{Float64}:
0.0007725424859373687
0.001469463130731183
0.002022542485937369
0.0023776412907378845
0.0025000000000000005
0.0023776412907378845
0.002022542485937369
0.0014694631307311833
0.0007725424859373689
3.0616169978683835e-19
-0.0007725424859373683
-0.0014694631307311829
-0.002022542485937369
-0.0023776412907378845
-0.0025000000000000005
-0.0023776412907378845
-0.0020225424859373693
-0.0014694631307311838
-0.0007725424859373692
= spdiagm( -1 => ones(Float64,N-1),
P 0 => -2*ones(Float64,N),
1 => ones(Float64,N-1))
19×19 SparseMatrixCSC{Float64, Int64} with 55 stored entries:
⎡⠻⣦⡀⠀⠀⠀⠀⠀⠀⠀⎤
⎢⠀⠈⠻⣦⡀⠀⠀⠀⠀⠀⎥
⎢⠀⠀⠀⠈⠻⣦⡀⠀⠀⠀⎥
⎢⠀⠀⠀⠀⠀⠈⠻⣦⡀⠀⎥
⎣⠀⠀⠀⠀⠀⠀⠀⠈⠻⠆⎦
= P \ B u
19-element Vector{Float64}:
-0.007892189393343805
-0.015011836300750241
-0.020662020077425496
-0.024289661368163382
-0.025539661368163383
-0.024289661368163386
-0.0206620200774255
-0.015011836300750248
-0.007892189393343811
-6.308085367188389e-18
0.0078921893933438
0.01501183630075024
0.020662020077425496
0.024289661368163382
0.025539661368163387
0.024289661368163386
0.020662020077425496
0.015011836300750243
0.007892189393343806
plot_solution(x, u)
LinearAlgebra
using LinearAlgebra
= 0.05
Δx = Δx:Δx:1-Δx
x = length(x)
N = sin.(2π*x) * Δx^2 B
19-element Vector{Float64}:
0.0007725424859373687
0.001469463130731183
0.002022542485937369
0.0023776412907378845
0.0025000000000000005
0.0023776412907378845
0.002022542485937369
0.0014694631307311833
0.0007725424859373689
3.0616169978683835e-19
-0.0007725424859373683
-0.0014694631307311829
-0.002022542485937369
-0.0023776412907378845
-0.0025000000000000005
-0.0023776412907378845
-0.0020225424859373693
-0.0014694631307311838
-0.0007725424859373692
= ones(Float64, N-1)
DU = -2ones(Float64, N)
D = ones(Float64, N-1) DL
18-element Vector{Float64}:
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
= Tridiagonal(DL, D, DU) T
19×19 Tridiagonal{Float64, Vector{Float64}}:
-2.0 1.0 ⋅ ⋅ ⋅ ⋅ … ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
1.0 -2.0 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ 1.0 -2.0 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ 1.0 -2.0 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ 1.0 -2.0 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ 1.0 -2.0 … ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ … ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 ⋅ ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ -2.0 1.0 ⋅ ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 -2.0 1.0 ⋅ ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ … ⋅ 1.0 -2.0 1.0 ⋅ ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 -2.0 1.0 ⋅
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 -2.0 1.0
⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ ⋅ 1.0 -2.0
= T \ B u
19-element Vector{Float64}:
-0.007892189393343806
-0.015011836300750243
-0.020662020077425496
-0.024289661368163386
-0.025539661368163387
-0.024289661368163386
-0.0206620200774255
-0.015011836300750247
-0.007892189393343808
-1.5770213417970973e-18
0.007892189393343803
0.015011836300750236
0.02066202007742549
0.024289661368163375
0.025539661368163376
0.02428966136816338
0.020662020077425493
0.015011836300750241
0.007892189393343805
plot_solution(x, u)
LAPACK
using LinearAlgebra
= 0.05
Δx = Δx:Δx:1-Δx ## Solve only interior points: the endpoints are set to zero.
x = length(x)
N = sin.(2π*x) * Δx^2 B
19-element Vector{Float64}:
0.0007725424859373687
0.001469463130731183
0.002022542485937369
0.0023776412907378845
0.0025000000000000005
0.0023776412907378845
0.002022542485937369
0.0014694631307311833
0.0007725424859373689
3.0616169978683835e-19
-0.0007725424859373683
-0.0014694631307311829
-0.002022542485937369
-0.0023776412907378845
-0.0025000000000000005
-0.0023776412907378845
-0.0020225424859373693
-0.0014694631307311838
-0.0007725424859373692
= ones(Float64, N-1)
DU = -2ones(Float64, N)
D = ones(Float64, N-1) DL
18-element Vector{Float64}:
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
1.0
gtsv!(DL, D, DU, B) LAPACK.
19-element Vector{Float64}:
-0.007892189393343806
-0.015011836300750243
-0.020662020077425496
-0.024289661368163386
-0.025539661368163387
-0.024289661368163386
-0.0206620200774255
-0.015011836300750247
-0.007892189393343808
-1.5770213417970973e-18
0.007892189393343803
0.015011836300750236
0.02066202007742549
0.024289661368163375
0.025539661368163376
0.02428966136816338
0.020662020077425493
0.015011836300750241
0.007892189393343805
plot_solution(x, B)