2

I have the following example:

using DifferentialEquations

function test1(du,u,p,t)
    a,b,c = p
    
    d=a^0.1*(t+1)
    e=u[1]/a
    f=u[2]/d
    
    du[1] = a*u[1]
    du[2] = d*u[2]
    du[3] = b*u[2] - c*u[3]
end

p = (2,0.75,0.8)
u0 = [1.0;1.0;1.0]
tspan = (0.0,3.0)
prob = ODEProblem(test1,u0,tspan,p)
sol = solve(prob,saveat=0.3)

The sol objects contain state outputs but, I need efficiently other variables ("d","e","f") as well.

The closest I can get is:

function test2(du,u,p,t)
    
    global i
    global Out_values
    global sampletimes
    
    a,b,c = p
    
    d=a^0.1*(t+1)
    e=u[1]/a
    f=u[2]/d
    
    if t in sampletimes
        
        Out_values[1,i] = d
        Out_values[2,i] = e
        Out_values[3,i] = f
        i=i+1
    
    end
    
    du[1] = a*u[1]
    du[2] = d*u[2]
    du[3] = b*u[2] - c*u[3]
end

sampletimes = tspan[1]:0.3:tspan[2]
Out_values = Array{Float64}(undef, 3, 2*11)
i=1

prob = ODEProblem(test2,u0,tspan,p)
sol = solve(prob,saveat=0.3,tstops=sampletimes)

However, this solution is not ideal because:

  1. it duplicates saveat and I get two sets of slightly different outputs (not sure why), and
  2. it can't expand if I decide not to use saveat and I want to output all solutions, i.e. sol = solve(prob).

Any help is appreciated.

eod
  • 449
  • 3
  • 17
  • 1
    I think you'll get better results from ModellingToolkit for these types of requirements. – Oscar Smith Aug 21 '22 at 18:10
  • @OscarSmith Thank you for your answer. I am new to Julia and DifferentialEquations.jl. Can you kindly point me to a tutorial or any kind of example? – eod Aug 22 '22 at 13:54

0 Answers0