# How to get sol.du in DifferentialEquations.jl

**URL:** <https://discourse.julialang.org/t/how-to-get-sol-du-in-differentialequations-jl/21093>\
**Category:** General Usage\
**Tags:** diffeq\
**Created:** [February 22, 2019, 7:42pm UTC](https://discourse.julialang.org/t/how-to-get-sol-du-in-differentialequations-jl/21093 "2019-02-22T19:42:48Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mleprovost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mleprovost/32/7166_2.png) [@mleprovost](https://discourse.julialang.org/u/mleprovost)\
**Post date:** [February 22, 2019, 7:42pm UTC](https://discourse.julialang.org/t/how-to-get-sol-du-in-differentialequations-jl/21093/1 "2019-02-22T19:42:48Z")

</div>

Hello,

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( [http://docs.juliadiffeq.org/latest/basics/solution.html#Interpolations-1](http://docs.juliadiffeq.org/latest/basics/solution.html#Interpolations-1)) 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,

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 22, 2019, 8:10pm UTC](https://discourse.julialang.org/t/how-to-get-sol-du-in-differentialequations-jl/21093/2 "2019-02-22T20:10:53Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![mleprovost](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mleprovost/32/7166_2.png) [@mleprovost](https://discourse.julialang.org/u/mleprovost)\
**Post date:** [February 23, 2019, 12:12am UTC](https://discourse.julialang.org/t/how-to-get-sol-du-in-differentialequations-jl/21093/3 "2019-02-23T00:12:44Z")

</div>

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.

```julia
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]
end

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

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

sol.k[:][:]
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,

---

<div class="post-metadata">

**Author:** ![ChrisRackauckas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/chrisrackauckas/32/77_2.png) [@ChrisRackauckas](https://discourse.julialang.org/u/ChrisRackauckas)\
**Post date:** [February 23, 2019, 1:48am UTC](https://discourse.julialang.org/t/how-to-get-sol-du-in-differentialequations-jl/21093/4 "2019-02-23T01:48:24Z")

</div>

`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.
