对于这种ODE系统,Julia中的微分方程为什么给我一个“无匹配方法”错误?

问题描述 投票:0回答:1

我想在Julia中解决由七个耦合ODE组成的系统。

我的代码是:

function bsys!(dv,v,k,t)

    dp,thp,dr,thr,dx,thx,phi = v
    a = exp(t)

    rp = r_y(a)
    rr = r_r(a)
    rx = r_x(a)

    ix = T_a(a)/mx
    vy = v_a(a)
    tci = (ny_a(a)*vy*(4*pi*alpha*alpha/(3*mx*mx))*sqrt(3/2)*ix)/Hi


    H = sqrt((8*pi*G/3)*(rp + rr + rx))/Hi

    phidot = -(1 + k*k/(3*H*H*a*a))*phi + 4*pi*G*(rp*dp + rr*dr + rx*dx)/(3*H*H*Hi*Hi)

    w = w_a(a)
    wd = dw_a(a)
    cs2 = cs2_a(a)
    wx = wx_a(a)
    wdx = dwx_a(a)
    cs2x = cs2x_a(a)

    dv[1] = -(1+w)*thp/(a*H) - 3*(1+w)*phidot -3*(cs2 - w)*dp + gammatilde*phi/H
    dv[2] = -(1-3*w)*thp - k*k*phi/(a*H) - wd*thp/(1 + w) + cs2*k*k*dp/(a*H*(1+w)) + (w/(1+w))*gammatilde*thp/H 
    dv[3] = -(4/3)*thr/(a*H) - 4*phidot + (rp/rr)*gammatilde*(dp-dr-phi)/H 
    dv[4] = -k*k*phi/(a*H) + (1/4)*k*k*dr/(a*H) + (rp/rr)*gammatilde*((3/4)*thp - thr)/H 
    dv[5] = -(1+wx)*thx/(a*H) - 3*(1+wx)*phidot -3*(cs2x - wx)*dx 
    dv[6] = -(1-3*wx)*thx - k*k*phi/(a*H) - wdx*thx/(1 + wx) + cs2x*k*k*dx/(a*H*(1+wx)) + tci*(thp - thx)/H     
    dv[7] = phidot


end

tspan = (0.0,3.0)

k = 100*krhtilde

init = (2.0,-0.5*k*k,2.0,-0.5*k*k,2.0,-0.5*k*k,1.0)

prob = ODEProblem(bsys!,init,tspan,k)


sol = solve(prob)

我收到以下错误:

MethodError: no method matching zero(::NTuple{7,Float64})
Closest candidates are:
  zero(!Matched::Type{Missing}) at missing.jl:103
  zero(!Matched::Type{LibGit2.GitHash}) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.3\LibGit2\src\oid.jl:220
  zero(!Matched::Type{Pkg.Resolve.VersionWeights.VersionWeight}) at D:\buildbot\worker\package_win64\build\usr\share\julia\stdlib\v1.3\Pkg\src\resolve\VersionWeights.jl:19
  ...

