Search code examples
juliaodedifferentialequations.jl

solve system of ODEs with read in external forcing


In Julia, I want to solve a system of ODEs with external forcings g1(t), g2(t) like

dx1(t) / dt = f1(x1, t) + g1(t)
dx2(t) / dt = f2(x1, x2, t) + g2(t)

with the forcings read in from a file.

I am using this study to learn Julia and the package DifferentialEquations, but I am having difficulties finding the correct approach.

I could imagine that using a callback could work, but that seems pretty cumbersome.

Do you have an idea of how to implement such an external forcing?


Solution

  • You can use functions inside of the integration function. So you can use something like Interpolations.jl to build an interpolating polynomial from the data in your file, and then do something like:

    g1 = interpolate(data1, options...)
    g2 = interpolate(data2, options...)
    p = (g1,g2) # Localize these as parameters to the model
    
    function f(du,u,p,t)
    g1,g2 = p
      du[1] = ... + g1[t] # Interpolations.jl interpolates via []
      du[2] = ... + g2[t]
    end
    # Define u0 and tspan
    ODEProblem(f,u0,tspan,p)