MethodError: no method matching Float64(::Num) ERROR

Hello, I am new to Julia. I have used MATLAB earlier. I am trying to solve a system of ODE using BoundaryValueDiffEq with the help of simple pendulum example in Julia docs and I am getting an error. Please help me resolve it.

using NeuralPDE, Flux, ModelingToolkit, GalacticOptim, Optim, DiffEqFlux, OrdinaryDiffEq,BoundaryValueDiffEq, Plots
import ModelingToolkit: Interval, infimum, supremum
@parameters eta
@variables y1(..),y2(..),y3(..),y4(..),y5(..),y6(..),y7(..),y8(..)

Deta = Differential(eta)
eta_min= 0.
eta_max = 6.0
lambda=-1
n=0.5
S=2.2
 beta=1.1
 K1=0.1
 rad=2
 phi0=0.005
 Pr=21
 Rhof=1150
 Rhos=19300
 Cpf=3617
 Cps=128
 Kf=0.53
 Ks=328
 tmp1=1-phi0
 tmp2=Ks-Kf
 tmp3=Ks+2*Kf
 tmp4=Cps*Rhos
 tmp5=Cpf*Rhof
 A1=tmp1+phi0*Rhos/Rhof
 A2=tmp1^(-2.5)
 A3=(2*phi0*tmp2+tmp3)/(-1*phi0*tmp2+tmp3)
 A4=tmp1+phi0*tmp4/tmp5
 eb = eta + beta
tspan = (0.0,6.0)
function simplependulum!(dz,z,p,eta)
 form1=A2+K1
 form2=z[2]*z[3]-z[1]*z[4]
 form3=z[2]*z[1]-z[1]*z[3]
 form4=z[1]*z[6]-z[5]*z[2]
 form5=-2*z[4]/eb
 form6=z[3]/(eb^2)
 form7=z[3]/(eb^3)
 form8=beta*A1/eb
 form9=beta*A1/eb^2
 form10=beta*A1/eb^3
 form11=K1*z[6]/eb
 form12=-1*z[6]/eb
 form13=K1/A1
 form14=z[2]/eb
 form15=-1*z[8]/eb
 vari1=beta/(eb*(A2/A1+2*form13))
 vari2=2*z[5]+z[3]+form14
 vari3=A4*Pr*beta/((A3+4/3*rad)*eb)

 formu3=form15-vari3*z[8]*z[1]
 formu2=form12-vari1*form4+form13*vari2
 formu1=(form1*(form5+form6-form7)+form8*form2+form9*form3+form10*z[2]*z[1]+form11+K1*formu2)/form1
 
 dz[1] = z[2]
 dz[2] = z[3]
 dz[3] = z[4]
 dz[4] = formu1
 dz[5] = z[6]
 dz[6] = formu2
 dz[7] = z[8]
 dz[8] = formu3
end
function bc1!(residual, z, p, eta)
    residual[1] = z[1][1] - S
    residual[2] = z[1][2] - lambda
    residual[3] = z[end][2] - 0.0
    residual[4] = z[end][3] - 0.0
    residual[5] = z[1][5] + n*z[1][3]
    residual[6] = z[end][5] - 0.0
    residual[7] = z[1][7] - 1.0
    residual[8] = z[end][7] - 0.0
end
bvp1 = BVProblem(simplependulum!, bc1!, [0.,0.,0.,0.,0.,0.,1.,0.], tspan)
sol1 = solve(bvp1, Shooting(Tsit5()), dt=0.1)

THE ERROR I AM GETTING IS:

Closest candidates are:
  (::Type{T})(::Real, ::RoundingMode) where T<:AbstractFloat at C:\Users\Shantanu\AppData\Local\Programs\Julia-1.7.2\share\julia\base\rounding.jl:200
  (::Type{T})(::T) where T<:Number at C:\Users\Shantanu\AppData\Local\Programs\Julia-1.7.2\share\julia\base\boot.jl:770
  (::Type{T})(::SymbolicUtils.Symbolic) where T<:Union{AbstractFloat, Integer, Complex{<:Integer}, Complex{<:AbstractFloat}} at C:\Users\Shantanu\.julia\packages\Symbolics\vQXbU\src\Symbolics.jl:135
  ...

Stacktrace:
  [1] convert(#unused#::Type{Float64}, x::Num)
    @ Base .\number.jl:7
  [2] setindex!(A::Vector{Float64}, x::Num, i1::Int64)
    @ Base .\array.jl:903
  [3] simplependulum!(dz::Vector{Float64}, z::Vector{Float64}, p::SciMLBase.NullParameters, eta::Float64)
    @ Main .\In[125]:31
  [4] ODEFunction
    @ C:\Users\Shantanu\.julia\packages\SciMLBase\nbKmA\src\scimlfunctions.jl:345 [inlined]
  [5] initialize!(integrator::OrdinaryDiffEq.ODEIntegrator{Tsit5{typeof(OrdinaryDiffEq.trivial_limiter!), typeof(OrdinaryDiffEq.trivial_limiter!), Static.False}, true, Vector{Float64}, Nothing, Float64, SciMLBase.NullParameters, Float64, Float64, Float64, Float64, Vector{Vector{Float64}}, ODESolution{Float64, 2, Vector{Vector{Float64}}, Nothing, Nothing, Vector{Float64}, Vector{Vector{Vector{Float64}}}, BVProblem{Vector{Float64}, Tuple{Float64, Float64}, true, SciMLBase.NullParameters, ODEFunction{true, typeof(simplependulum!), LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, typeof(bc1!), SciMLBase.StandardBVProblem, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}}, Tsit5{typeof(OrdinaryDiffEq.trivial_limiter!), typeof(OrdinaryDiffEq.trivial_limiter!), Static.False}, OrdinaryDiffEq.InterpolationData{ODEFunction{true, typeof(simplependulum!), LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, Vector{Vector{Float64}}, Vector{Float64}, Vector{Vector{Vector{Float64}}}, OrdinaryDiffEq.Tsit5Cache{Vector{Float64}, Vector{Float64}, Vector{Float64}, OrdinaryDiffEq.Tsit5ConstantCache{Float64, Float64}, typeof(OrdinaryDiffEq.trivial_limiter!), typeof(OrdinaryDiffEq.trivial_limiter!), Static.False}}, DiffEqBase.DEStats}, ODEFunction{true, typeof(simplependulum!), LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, OrdinaryDiffEq.Tsit5Cache{Vector{Float64}, Vector{Float64}, Vector{Float64}, OrdinaryDiffEq.Tsit5ConstantCache{Float64, Float64}, typeof(OrdinaryDiffEq.trivial_limiter!), typeof(OrdinaryDiffEq.trivial_limiter!), Static.False}, OrdinaryDiffEq.DEOptions{Float64, Float64, Float64, Float64, PIController{Rational{Int64}}, typeof(DiffEqBase.ODE_DEFAULT_NORM), typeof(LinearAlgebra.opnorm), Nothing, CallbackSet{Tuple{}, Tuple{}}, typeof(DiffEqBase.ODE_DEFAULT_ISOUTOFDOMAIN), typeof(DiffEqBase.ODE_DEFAULT_PROG_MESSAGE), typeof(DiffEqBase.ODE_DEFAULT_UNSTABLE_CHECK), DataStructures.BinaryHeap{Float64, DataStructures.FasterForward}, DataStructures.BinaryHeap{Float64, DataStructures.FasterForward}, Nothing, Nothing, Int64, Tuple{}, Tuple{}, Tuple{}}, Vector{Float64}, Float64, Nothing, OrdinaryDiffEq.DefaultInit}, cache::OrdinaryDiffEq.Tsit5Cache{Vector{Float64}, Vector{Float64}, Vector{Float64}, OrdinaryDiffEq.Tsit5ConstantCache{Float64, Float64}, typeof(OrdinaryDiffEq.trivial_limiter!), typeof(OrdinaryDiffEq.trivial_limiter!), Static.False})
    @ OrdinaryDiffEq C:\Users\Shantanu\.julia\packages\OrdinaryDiffEq\ZBye7\src\perform_step\low_order_rk_perform_step.jl:627
  [6] __init(prob::BVProblem{Vector{Float64}, Tuple{Float64, Float64}, true, SciMLBase.NullParameters, ODEFunction{true, typeof(simplependulum!), LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, typeof(bc1!), SciMLBase.StandardBVProblem, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}}, alg::Tsit5{typeof(OrdinaryDiffEq.trivial_limiter!), typeof(OrdinaryDiffEq.trivial_limiter!), Static.False}, timeseries_init::Tuple{}, ts_init::Tuple{}, ks_init::Tuple{}, recompile::Type{Val{true}}; saveat::Tuple{}, tstops::Tuple{}, d_discontinuities::Tuple{}, save_idxs::Nothing, save_everystep::Bool, save_on::Bool, save_start::Bool, save_end::Nothing, callback::Nothing, dense::Bool, calck::Bool, dt::Float64, dtmin::Nothing, dtmax::Float64, force_dtmin::Bool, adaptive::Bool, gamma::Rational{Int64}, abstol::Nothing, reltol::Nothing, qmin::Rational{Int64}, qmax::Int64, qsteady_min::Int64, qsteady_max::Int64, beta1::Nothing, beta2::Nothing, qoldinit::Rational{Int64}, controller::Nothing, fullnormalize::Bool, failfactor::Int64, maxiters::Int64, internalnorm::typeof(DiffEqBase.ODE_DEFAULT_NORM), internalopnorm::typeof(LinearAlgebra.opnorm), isoutofdomain::typeof(DiffEqBase.ODE_DEFAULT_ISOUTOFDOMAIN), unstable_check::typeof(DiffEqBase.ODE_DEFAULT_UNSTABLE_CHECK), verbose::Bool, timeseries_errors::Bool, dense_errors::Bool, advance_to_tstop::Bool, stop_at_next_tstop::Bool, initialize_save::Bool, progress::Bool, progress_steps::Int64, progress_name::String, progress_message::typeof(DiffEqBase.ODE_DEFAULT_PROG_MESSAGE), userdata::Nothing, allow_extrapolation::Bool, initialize_integrator::Bool, alias_u0::Bool, alias_du0::Bool, initializealg::OrdinaryDiffEq.DefaultInit, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ OrdinaryDiffEq C:\Users\Shantanu\.julia\packages\OrdinaryDiffEq\ZBye7\src\solve.jl:456
  [7] #__solve#502
    @ C:\Users\Shantanu\.julia\packages\OrdinaryDiffEq\ZBye7\src\solve.jl:4 [inlined]
  [8] #solve_call#39
    @ C:\Users\Shantanu\.julia\packages\DiffEqBase\U3LtB\src\solve.jl:155 [inlined]
  [9] #solve_up#41
    @ C:\Users\Shantanu\.julia\packages\DiffEqBase\U3LtB\src\solve.jl:182 [inlined]
 [10] #solve#40
    @ C:\Users\Shantanu\.julia\packages\DiffEqBase\U3LtB\src\solve.jl:168 [inlined]
 [11] (::BoundaryValueDiffEq.var"#18#19"{Base.Pairs{Symbol, Float64, Tuple{Symbol}, NamedTuple{(:dt,), Tuple{Float64}}}, BVProblem{Vector{Float64}, Tuple{Float64, Float64}, true, SciMLBase.NullParameters, ODEFunction{true, typeof(simplependulum!), LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, typeof(bc1!), SciMLBase.StandardBVProblem, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}}, Shooting{Tsit5{typeof(OrdinaryDiffEq.trivial_limiter!), typeof(OrdinaryDiffEq.trivial_limiter!), Static.False}, BoundaryValueDiffEq.var"#1#2"}, typeof(bc1!)})(resid::Vector{Float64}, minimizer::Vector{Float64})
    @ BoundaryValueDiffEq C:\Users\Shantanu\.julia\packages\BoundaryValueDiffEq\fnUX7\src\solve.jl:11
 [12] (::NLSolversBase.var"#fj_finitediff!#21"{BoundaryValueDiffEq.var"#18#19"{Base.Pairs{Symbol, Float64, Tuple{Symbol}, NamedTuple{(:dt,), Tuple{Float64}}}, BVProblem{Vector{Float64}, Tuple{Float64, Float64}, true, SciMLBase.NullParameters, ODEFunction{true, typeof(simplependulum!), LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, typeof(bc1!), SciMLBase.StandardBVProblem, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}}, Shooting{Tsit5{typeof(OrdinaryDiffEq.trivial_limiter!), typeof(OrdinaryDiffEq.trivial_limiter!), Static.False}, BoundaryValueDiffEq.var"#1#2"}, typeof(bc1!)}, FiniteDiff.JacobianCache{Vector{Float64}, Vector{Float64}, Vector{Float64}, UnitRange{Int64}, Nothing, Val{:central}(), Float64}})(F::Vector{Float64}, J::Matrix{Float64}, x::Vector{Float64})
    @ NLSolversBase C:\Users\Shantanu\.julia\packages\NLSolversBase\cfJrN\src\objective_types\oncedifferentiable.jl:138
 [13] value_jacobian!!(obj::OnceDifferentiable{Vector{Float64}, Matrix{Float64}, Vector{Float64}}, F::Vector{Float64}, J::Matrix{Float64}, x::Vector{Float64})
    @ NLSolversBase C:\Users\Shantanu\.julia\packages\NLSolversBase\cfJrN\src\interface.jl:124
 [14] value_jacobian!!
    @ C:\Users\Shantanu\.julia\packages\NLSolversBase\cfJrN\src\interface.jl:122 [inlined]
 [15] trust_region_(df::OnceDifferentiable{Vector{Float64}, Matrix{Float64}, Vector{Float64}}, initial_x::Vector{Float64}, xtol::Float64, ftol::Float64, iterations::Int64, store_trace::Bool, show_trace::Bool, extended_trace::Bool, factor::Float64, autoscale::Bool, cache::NLsolve.NewtonTrustRegionCache{Vector{Float64}})
    @ NLsolve C:\Users\Shantanu\.julia\packages\NLsolve\gJL1I\src\solvers\trust_region.jl:119
 [16] trust_region (repeats 2 times)
    @ C:\Users\Shantanu\.julia\packages\NLsolve\gJL1I\src\solvers\trust_region.jl:235 [inlined]
 [17] nlsolve(df::OnceDifferentiable{Vector{Float64}, Matrix{Float64}, Vector{Float64}}, initial_x::Vector{Float64}; method::Symbol, xtol::Float64, ftol::Float64, iterations::Int64, store_trace::Bool, show_trace::Bool, extended_trace::Bool, linesearch::LineSearches.Static, linsolve::NLsolve.var"#27#29", factor::Float64, autoscale::Bool, m::Int64, beta::Int64, aa_start::Int64, droptol::Float64)
    @ NLsolve C:\Users\Shantanu\.julia\packages\NLsolve\gJL1I\src\nlsolve\nlsolve.jl:26
 [18] nlsolve(f::Function, initial_x::Vector{Float64}; method::Symbol, autodiff::Symbol, inplace::Bool, kwargs::Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}})
    @ NLsolve C:\Users\Shantanu\.julia\packages\NLsolve\gJL1I\src\nlsolve\nlsolve.jl:52
 [19] nlsolve(f::Function, initial_x::Vector{Float64})
    @ NLsolve C:\Users\Shantanu\.julia\packages\NLsolve\gJL1I\src\nlsolve\nlsolve.jl:46
 [20] #1
    @ C:\Users\Shantanu\.julia\packages\BoundaryValueDiffEq\fnUX7\src\algorithms.jl:11 [inlined]
 [21] __solve(prob::BVProblem{Vector{Float64}, Tuple{Float64, Float64}, true, SciMLBase.NullParameters, ODEFunction{true, typeof(simplependulum!), LinearAlgebra.UniformScaling{Bool}, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, Nothing, typeof(SciMLBase.DEFAULT_OBSERVED), Nothing}, typeof(bc1!), SciMLBase.StandardBVProblem, Base.Pairs{Symbol, Union{}, Tuple{}, NamedTuple{(), Tuple{}}}}, alg::Shooting{Tsit5{typeof(OrdinaryDiffEq.trivial_limiter!), typeof(OrdinaryDiffEq.trivial_limiter!), Static.False}, BoundaryValueDiffEq.var"#1#2"}; kwargs::Base.Pairs{Symbol, Float64, Tuple{Symbol}, NamedTuple{(:dt,), Tuple{Float64}}})
    @ BoundaryValueDiffEq C:\Users\Shantanu\.julia\packages\BoundaryValueDiffEq\fnUX7\src\solve.jl:15

That error says it doesn’t know how to handle a symbolic value. When I run your code, eta is not defined, so I assume that in a previous case you defined eta to be a symbolic variable then switched to doing this without clearing your variable.

I have now posted the whole code and I have defined eta as a parameter.Can you please have a look at it again?

I don’t understand your code. Why is eta defined as symbolic there, but then you use the BVP solver? Those are two completely different packages: one is NeuralPDE stuff and the other is BoundaryValueDiffEq.jl

I guess let’s take a step back, what are you trying to do? Are you trying to solve the BVP, but eta_min and eta_max, are you trying to solve the BVP and estimate eta?

I have used pinn to solve my system of ODE, thats why there is NeuralPDE stuff and I wanted to compare that solution to the solution from BVP solvers.I want eta to be an independent variable like t is for time generally.

Oh I see, eta is your independent variable. So then what is eb? Is that a quantity dependent on the independent variable?

Yes it is eta + a constant term

Yeah so you should move eb = eta + beta into simplependulum! so it’s calculated differently at each eta-point.

2 Likes

Oh yes you are right!!
Thanks!

1 Like

Yeah so eta was basically just not defined, it was just picking up the definition from your NeuralPDE.jl code, hence the weird error when the symbolic variable ended up in the BVP solver. No problem.

1 Like

Can you tell me how to give initial guess as function of t or eta(in my case) in BVProblem?

Make u0 be an AbstractArray{>:AbstractArray}), i.e. an array of arrays.

I am getting exactly the same MethodError :no method matching Float64(::Num) but code is different. Can you explain what you learned about that error ? What is main cause ? How to solve ?

Please post code in a new thread. As the error says, it’s just from trying to convert a Num to a Float64. Why is it doing that? I don’t know, I haven’t seen your code. Did you define a Vector{Float64} and then a[i] = vforvaNum`?

1 Like

Dict line is main source of error.

Is substitute.(M, (Dict([p => prob]),)) a Matrix{Num} that then just happens to all be Float64 numbers? Can you boil this down into an MWE?

What is computed? Show before the plot