我想在julia中求解含时变项的微分方程,
在matlab中是:
请问怎么用julia的DifferentialEquations包求解上式
希望各位多多指点,非常感谢!
翻翻手册?Ordinary Differential Equations · DifferentialEquations.jl
Matlab 的等价于
myode.m
function dydt = myode(t,y)
f = t^2 - t -3;
g = 3*sin(t-0.25);
dydt = -f.*y + g;
ODEtest.m
tspan = [1 5];
ic = 1;
opts = odeset('RelTol',1e-2,'AbsTol',1e-4);
[t,y] = ode45(@(t,y) myode(t,y), tspan, ic, opts);
plot(t,y)
这样就免去了插值
Julia 代码,网不好还没装上这个包,就没有测试。
using DifferentialEquations
f(u,p,t) = -(t.^2 - t -3).*u + (3 .* sin.(t-0.25))
u0 = 1
tspan = (1.0, 5.0)
prob = ODEProblem(f, u0, tspan)
sol = solve(prob, Tsit5(), reltol=1e-8, abstol=1e-8)
using Plots
plot(sol, linewidth=5)
Julia 插值可以看看这个包 JuliaMath/Interpolations.jl: Fast, continuous interpolation of discrete datasets in Julia
好的,多谢大神指导,谢谢!
可以只安装OrdinaryDiffEq.jl
.
OrdinaryDiffEq.jl
的解最低是三阶Hermite插值. 很多算法都有优化的高阶插值, 比如说Vern9
有九阶插值, Tsit5
有四阶插值.
应该是 u0=1.0
.
没有需要broadcast的运算, 直接 f(u,p,t) = -(t^2 - t -3)*u + (3 * sin(t-0.25))
就好