Julia 的 DifferentialEquations 问题将解决方案转换为数组
Julia's DifferentialEquations issue in converting solution to array
我使用 DifferentialEquations
求解了一个微分方程组(van der Pol 方程)。
我想导出解决方案。为此,我使用了 convert(Array,sol)
,但是,转换后的解决方案与我通过 sol
.
获得的解决方案不同
更多解释见下方代码:
using DifferentialEquations
using Plots
function fun(du,u,p,t)
du[1] = u[2]
du[2] = 1000*(1-u[1]^2)*u[2]-u[1]
end
u0 = [2.0,0.0]
tspan = (0.0,3000.0)
prob = ODEProblem(fun,u0,tspan)
sol = solve(prob)
a = convert(Array,sol)#Here I tried to convert the solution to an array
plot(a[1,:])
plot(sol,vars = 1)
a = convert(Array,sol)
plot(a[1,:])
returns:
plot(sol,vars = 1)
returns:
转换后的解决方案与sol
中包含的内容相同。问题在于 x 轴(这里是时间)上变量的步长不均匀。所以仅使用 plot(a[1,:])
绘图是不够的。我们必须提供解决方案在什么时候具有它所具有的价值。使用 plot(sol.t,a[1,:])
绘制正确答案。
我使用 DifferentialEquations
求解了一个微分方程组(van der Pol 方程)。
我想导出解决方案。为此,我使用了 convert(Array,sol)
,但是,转换后的解决方案与我通过 sol
.
更多解释见下方代码:
using DifferentialEquations
using Plots
function fun(du,u,p,t)
du[1] = u[2]
du[2] = 1000*(1-u[1]^2)*u[2]-u[1]
end
u0 = [2.0,0.0]
tspan = (0.0,3000.0)
prob = ODEProblem(fun,u0,tspan)
sol = solve(prob)
a = convert(Array,sol)#Here I tried to convert the solution to an array
plot(a[1,:])
plot(sol,vars = 1)
a = convert(Array,sol)
plot(a[1,:])
returns:
plot(sol,vars = 1)
returns:
转换后的解决方案与sol
中包含的内容相同。问题在于 x 轴(这里是时间)上变量的步长不均匀。所以仅使用 plot(a[1,:])
绘图是不够的。我们必须提供解决方案在什么时候具有它所具有的价值。使用 plot(sol.t,a[1,:])
绘制正确答案。