微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

是否可以使用回调来访问 Julia 的微分方程集成问题中的单个轨迹?

如何解决是否可以使用回调来访问 Julia 的微分方程集成问题中的单个轨迹?

我是 Julia 的新手,并尝试使用 Julia 包 DifferentialEquations 来同时求解同一组耦合 ODE 的多个条件。我的系统是一个实验模型,在其中一种条件下,我在过程中途增加一个因变量的数量

我希望能够调整这条单一轨迹的条件,但到目前为止我只能一次调整所有轨迹。是否可以使用回调访问单个?如果没有,有没有更好的方法来做到这一点?

这是一个使用洛伦兹方程的简化示例,用于我想要做的事情:

#Differential Equations setup
function lorentz!(du,u,p,t)
    a,r,b=p
      
    du[1]= a*(u[2]-u[1])             
    du[2]=u[1]*(r-u[3])-u[2]
    du[3]=u[1]*u[2]-b*u[3];      
end

#function to cycle through inital conditions
function prob_func(prob,i,repeat)
    remake(prob; u0 = u0_arr[i]);
end
    
#inputs
t_span=[(0.0,100.0),(0.0,100.0)];
u01=[0.0;1.0;0.0];
u02=[0.0;1.0;0.0];
u0_arr = [u01,u02];
p=[10.,28.,8/3];

#initialising the Ensemble Problem
prob = ODEProblem(lorentz!,u0_arr[1],t_span[1],p);
CombinedProblem = EnsembleProblem(prob,prob_func = prob_func,#-> (prob),#repeat is a count for how many times the trajectories had been repeated
                                    safetycopy = true # determines whether a safetly deepcopy is called on the prob before the prob_func (sounds best to leave as true for user-given prob_func)
                                    );
    
#introducing callback 
function condition(u,t,repeat)
        return  50 .-t      
    end
function affect!(repeat)
        repeat.u[1]=repeat.u[1] +50
    end
callback = DifferentialEquations.ContinuousCallback(condition,affect!)  

#solving
sim=solve(CombinedProblem,Rosenbrock23(),EnsembleSerial(),trajectories=2,callback=callback);
 

# Plotting for ease of understanding example
plot(sim[1].t,sim[1][1,:])
plot!(sim[2].t,sim[2][1,:])

我想制作这样的东西:

https://fullcalendar.io/docs/icalendar

但这段代码产生:

Example_desired_outcome

感谢您的帮助!

解决方法

您可以使回调依赖于参数,并使参数在问题之间有所不同。例如:

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

condition(t,integrator) = u[2] - 0.5
affect!(integrator) = integrator.prob.p = 1

有关更多信息,请查看有关此主题的常见问题解答:https://diffeq.sciml.ai/stable/basics/faq/#Switching-ODE-functions-in-the-middle-of-integration

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。