gpt4 book ai didi

Julia 的微分方程步长控制

转载 作者:行者123 更新时间:2023-12-05 00:48:21 24 4
gpt4 key购买 nike

我想解决double pendulum在 Julia 中使用 DifferentialEquations 的方程。对于一些初始值,我得到了错误:

WARNING: dt <= dtmin. Aborting. If you would like to force continuation with
dt=dtmin, set force_dtmin=true

如果我使用 force_dtmin=true,我会得到:

WARNING: Instability detected. Aborting

我不知道该怎么做。这是代码:

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))

最佳答案

我最近更改了此警告,改为明确告诉用户这很可能是模型存在问题。如果你看到这个,那么通常有两个可能的问题:

  1. ODE 是刚性的,您只对非刚性方程使用积分器
  2. 您的型号代码不正确。

虽然 (1) 过去经常出现,但现在自动算法会自动检测它,所以问题几乎总是 (2)。

因此,您可以打印出计算得出的导数,看看它是否符合您的预期。如果你这样做了,那么你会注意到

thdot1 = du[1]
thdot2 = du[2]

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

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

制作:

enter image description here

关于Julia 的微分方程步长控制,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51983171/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com