using Distributions
using PyPlot
using Plots
using Random
using Statistics
function f(x;debug::Bool=false)
result =1+ sqrt.(x + 1.0/pi^2)*sin.(1/(x + 1.0/pi^2))
if debug
print(x," → ",result,"\t")
end
result
end
f(0.12)
f(0.12,debug=true)
f.([0.12,0.12])
f.([0.12,0.12],debug=true)
f(f(12))
(f ∘ f)(12)
12 |> f |> f
?range
Write \phi
and then press Tab
. Or even just start \ph
and press tab once to complete, the second time to make the glyph.
ϕ = range(0.01,1,length=10000); y = f.(ϕ);
PyPlot.plot(ϕ, y, color="red", linewidth=2.0, linestyle="--")
title("A sinusoidally modulated square root")
xlabel("X axis")
ylabel("X axis")
xscale("log")
xticks([0.01,0.02,0.05,0.5,1],20*[0.01,0.02,0.05,0.5,1])
Plots.plot(ϕ, y, color="red", linewidth=2.0, linestyle=:dash)
p=Plots.plot!(ϕ,2*y) # Mutating plot by ! I can add a plot to the old one
Plots.plot!(p,ϕ,3*y) # Here I keep mutating the old plot by adding to the variable `p`.
p #Beware this additions is inplace
?rand()
?values
random_values=rand( Uniform(0, 1),10000);
typeof(random_values)
sampled_values=[ f(num) for num in random_values];
eltype(random_values)
sum(sampled_values)/length(sampled_values)
function mcIntegral(nP)
_values=rand(Uniform(0, 1),nP);
_sampled_values=[ f(num) for num in _values];
sum(_sampled_values)/length(_sampled_values)
end
function averageMCintegral(nP)
trials=100
range=[ k for k in 1:1:trials ]
#print(range)
results=[ mcIntegral(nP) for k in range ]
[Statistics.mean( results), Statistics.std(results)]
end
averageMCintegral(100)
points = [10, 100, 1000, 3000, 10000, 30000, 300000, 3000000, 8000000 ]
integrals = [ [k; averageMCintegral(k)] for k in points ]
σs=[ i[3] for i in integrals ]
I=[ i[2] for i in integrals ]
σs[1:end-k]
k=1
Plots.plot(points[1:end-k], abs.(I .- I[end])[1:end-k] , xaxis=:log , yaxis=:log)#, ribbon=(σs[1:end-k]))
k=1
Plots.plot(points[1:end-k], abs.(I .- I[end])[1:end-k], ribbon=(σs[1:end-k]), xaxis=:log )