Search code examples
juliadifferential-equations

Julia integrating differential equations: MethodError: no method matching


I am trying to vector differential equation in Julia. But I get stuck on the following error warning:

MethodError: no method matching hDerivative(::Array{Float64,1}, ::Nothing, >::Float64) Closest candidates are: hDerivative(::Any, ::Any) at In[8]:3 hDerivative(::Any) at In[13]:3

I am basically unsure about the syntax of the function "hDerivative". I tried returning the differential, but also to make to take "timederiv" as an argument of the function hDerivative, analogously to what I have seen in tuturials about differential equations in Julia, altough this look a little strange (I am used to python).

This is an example of the code I used:

using DifferentialEquations

N=10
J=randn(Float64,N,N)
g=1

function hDerivative(h,timederiv)
    timederiv=zeros(Float64,N)
    for i=1:length(h)
        for j=1:length(h)
            timederiv[i]=timederiv[i]+J[i,j]*tanh(h[j])            
        end
    end  
end

hinit=zeros(Float64,N)
tspan=(0.0,1.0)
prob = ODEProblem(hDerivative,hinit,tspan)
solve(prob)

Can anyone help me out?


Solution

  • @LutzL's comment is exactly correct that the problem with this code is that it doesn't define the derivative function as mentioned in the documentation. Instead, the following code which utilizes the (du,u,p,t) form works:

    using DifferentialEquations
    
    N=10
    J=randn(Float64,N,N)
    g=1
    
    function hDerivative(timederiv,h,p,t)
        for i=1:length(h)
            timederiv[i] = 0
            for j=1:length(h)
                timederiv[i]=timederiv[i]+J[i,j]*tanh(h[j])            
            end
        end  
    end
    
    hinit=zeros(Float64,N)
    tspan=(0.0,1.0)
    prob = ODEProblem(hDerivative,hinit,tspan)
    solve(prob)