2
votes

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?

1
Per the documentation the order of arguments in the ODE function is function f(du,u,p,t), the return vector followed by the state vector and possibly paramters and the time. You have a different order and number of arguments. If you think that should work, please reference the example you are working off of.Lutz Lehmann
So perhaps prob = ODEProblem((du,u,p,t)->hDerivative(u,du), hinit, tspan) could work?Lutz Lehmann

1 Answers

2
votes

@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)