In [13]:
include("CLASS_REPO/Astro585/lab4/HW4_leapfrog.jl")
include("CLASS_REPO/Astro585/lab4/HW4_leapfrog_student.jl")
Out[13]:
In [14]:
Profile.clear()
for i in 0:50
@profile orbit = integrate_leapfrog!([1.0,0.0,00.0,1.0],2pi/200,6pi);
#println("X:",orbit[1][end]," Y:",orbit[2][end]," Vx:",orbit[3][end]," Vy:",orbit[4][end])
end
Profile.print()
In [15]:
Profile.clear()
for i in 0:50
@profile orbit = integrate_leapfrog_student([1.0,0.0,0.0,1.0],2pi/200,6pi);
#println("X:",orbit[1][end]," Y:",orbit[2][end]," Vx:",orbit[3][end]," Vy:",orbit[4][end])
end
Profile.print()
In [29]:
function f(x::Vector{Float64})
return exp(-0.5.*x.^2)/sqrt(2pi)
end
Out[29]:
In [30]:
x = linspace(-3,3,1000)
y = f(x)
using PyPlot
plot(x,y)
Out[30]:
In [83]:
function loop_interate(f, a::Float64, b::Float64, integration_number::Int=1000)
@assert a < b
interval_bounds = linspace(a,b,integration_number+1)
value_array = zeros(Float64,integration_number)
for i = 2:integration_number
#trapizoid rule, cause that is enough for a homework
value_array[i] = (interval_bounds[i] - interval_bounds[i-1]) *.5*(f(interval_bounds[i-1] )+f(interval_bounds[i] ) )
end
return sum(value_array)
end
Out[83]:
In [118]:
#Does it obey the 68–95–99.7 rule?
println(loop_interate(f,-1.0,1.0))
println(loop_interate(f,-2.0,2.0))
println(loop_interate(f,-3.0,3.0))
println(loop_interate(f,-100.0,100.0))
In [108]:
function vector_interate(f, a::Float64, b::Float64, integration_number::Int=1000)
@assert a < b
bounds = linspace(a,b,integration_number+1)
lower_bounds = bounds[1:integration_number]
upper_bounds = bounds[2:end]
value_array = (upper_bounds .- lower_bounds) *.5.*(f(lower_bounds ).+f(upper_bounds))
return sum(value_array)
end
Out[108]:
In [120]:
#Does it obey the 68–95–99.7 rule?
println(vector_interate(f,-1.0,1.0))
println(vector_interate(f,-2.0,2.0))
println(vector_interate(f,-3.0,3.0))
println(vector_interate(f,-100.0,100.0))
In [143]:
function trapazoid_rule(a::Float64,b::Float64)
#If f is not alrady complied you are out of luck here.
return (b - a) * .5 * (f(a) + f(b))
end
function map_plus_reduce_interate(f, a::Float64, b::Float64, integration_number::Int=1000)
#Don't actually call f anymore
@assert a < b
bounds = linspace(a,b,integration_number+1)
lower_bounds = bounds[1:integration_number]
upper_bounds = bounds[2:end]
value_array = map(trapazoid_rule,lower_bounds,upper_bounds)
return reduce(+,value_array)
end
Out[143]:
In [144]:
#Does it obey the 68–95–99.7 rule?
println(map_plus_reduce_interate(f,-1.0,1.0))
println(map_plus_reduce_interate(f,-2.0,2.0))
println(map_plus_reduce_interate(f,-3.0,3.0))
println(map_plus_reduce_interate(f,-100.0,100.0))
In [159]:
#mapreduce can't handle multiple itr, so just tweaking map_plus_reduce wont do it. errgh, tired and grumpy,
#don't want to code no more today
function trapazoid_rule(bounds)
#If f is not alrady complied you are out of luck here.
return (b - a) * .5 * (f(a) + f(b))
end
function mapreduce_interate(f, a::Float64, b::Float64, integration_number::Int=1000)
#Don't actually call f anymore
@assert a < b
bounds = linspace(a,b,integration_number+1)
#lower_bounds = bounds[1:integration_number]
#upper_bounds = bounds[2:end]
return mapreduce(trapazoid_rule,+,bounds)
end
Out[159]:
In [149]:
#Does it obey the 68–95–99.7 rule?
println(mapreduce_interate(f,-1.0,1.0))
println(mapreduce_interate(f,-2.0,2.0))
println(mapreduce_interate(f,-3.0,3.0))
println(mapreduce_interate(f,-100.0,100.0))
In [151]:
#blah! looks interesting...loops faster than vectors? Strange.
In [160]:
println("Loop: ",@elapsed loop_interate(f,-1.0,1.0,1000000))
println("Vector: ",@elapsed vector_interate(f,-1.0,1.0,1000000))
println("Map+Reduce: ",@elapsed map_plus_reduce_interate(f,-1.0,1.0,1000000))
In [161]:
Profile.clear()
@profile loop_interate(f,-1.0,1.0,1000000)
Profile.print()
In [162]:
Profile.clear()
@profile vector_interate(f,-1.0,1.0,1000000)
Profile.print()
In [163]:
Profile.clear()
@profile map_plus_reduce_interate(f,-1.0,1.0,1000000)
Profile.print()
In [ ]: