solving DiffEq on Complex Arrays

Dear Julia,
Following some examples, I am trying to solve a system ODE diff. eqs. on complex numbers, for which I have to use Complex array types. I have tried this from an example but it fails for me with Julia 1.0.3. Normal real/float ODE works fine, but when I try to use Complex array types I get such a problem (pls see below).

Could you please advise what I am doing wrong here?

Thanks a lot!

Best regards,
Balint

using DifferentialEquations
E = 200.0
function Eq(t::Float64, u::Array{Complex{Float64},1}, du::Array{Complex{Float64},1})
    du[1] = -im * E * u[1]
    du[2] = -im * E * u[2]
end

u = [1.0 + 0.0im, 1.5 + 0.0im]
T = 1000.0
prob = ODEProblem(Eq, u, (0, T))
prob.u0[1] = u[1]; prob.u0[2] = u[2]
sol = solve(prob; save_everystep=true, dense=false)

Stacktrace:
 [1] (::ODEFunction{false,typeof(Eq),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing})(::Array{Complex{Float64},1}, ::Nothing, ::Vararg{Any,N} where N) at /Users/balintradics/.julia/packages/DiffEqBase/s4c9b/src/diffeqfunction.jl:188
 [2] ode_determine_initdt(::Array{Complex{Float64},1}, ::Float64, ::Float64, ::Float64, ::Float64, ::Float64, ::typeof(DiffEqBase.ODE_DEFAULT_NORM), ::ODEProblem{Array{Complex{Float64},1},... (very long stack trace)

Welcome to the community! I see you found a very old code snippet. For the master resource you should consult docs.juliadiffeq.org. For example, you’d solve the equation with:

using DifferentialEquations
function Eq(du,u,p,t)
    E = 200.0
    du[1] = -im * E * u[1]
    du[2] = -im * E * u[2]
end

u = [1.0 + 0.0im, 1.5 + 0.0im]
T = 1000.0
prob = ODEProblem(Eq, u, (0, T))
sol = solve(prob; dense=false)
1 Like

I am sorry for my very late answer. Thanks a lot, it worked perfectly! :slight_smile:

1 Like

Hi! I’m definetely a newbie of Julia
I’m trying to solve systems of ODE with complex numbers. I tried both the snippets reported above but none worked (yes, I installed the package and it works with real numbers… :wink: ).
Using the latter I get the following error:

MethodError: no method matching isless(::Float64, ::Complex{Float64})

I’m using Julia 1.2.0 on win8.1
Could anyone suggest me where I’m wrong?

Thanks!
Paolo

Choosing a solver algorithm by hand works:

using DifferentialEquations
function Eq(du,u,p,t)
    E = 200.0
    du[1] = -im * E * u[1]
    du[2] = -im * E * u[2]
end

u = [1.0 + 0.0im, 1.5 + 0.0im]
T = 1000.0
prob = ODEProblem(Eq, u, (0, T))
sol = solve(prob, Tsit5(); dense=false)

Ou! I didn’t though about this…

Really thanks!
P.

Can you open an issue with your example? Sounds like the default algorithm chooser is trying to see if your state value is less than ? for choosing an appropriate method

what’d I do to open an issue?
I simply copy&paste the snippet above.
Do you want me to report the entire error message?

The snippet above works. What we need is your snippet that doesn’t work. We need the example that doesnt work so we can run it to fix it.

here it is…

using DifferentialEquations
function Eq(du,u,p,t)
    E = 200.0
    du[1] = -im * E * u[1]
    du[2] = -im * E * u[2]
end

u = [1.0 + 0.0im, 1.5 + 0.0im]
T = 1000.0
prob = ODEProblem(Eq, u, (0, T))
sol = solve(prob; dense=false)

Thanks! This PR fixes it: https://github.com/JuliaDiffEq/OrdinaryDiffEq.jl/pull/905