I’m exploring DifferentialEquations,jl (and teaching it to high school students tomorrow ). First demo is simple projectile motion, a 4d ODE wih state vector u = [x, y, vx, vy]. I want to extract x and y from the solution and plot them together. But the code doesn’t behave as the documentation suggests (http://docs.juliadiffeq.org/latest/basics/solution.html).
Minimal example
using DifferentialEquations
# frictionless 2d projectile motion
# u = [x, y, vx, vy], f(u) = du/dt = [vx, vy, 0, -9.8]
function f(t, u)
return [u[3], u[4], 0.0, -9.8]
end
x0 = 0.0 # initial horizontal position
y0 = 1.0 # initial height
v0 = 10.0 # initial speed
θ = 0.5 # inital angle
u0 = [x0, y0, v0*cos(θ), v0*sin(θ)]
tspan = (0.0, 5.0)
prob = ODEProblem(f, u0, tspan)
soln = solve(prob)
Running this produces the sensible answer
julia> soln.u
7-element Array{Array{Float64,1},1}:
[0.0, 1.0, 8.77583, 4.79426]
[0.00173129, 1.00095, 8.77583, 4.79232]
[0.0190442, 1.01038, 8.77583, 4.77299]
[0.192173, 1.10263, 8.77583, 4.57966]
[1.92346, 1.8154, 8.77583, 2.64632]
[19.2363, -12.0343, 8.77583, -16.687]
[43.8791, -97.5287, 8.77583, -44.2057]
The DiffEQ documentation says soln.u[1,:]
should extract a timeseries of the first component u1 = x (the first column of the above), but instead it extracts the state vector [u1, u2, u3, u4] at the first time step.
julia> soln.u[1,:]
1-element Array{Array{Float64,1},1}:
[0.0, 1.0, 8.77583, 4.79426]
No permutation of colons, transposes, rows, cols produces what I need, e.g.
julia> soln.u[:,1]
7-element Array{Array{Float64,1},1}:
[0.0, 1.0, 8.77583, 4.79426]
[0.00173129, 1.00095, 8.77583, 4.79232]
[0.0190442, 1.01038, 8.77583, 4.77299]
[0.192173, 1.10263, 8.77583, 4.57966]
[1.92346, 1.8154, 8.77583, 2.64632]
[19.2363, -12.0343, 8.77583, -16.687]
[43.8791, -97.5287, 8.77583, -44.2057]
How can I convert Array{Array{Float64,1},1}
into ````Array{Float64,2}``` and then perform normal colon indexing of rows and cols?
running julia-0.6.0 on Linux, downloaded binary tarball from julialang.org