Julia 朱莉娅';微分方程步长控制

Julia 朱莉娅';微分方程步长控制,julia,differential-equations,Julia,Differential Equations,我想用Julia中的微分方程解方程。对于某些初始值,我得到了错误: WARNING: dt <= dtmin. Aborting. If you would like to force continuation with dt=dtmin, set force_dtmin=true 我不知道该怎么做。以下是代码: using DifferentialEquations using Plots m = 1 l = 0.3 g = pi*pi function dbpen(du,u,pr

我想用Julia中的
微分方程
解方程。对于某些初始值,我得到了错误:

WARNING: dt <= dtmin. Aborting. If you would like to force continuation with
 dt=dtmin, set force_dtmin=true
我不知道该怎么做。以下是代码:

using DifferentialEquations
using Plots

m = 1
l = 0.3
g = pi*pi
function dbpen(du,u,pram,t)
  th1 = u[1]
  th2 = u[2]
  thdot1 = du[1]
  thdot2 = du[2]
  p1 =  u[3]
  p2 =  u[4]  
 du[1] = (6/(m*l^2))*(2*p1-3*p2*cos(th1-th2))/(16-9*(cos(th1-th2))^2)
 du[2] = (6/(m*l^2))*(8*p2-3*p1*cos(th1-th2))/(16-9*(cos(th1-th2))^2)
 du[3] = (-0.5*m*l^2)*(thdot1*thdot2*sin(th1-th2)+(3*g/l)*sin(th1))
 du[4] = (-0.5*m*l^2)*(-thdot1*thdot2*sin(th1-th2)+(g/l)*sin(th2))
end

u0 = [0.051;0.0;0.0;0.0]
tspan = (0.0,100.0)
prob = ODEProblem(dbpen,u0,tspan)
sol = solve(prob)

plot(sol,vars=(0,1))

似乎你的颂歌很僵硬,默认情况下需要非常小的dt。 您可以切换到stiff ODE解算器或给出如下提示:

sol = solve(prob,alg_hints=[:stiff])
function dbpen(du,u,pram,t)
  th1 = u[1]
  th2 = u[2]
  p1 =  u[3]
  p2 =  u[4]  
  du[1] = (6/(m*l^2))*(2*p1-3*p2*cos(th1-th2))/(16-9*(cos(th1-th2))^2)
  du[2] = (6/(m*l^2))*(8*p2-3*p1*cos(th1-th2))/(16-9*(cos(th1-th2))^2)
  thdot1 = du[1]
  thdot2 = du[2]
  du[3] = (-0.5*m*l^2)*(thdot1*thdot2*sin(th1-th2)+(3*g/l)*sin(th1))
  du[4] = (-0.5*m*l^2)*(-thdot1*thdot2*sin(th1-th2)+(g/l)*sin(th2))
end

参考:包文档中的ODE示例最近我更改了此警告,以明确告诉用户这很可能是模型的问题。如果您看到这一点,那么通常会有两个可能的问题:

  • ODE是刚性的,您仅对非刚性方程使用积分器
  • 您的型号代码不正确
  • 虽然(1)经常出现,但现在自动算法会自动检测,所以问题几乎总是(2)

    所以你能做的就是打印出你计算的导数,看看它是否符合你的预期。如果你这么做了,你会注意到

    thdot1 = du[1]
    thdot2 = du[2]
    
    为您提供可以无限小/无限大的虚拟值。原因是你应该覆盖它们!看起来你真正想做的是计算前两个导数项,然后在第二组导数项中使用它们。为此,您必须确保首先更新值!一个可能的代码如下所示:

    sol = solve(prob,alg_hints=[:stiff])
    
    function dbpen(du,u,pram,t)
      th1 = u[1]
      th2 = u[2]
      p1 =  u[3]
      p2 =  u[4]  
      du[1] = (6/(m*l^2))*(2*p1-3*p2*cos(th1-th2))/(16-9*(cos(th1-th2))^2)
      du[2] = (6/(m*l^2))*(8*p2-3*p1*cos(th1-th2))/(16-9*(cos(th1-th2))^2)
      thdot1 = du[1]
      thdot2 = du[2]
      du[3] = (-0.5*m*l^2)*(thdot1*thdot2*sin(th1-th2)+(3*g/l)*sin(th1))
      du[4] = (-0.5*m*l^2)*(-thdot1*thdot2*sin(th1-th2)+(g/l)*sin(th2))
    end
    
    使: