solve system of ODEs with read in external forcing

落爺英雄遲暮 提交于 2020-03-24 04:54:10

问题


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?


回答1:


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)


来源:https://stackoverflow.com/questions/49428939/solve-system-of-odes-with-read-in-external-forcing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!