I’m new to Julia and solving ODE’s in Julia and expect I’m overlooking something quite simple. I would expect the two functions below, test_1! and test_2! which both define the same simple system of ODEs, to perform identically. However test_1! erroneously produces a solution constant in time which is clearly incorrect while test_2! works fine.
Thank you in advance for any insights!
using DifferentialEquations
using Random
function test_1!(du, u, p, t)
W = p[1]
du = copy(W)
end
function test_2!(du, u, p, t)
W = p[1]
for i in 1:length(W)
du[i] = W[i]
end
end
Random.seed!(1234); # for reproducibility
nOsc = 5;
theta0 = 2 * pi * rand(Float64, nOsc);
W = 0.5 * randn(Float64, nOsc);
prob = ODEProblem(test_1!, theta0, (0, 20), [W]); # doesn't work. sol.u is constant
#prob = ODEProblem(test_2!, theta0, (0, 20), [W]); # works as expected.
sol = solve(prob)
Thank you very much! Can you give me guidance or a link reference to when I need to use .= instead of =? For example suppose I modify du after initialization. Which assignment would I use? Is this correct?