jupyter | ||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
黒木玄
2018-05-20~2019-04-03, 2023-06-22
- Copyright 2018,2023 Gen Kuroki
- License: MIT https://opensource.org/licenses/MIT
- Repository: https://github.com/genkuroki/Calculus
このファイルは次の場所できれいに閲覧できる:
このファイルは Julia Box で利用できる.
自分のパソコンにJulia言語をインストールしたい場合には
を参照せよ. 前者は古く, 後者の方が新しい.
論理的に完璧な説明をするつもりはない. 細部のいい加減な部分は自分で訂正・修正せよ.
$ \newcommand\eps{\varepsilon} \newcommand\ds{\displaystyle} \newcommand\Z{{\mathbb Z}} \newcommand\R{{\mathbb R}} \newcommand\C{{\mathbb C}} \newcommand\QED{\text{□}} \newcommand\root{\sqrt} $
using Base.MathConstants
using Base64
using Printf
using Statistics
const e = ℯ
endof(a) = lastindex(a)
linspace(start, stop, length) = range(start, stop, length=length)
using Plots
#gr(); ENV["PLOTS_TEST"] = "true"
#clibrary(:colorcet)
#clibrary(:misc)
default(fmt=:png)
function pngplot(P...; kwargs...)
sleep(0.1)
pngfile = tempname() * ".png"
savefig(plot(P...; kwargs...), pngfile)
showimg("image/png", pngfile)
end
pngplot(; kwargs...) = pngplot(plot!(; kwargs...))
showimg(mime, fn) = open(fn) do f
base64 = base64encode(f)
display("text/html", """<img src="data:$mime;base64,$base64">""")
end
using SymPy
#sympy.init_printing(order="lex") # default
#sympy.init_printing(order="rev-lex")
using SpecialFunctions
using QuadGK
# Override the Base.show definition of SymPy.jl:
# https://github.com/JuliaPy/SymPy.jl/blob/29c5bfd1d10ac53014fa7fef468bc8deccadc2fc/src/types.jl#L87-L105
@eval SymPy function Base.show(io::IO, ::MIME"text/latex", x::SymbolicObject)
print(io, as_markdown("\\displaystyle " * sympy.latex(x, mode="plain", fold_short_frac=false)))
end
@eval SymPy function Base.show(io::IO, ::MIME"text/latex", x::AbstractArray{Sym})
function toeqnarray(x::Vector{Sym})
a = join(["\\displaystyle " * sympy.latex(x[i]) for i in 1:length(x)], "\\\\")
"""\\left[ \\begin{array}{r}$a\\end{array} \\right]"""
end
function toeqnarray(x::AbstractArray{Sym,2})
sz = size(x)
a = join([join("\\displaystyle " .* map(sympy.latex, x[i,:]), "&") for i in 1:sz[1]], "\\\\")
"\\left[ \\begin{array}{" * repeat("r",sz[2]) * "}" * a * "\\end{array}\\right]"
end
print(io, as_markdown(toeqnarray(x)))
end
と定める.
ゆえに,
$$ \begin{aligned} F_n(x) &= n!,e^{1/x}\left( \left[-e^{-t}t^{-n-1}\right]{1/x}^\infty + \int{1/x}^\infty e^{-t}(-(n+1)t^{-n-2})dt \right) \ &= n!,x^{n+1} - F_{n+1}(x). \end{aligned} $$
ゆえに
ゆえに
を
さらに
なので
以上をまとめると
ここで
しかし,
は決して収束しない.
このように全てを足し上げると発散する場合であっても, 有限項と剰余項の和の形式で表わせば, すべてがwell-definedな量だけを使って議論を進めることができる場合がある. 上の場合には剰余項は
[(n, factorial(n)*(1/10)^(n+1)) for n in 7:13]
F₀(x) = exp(1/x)*quadgk(u->exp(-1/(x*u))/u, 0, 1)[1]
F₀_ae(x, n=10) = sum(k->(-1)^k*factorial(k)*x^(k+1), 0:n-1)
@show Y = F₀(1/10)
@show Y_ae9 = F₀_ae(1/10, 9)
@show Y_ae10 = F₀_ae(1/10, 10)
n = 1:20
plot(size=(500,350))
plot!(ylims=(-0.0001,0.0001))
plot!(legend=:top)
plot!(n, F₀_ae.(1/10, n) .- Y, label="error")
このように
その方面の入門書としては次の文献がある. 非常に面白い本なのでおすすめできる.
は指数積分函数と呼ばれる特殊函数の1つである(
が非常に面白い解説になっている. 特殊函数の数値計算の最適化に興味がある人は是非とも閲覧して欲しい.
以下は
の連分数展開による数値計算である. この函数の連分数展開については
の第3章の最後のp.74にある例3.10および第6章のpp.132-133にある例6.4を参照せよ. この本も非常に面白い本なのでおすすめできる.
function G_cf(z; n::Int=2)
cf = 1 + (n+1)/z
for i = n:-1:1
cf = z + (1+i)/cf
cf = 1 + i/cf
end
return 1 / (z + 1/cf)
end
G(z) = exp(z)*quadgk(u->exp(-z/u)/u, 0, 1)[1]
sympy.init_printing(order="lex")
z = symbols("z")
for n in 1:3
cf = G_cf(z, n=n)
display(cf)
end
sympy.init_printing(order="rev-lex")
display(series(sum((-1)^n*factorial(n)*z^(n+1) for n in 0:9), n=11))
for n in 1:3
display(series(G_cf(1/z, n=n), n=11))
end
G_cf(4), G(4)
x = 0.05:0.05:4
@time y = G.(x)
@time y_cf1 = G_cf.(x, n=1)
@time y_cf2 = G_cf.(x, n=2)
@time y_cf3 = G_cf.(x, n=3)
plot(size=(500,350))
plot!(title="y = G(x) = e^x E_1(x)", titlefontsize=11)
plot!(xlims=(0,maximum(x)), ylims=(0,2.3))
plot!(x, y, label="numetical integration")
plot!(x, y_cf1, label="continued fractional approximation n=1", ls=:dash)
plot!(x, y_cf2, label="continued fractional approximation n=2", ls=:dash)
plot!(x, y_cf3, label="continued fractional approximation n=3", ls=:dash)