English: An intuitive animation that shows how Green's functions that solve a differential equation subject to a point-like source can be superposed to solve it subject to an arbitrary source.
using LinearAlgebra using Plots using Printf function solve(f; x1=0, x2=1) N = length(f) x = Array(range(x1, x2, length=N)) h = x[2] - x[1] diag = fill(+2/h^2, N-2) semidiag = fill(-1/h^2, N-3) L = Tridiagonal(semidiag, diag, semidiag) u = L \ f[2:end-1] u = cat([0], u, [0], dims=1) return x, u end function animate() # Store all Green's function solutions N = 101 U = zeros(N, N) p = plot() for i in 1:N f = [i == j ? 1 : 0 for j in 1:N] x, u = solve(f) U[i,:] = u plot!(p, u) end # Solve a real problem f1 = exp.(-(x.-0.5).^2 / (2*0.01)) x, u1 = solve(f1) u = zeros(N) barw = x[2]-x[1] # plot bars with no gap between them anim = @animate for i in 1:N y = @sprintf("%.2f", (i-1) / (N-1)) # as string f2 = [i == j ? 1 : 0 for j in 1:N] x, u2 = solve(f2) u += u2 * f1[i] colors = [i == j ? :black : :red for j in 1:N] # for some reason, only (1599, 1600) gives a height that is divisible by 2 during mp4 generation plot(layout=(2, 2), size=(1599, 1600), xlims=(0,1), xticks=([0, 0.5, 1], ["\$0\$", "\$x\$", "\$1\$"]), yticks=nothing, bar_width=barw, titlefontsize=40, tickfontsize=40, framestyle=:box, grid=false, legend=nothing, margin=10Plots.mm, top_margin=0Plots.mm) # Plot point-source and Green's function solution bar!(subplot=1, x[i:i], f2[i:i], color=:green, linecolor=:green, bar_width=barw, ylims=(0, 1.10), title="\$\\delta(x-$y)\$") bar!(subplot=2, x, u2, color=:darkgreen, linecolor=:darkgreen, bar_width=barw, ylims=(0, 0.02), title="\$G(x,$y)\$") # Plot full source and full solution bar!(subplot=3, x[1:i], f1[1:i], color=:blue, linecolor=:blue, bar_width=barw, ylims=(0, 1.10), title="\$ \\hat{L}\\,(x) u(x) = f(x < $y) \$") bar!(subplot=3, x[i+1:end], f1[i+1:end], color=:lightgrey, linecolor=:lightgrey, bar_width=barw, ylims=(0, 1.10)) bar!(subplot=4, x, u1, color=:lightgrey, linecolor=:lightgrey, bar_width=barw, ylims=(0, 0.06)) bar!(subplot=4, x, u, color=:darkblue, linecolor=:darkblue, bar_width=barw, ylims=(0, 0.06), title="\$ u(x) = {\\int}_{0}^{$y} \\! f(x') \\, G(x,x') \\, \\mathrm{d} x' \$") end mp4(anim, "green.mp4", fps=5) run(`ffmpeg -i green.mp4 -vf "fps=10,scale=640:640:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" -loop 0 green.gif`) end animate()
Licensing
I, the copyright holder of this work, hereby publish it under the following license:
to share – to copy, distribute and transmit the work
to remix – to adapt the work
Under the following conditions:
attribution – You must give appropriate credit, provide a link to the license, and indicate if changes were made. You may do so in any reasonable manner, but not in any way that suggests the licensor endorses you or your use.
share alike – If you remix, transform, or build upon the material, you must distribute your contributions under the same or compatible license as the original.