Stacktrace:
 [1] alg_cache(::Tsit5, ::NTuple{7,Float64}, ::NTuple{7,Float64}, ::Type, ::Type, ::Type, ::NTuple{7,Float64}, ::NTuple{7,Float64}, ::ODEFunction{true,typeof(bsys!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing}, ::Float64, ::Float64, ::Float64, ::Float64, ::Bool, ::Val{true}) at C:\Users\hganj\.julia\packages\OrdinaryDiffEq\8Pn99\src\caches\low_order_rk_caches.jl:349
 [2] (::OrdinaryDiffEq.var"#197#198"{NTuple{7,Float64},NTuple{7,Float64},DataType,DataType,DataType,NTuple{7,Float64},NTuple{7,Float64},ODEFunction{true,typeof(bsys!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Float64,Float64,Float64,Float64,Bool})(::Tsit5) at C:\Users\hganj\.julia\packages\OrdinaryDiffEq\8Pn99\src\caches\basic_caches.jl:22
 [3] map(::OrdinaryDiffEq.var"#197#198"{NTuple{7,Float64},NTuple{7,Float64},DataType,DataType,DataType,NTuple{7,Float64},NTuple{7,Float64},ODEFunction{true,typeof(bsys!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Float64,Float64,Float64,Float64,Bool}, ::Tuple{Tsit5,Rosenbrock23{0,false,DefaultLinSolve,DataType}}) at .\tuple.jl:140
 [4] alg_cache(::CompositeAlgorithm{Tuple{Tsit5,Rosenbrock23{0,false,DefaultLinSolve,DataType}},AutoSwitch{Tsit5,Rosenbrock23{0,false,DefaultLinSolve,DataType},Rational{Int64},Int64}}, ::NTuple{7,Float64}, ::NTuple{7,Float64}, ::Type, ::Type, ::Type, ::NTuple{7,Float64}, ::NTuple{7,Float64}, ::ODEFunction{true,typeof(bsys!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing}, ::Float64, ::Float64, ::Float64, ::Float64, ::Bool, ::Val{true}) at C:\Users\hganj\.julia\packages\OrdinaryDiffEq\8Pn99\src\caches\basic_caches.jl:21
 [5] #__init#329(::Array{Float64,1}, ::Array{Float64,1}, ::Array{Float64,1}, ::Nothing, ::Bool, ::Bool, ::Bool, ::Bool, ::Nothing, ::Bool, ::Bool, ::Float64, ::Float64, ::Float64, ::Bool, ::Bool, ::Rational{Int64}, ::Nothing, ::Nothing, ::Rational{Int64}, ::Int64, ::Int64, ::Int64, ::Rational{Int64}, ::Bool, ::Int64, ::Nothing, ::Nothing, ::Int64, ::typeof(DiffEqBase.ODE_DEFAULT_NORM), ::typeof(LinearAlgebra.opnorm), ::typeof(DiffEqBase.ODE_DEFAULT_ISOUTOFDOMAIN), ::typeof(DiffEqBase.ODE_DEFAULT_UNSTABLE_CHECK), ::Bool, ::Bool, ::Bool, ::Bool, ::Bool, ::Bool, ::Bool, ::Int64, ::String, ::typeof(DiffEqBase.ODE_DEFAULT_PROG_MESSAGE), ::Nothing, ::Bool, ::Bool, ::Bool, ::Bool, ::Base.Iterators.Pairs{Symbol,Bool,Tuple{Symbol,Symbol},NamedTuple{(:default_set, :second_time),Tuple{Bool,Bool}}}, ::typeof(DiffEqBase.__init), ::ODEProblem{NTuple{7,Float64},Tuple{Float64,Float64},true,Float64,ODEFunction{true,typeof(bsys!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem}, ::CompositeAlgorithm{Tuple{Tsit5,Rosenbrock23{0,false,DefaultLinSolve,DataType}},AutoSwitch{Tsit5,Rosenbrock23{0,false,DefaultLinSolve,DataType},Rational{Int64},Int64}}, ::Array{NTuple{7,Float64},1}, ::Array{Float64,1}, ::Array{Any,1}, ::Type{Val{true}}) at C:\Users\hganj\.julia\packages\OrdinaryDiffEq\8Pn99\src\solve.jl:281
 [6] (::DiffEqBase.var"#kw##__init")(::NamedTuple{(:default_set, :second_time),Tuple{Bool,Bool}}, ::typeof(DiffEqBase.__init), ::ODEProblem{NTuple{7,Float64},Tuple{Float64,Float64},true,Float64,ODEFunction{true,typeof(bsys!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem}, ::CompositeAlgorithm{Tuple{Tsit5,Rosenbrock23{0,false,DefaultLinSolve,DataType}},AutoSwitch{Tsit5,Rosenbrock23{0,false,DefaultLinSolve,DataType},Rational{Int64},Int64}}, ::Array{NTuple{7,Float64},1}, ::Array{Float64,1}, ::Array{Any,1}, ::Type{Val{true}}) at .\none:0 (repeats 5 times)
 [7] #__solve#328(::Base.Iterators.Pairs{Symbol,Bool,Tuple{Symbol,Symbol},NamedTuple{(:default_set, :second_time),Tuple{Bool,Bool}}}, ::typeof(DiffEqBase.__solve), ::ODEProblem{NTuple{7,Float64},Tuple{Float64,Float64},true,Float64,ODEFunction{true,typeof(bsys!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem}, ::CompositeAlgorithm{Tuple{Tsit5,Rosenbrock23{0,false,DefaultLinSolve,DataType}},AutoSwitch{Tsit5,Rosenbrock23{0,false,DefaultLinSolve,DataType},Rational{Int64},Int64}}) at C:\Users\hganj\.julia\packages\OrdinaryDiffEq\8Pn99\src\solve.jl:4
 [8] (::DiffEqBase.var"#kw##__solve")(::NamedTuple{(:default_set, :second_time),Tuple{Bool,Bool}}, ::typeof(DiffEqBase.__solve), ::ODEProblem{NTuple{7,Float64},Tuple{Float64,Float64},true,Float64,ODEFunction{true,typeof(bsys!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem}, ::CompositeAlgorithm{Tuple{Tsit5,Rosenbrock23{0,false,DefaultLinSolve,DataType}},AutoSwitch{Tsit5,Rosenbrock23{0,false,DefaultLinSolve,DataType},Rational{Int64},Int64}}) at .\none:0
 [9] #__solve#1(::Bool, ::Base.Iterators.Pairs{Symbol,Bool,Tuple{Symbol},NamedTuple{(:second_time,),Tuple{Bool}}}, ::typeof(DiffEqBase.__solve), ::ODEProblem{NTuple{7,Float64},Tuple{Float64,Float64},true,Float64,ODEFunction{true,typeof(bsys!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem}, ::Nothing) at C:\Users\hganj\.julia\packages\DifferentialEquations\hc2aQ\src\default_solve.jl:7
 [10] #__solve at .\none:0 [inlined]
 [11] #__solve#448 at C:\Users\hganj\.julia\packages\DiffEqBase\mDFok\src\solve.jl:185 [inlined]
 [12] __solve at C:\Users\hganj\.julia\packages\DiffEqBase\mDFok\src\solve.jl:180 [inlined]
 [13] #solve_call#443(::Bool, ::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}, ::typeof(DiffEqBase.solve_call), ::ODEProblem{NTuple{7,Float64},Tuple{Float64,Float64},true,Float64,ODEFunction{true,typeof(bsys!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem}) at C:\Users\hganj\.julia\packages\DiffEqBase\mDFok\src\solve.jl:44
 [14] solve_call at C:\Users\hganj\.julia\packages\DiffEqBase\mDFok\src\solve.jl:37 [inlined]
 [15] #solve#444 at C:\Users\hganj\.julia\packages\DiffEqBase\mDFok\src\solve.jl:67 [inlined]
 [16] solve(::ODEProblem{NTuple{7,Float64},Tuple{Float64,Float64},true,Float64,ODEFunction{true,typeof(bsys!),LinearAlgebra.UniformScaling{Bool},Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing,Nothing},Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}},DiffEqBase.StandardODEProblem}) at C:\Users\hganj\.julia\packages\DiffEqBase\mDFok\src\solve.jl:51
 [17] top-level scope at In[160]:1

ODE函数bsys中的各种函数调用均正常工作。我认为问题在于我在表达式中为dv赋值的算法。

对此将提供任何帮助。我是朱莉娅(Julia)的新手,不知道出什么问题了。

julia differential-equations differentialequations.jl
1个回答
0
投票

我认为init必须是数组而不是元组。尝试使用init = [ ... ]而不是init = ( ... )

© www.soinside.com 2019 - 2024. All rights reserved.