How to get sol.du in DifferentialEquations.jl


I am solving an ODE with DifferentialEquations.jl. I would like to access to the derivative of the solution at every time step via sol.du.
In the documentation( they give this example
[t+3u-du for (t,u,du) in zip(sol.t,sol.u,sol.du)]

How should I do to enable the option to save sol.du when I solve the ODE?

Thanks for your help,

We need to make this happen for non-DAEs. Generally it’s stored as sol.k[i][1].

1 Like

I am not sure that I understand the structure of sol.k.

For an ODE with 3 state variable, sol.k[1:end][1] which should contain the first derivatives at all time is only of Array of Array of 3 elements, I guess it contains the derivatives at time t_0.

And sol.k[:][:] is an Array of Array of Array. I tried to play around with VectorOfArray() and convert(Array,..) but I can only reduce it to a Array of Array

Is there a simple way to get an Array of type Array{Float64,2} of all state derivative at different time steps.

function lorenz(du,u,p,t)
 du[1] = 10.0*(u[2]-u[1])
 du[2] = u[1]*(28.0-u[3]) - u[2]
 du[3] = u[1]*u[2] - (8/3)*u[3]

u0 = [1.0;0.0;0.0]
tspan = (0.0,100.0)
prob = ODEProblem(lorenz,u0,tspan)
sol = solve(prob)

1-element Array{Array{Float64,1},1}:
 [4.94066e-324, 1.97626e-323, 2.47033e-323]

1250-element Array{Array{Array{Float64,1},1},1}:
 [[4.94066e-324, 1.97626e-323, 2.47033e-323]]                                                                                                                                                                                    
 [[-10.0, 28.0, 0.0], [-9.99782, 27.9982, 0.00016083], [-9.99557, ...blablabla

Thank you for your help,

sol.k[1:end][1] won’t work, just the linear index. If you want more of them, you need to do a comprehension: [sol.k[i][1] for i in 1:length(sol)] should be it.

1 Like