DifferentialEquations: Confusion regarding time points and values in the solution array

I am a bit confused about the timepoints that the solution array represents.

Since the algorithms used to solve are adaptive, the size of solutions might be different. Now, if I say that all my growth rates of species have the units hour^(-1) and all other constants have appropriate units and specify the time span to be 0.0 to 100.0 and I wish to track the population size in each hour. Given the model equations, if the length of solution comes out be 1000, then:

1) What time points do these 1000 values represent?    
2) How to get the values of population size in each hour i.e., get the 101 values which would represent population size at each hour? Can this array be obtained by using `solve(problem,saveat = 1.0)`?

Thank you.

ys = [sol(t) for t in 0.0:100]

You get some interpolated values which should be with the specified tolerances.

See here: https://docs.sciml.ai/stable/basics/solution/#Interpolations-1

Yes, if you want outputs saved by 1, then saveat=1.0

1 Like

So from I understand, if I set up the time span to be 0.0 to 10.0 and my units are still hour^(-1) and the algorithm somhow calculates stepsize as 0.5 (assume, I dont know if its possible or not), I must get the solution vector to be of length 10.0/0.5 i.e., 20, right?

And,would each of the 20 values would represent values for time points 0 hr, 0.5 hr, 1.0 hr, 1.5 hr…,19.5 hr,20 hr?

If you don’t use saveat, the solver will typically pick how many points it needs to represent the solution well enough that the interpolation will be accurate in between. I’ve almost exclusively used Tsit5() and this seems to work really well. As a result, I don’t worry about what points the solver actually finds and use the interpolation interface to find values at times of interest. The one time I got away from that was when doing a large and long simulation in which I was running low on RAM, but that should be the exception.

The time points the solver will use will probably not be evenly spaced. They will depend on how fast things are changing in your simulation. I would encourage you to try using the interpolation - it is a good feature.

I am quiet new to solving ODEs, rather this is my first time.
Can you shed some light upon the interpolating function as I did not find any good material on it online?
Is it anything like the integrated function of the ODE/s?


Just add a line like Intu' = u

Here’s a very simple example:

using DifferentialEquations

function ode!(du,u,p,t)
    du[1] =1- u[1]


If you examine the sol object after running that code, you see this:

julia> sol
retcode: Success
Interpolation: specialized 4th order "free" interpolation
t: 15-element Array{Float64,1}:
u: 15-element Array{Array{Float64,1},1}:

It only stored 15 points over the 5 seconds of simulation. However, you can get the value at any time by executing something like sol(1.0) (note I’m using parentheses, not brackets) and it will interpolate and give an accurate answer.
Or you can do this:

julia> sol(0.0:0.5:5.0)
t: 0.0:0.5:5.0
u: 11-element Array{Array{Float64,1},1}:

to get interpolation of results on a regular time basis.

Docs are here: https://docs.sciml.ai/stable/basics/solution/#Interpolations-1

1 Like

Thanks a ton!
These makes sense to me now.

1 Like