Search code examples
juliaodedifferentialequations.jl

Switching ODE functions in Julia


Fom document of DifferentialEquations package, switching between sets of ODE functions can be done using a parameter as

function f(du,u,p,t)
  if p==0
    du[1] = 2u[1]
  else
    du[1] = - u[1]
  end
  du[2] = -u[2]
end

Is this possible to use dependent variable (state variable) instead of parameter p as the switch like

function f(du,u,p,t)
  if (u[2]<=0 && du[2]>0)
    du[1] = 2u[1]
  else
    du[1] = - u[1]
  end
  du[2] = -u[2]
end

Thank you in advance for your help.


Solution

  • Is this possible to use dependent variable (state variable) instead of parameter p as the switch like

    Yes. It introduces a discontinuity, so it's not the best thing to do, but adaptivity will handle it. Sometimes performance can be improved by making a ContinuousCallback which rootfinds to that value as the condition, but then does nothing for the affect!. But yes, the code with the branch in it is fine.