10 KiB
10 KiB
Cell:
[Cell type raw - unsupported, skipped]
In [ ]:
#| error: false
#| echo: false
#| output: false
using PlotlyJS, Random
using HypertextLiteral
using JSON, UUIDs
using Base64
## see https://github.com/JuliaPlots/PlotlyJS.jl/blob/master/src/PlotlyJS.jl
## https://discourse.julialang.org/t/encode-a-plot-to-base64/27765/3
function IJulia.display_dict(p::PlotlyJS.SyncPlot)
Dict(
# "application/vnd.plotly.v1+json" => JSON.lower(p),
# "text/plain" => sprint(show, "text/plain", p),
"text/html" => let
buf = IOBuffer()
show(buf, MIME("text/html"), p)
#close(buf)
#String(resize!(buf.data, buf.size))
String(take!(buf))
end,
"image/png" => let
buf = IOBuffer()
buf64 = Base64EncodePipe(buf)
show(buf64, MIME("image/png"), p)
close(buf64)
#String(resize!(buf.data, buf.size))
String(take!(buf))
end,
)
end
function Base.show(io::IO, mimetype::MIME"text/html", p::PlotlyJS.SyncPlot)
uuid = string(UUIDs.uuid4())
show(io,mimetype,@htl("""
<div style="height: auto" id=\"$(uuid)\"></div>
<script>
require(['../js/plotly-latest.min.js'], function(plotly) {
plotly.newPlot($(uuid),
$(HypertextLiteral.JavaScript(json(p.plot.data))),
$(HypertextLiteral.JavaScript(json(p.plot.layout))),{responsive: true});
});
</script>
"""))
end In [ ]:
using Printf
iterationA(s) = sqrt(2 - sqrt(4 - s^2))
iterationB(s) = s / sqrt(2 + sqrt(4 - s^2))
s_B = s_A = sqrt(2) # Startwert
ϵ(x) = abs(x - 2π)/2π # rel. Fehler
ϵ_A = Float64[] # Vektoren für den Plot
ϵ_B = Float64[]
is = Float64[]
@printf """ approx. Wert von π
n-Eck Iteration A Iteration B
"""
for i in 3:35
push!(is, i)
s_A = iterationA(s_A)
s_B = iterationB(s_B)
doublePi_A = 2^i * s_A
doublePi_B = 2^i * s_B
push!(ϵ_A, ϵ(doublePi_A))
push!(ϵ_B, ϵ(doublePi_B))
@printf "%14i %20.15f %20.15f\n" 2^i doublePi_A/2 doublePi_B/2
endIn [ ]:
using PlotlyJS
layout = Layout(xaxis_title="Iterationsschritte", yaxis_title="rel. Fehler",
yaxis_type="log", yaxis_exponentformat="power",
xaxis_tick0=2, xaxis_dtick=2)
plot([scatter(x=is, y=ϵ_A, mode="markers+lines", name="Iteration A", yscale=:log10),
scatter(x=is, y=ϵ_B, mode="markers+lines", name="Iteration B", yscale=:log10)],
layout)
In [ ]:
ϵ_B[22:28]In [ ]:
ϵ_B[end]/eps(Float64)In [ ]:
setprecision(80) # precision für BigFloat
s = sqrt(BigFloat(2))
@printf " a = √(4-s^2) als BigFloat und als Float64\n\n"
for i = 3:44
s = iterationA(s)
x = sqrt(4-s^2)
if i > 20
@printf "%i %30.26f %20.16f \n" i x Float64(x)
end
end

