首页 > 解决方案 > Julia积分微分方程:MethodError:没有方法匹配

问题描述

我正在尝试在 Julia 中向量化微分方程。但我遇到以下错误警告:

MethodError: no method matching hDerivative(::Array{Float64,1}, ::Nothing, >::Float64) 最接近的候选是:hDerivative(::Any, ::Any) at In[8]:3 hDerivative(::任何)在 In[13]:3

我基本上不确定函数“hDerivative”的语法。我尝试返回微分,但也尝试将“timederiv”作为函数 hDerivative 的参数,类似于我在 Julia 中关于微分方程的教程中看到的,尽管这看起来有点奇怪(我习惯了 python) .

这是我使用的代码示例:

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)

谁能帮我吗?

标签: juliadifferential-equations

解决方案


@LutzL 的评论是完全正确的,该代码的问题在于它没有定义文档中提到的导数函数。相反,使用该(du,u,p,t)表单的以下代码有效:

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)

推荐阅读