Solving a system of differential equations with DifferentailEqations.jl

Hello there

when I try to solve a system of ordinary differential equations with the following code:

using DifferentialEquations

function dgl(du,u,p,t)
	du[1] = u[3]
	du[2] = u[4]
	du[3] = -sqrt(u[4]^2+u[5]^2)*u[3]
	du[4] = -sqrt(u[4]^2+u[5]^2)*u[4] - 1
end

u0 = [0.0; 0.0; 100.0; 100.0]
prob = ODEProblem(dgl, (0.0, 10.0), u0)
#sol = solve(prob)

I get this error I can’t grasp:

MethodError: Cannot `convert` an object of type Array{Float64,1} to an object of type Tuple{Float64,Float64}
This may have arisen from a call to the constructor Tuple{Float64,Float64}(...),
since type constructors fall back to convert methods.

Stacktrace:
 [1] (::DiffEqBase.##call#174#178)(::Void, ::Void, ::UniformScaling{Int64}, ::Type{DiffEqBase.ODEProblem{true,tType,isinplace,P,F,J,C,MM,PT} where PT where MM where C where J where F where P where isinplace where tType}, ::Function, ::Tuple{Float64,Float64}, ::Array{Float64,1}, ::Void, ::DiffEqBase.StandardODEProblem) at /home/christian/.julia/v0.6/DiffEqBase/src/problems/ode_problems.jl:23
 [2] DiffEqBase.ODEProblem{true,tType,isinplace,P,F,J,C,MM,PT} where PT where MM where C where J where F where P where isinplace where tType(::Function, ::Tuple{Float64,Float64}, ::Array{Float64,1}, ::Void) at /home/christian/.julia/v0.6/DiffEqBase/src/problems/ode_problems.jl:18
 [3] #ODEProblem#182(::Array{Any,1}, ::Type{T} where T, ::Function, ::Tuple{Float64,Float64}, ::Array{Float64,1}, ::Void) at /home/christian/.julia/v0.6/DiffEqBase/src/problems/ode_problems.jl:33
 [4] DiffEqBase.ODEProblem(::Function, ::Tuple{Float64,Float64}, ::Array{Float64,1}) at /home/christian/.julia/v0.6/DiffEqBase/src/problems/ode_problems.jl:32
 [5] execute_request(::ZMQ.Socket, ::IJulia.Msg) at /home/christian/.julia/v0.6/IJulia/src/execute_request.jl:180
 [6] (::Compat.#inner#14{Array{Any,1},IJulia.#execute_request,Tuple{ZMQ.Socket,IJulia.Msg}})() at /home/christian/.julia/v0.6/Compat/src/Compat.jl:332
 [7] eventloop(::ZMQ.Socket) at /home/christian/.julia/v0.6/IJulia/src/eventloop.jl:8
 [8] (::IJulia.##15#18)() at ./task.jl:335

​I feel like it must be something very stupid because my code is very similiar to the example from the docs

Thanks for any help

You just switched the ODEProblem constructor arguments. Should be prob = ODEProblem(dgl, u0, (0.0, 10.0)). By the way, your u0 has four elements, but you’re trying to access the fifth in dgl.

thank you so much – I felt really stupid
also thanks for the hint with the fifth element but it arised from reducing the example

1 Like