Search code examples
juliadifferential-equations

Julia's DifferentialEquations issue in converting solution to array


I solved a system of differential equations (van der Pol equations) using DifferentialEquations. I would like to export the solution. To do this I used convert(Array,sol), however, the converted solution is not the same as the solution I get by sol.

See the code below for more explanation:

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: enter image description here

plot(sol,vars = 1) returns: enter image description here


Solution

  • The converted solution is the same as what is contained in sol. The problem lies in the fact that the step size for the variable in the x axis (here it is time) is not uniform. So only plotting using plot(a[1,:]) is not enough. We must provide at what time the solution has the value it has. Using plot(sol.t,a[1,:]) plot the right answer.