The following code in Julia plots a Lorenztian curve and then uses the curve_fit function to determine the parameters.
using LsqFit
model(x,p)=p[1] ./(p[1]^2 .+(x .-p[2]).^2)
#Test values
p0=[10,50]
tdata=range(-150,stop=150,length=300)
ydata = model(tdata, p0)
fit=curve_fit(model,tdata,ydata,p0)
In this case the result should be exact as I use the model to calculate the y-values and then pass these exact y-values to the curve_fit function. However, Julia returns the error:
InexactError: Int64(NaN)
Int64(::Float64) at float.jl:710
x_of_nans(::Array{Int64,1}, ::Type{T} where T) at NLSolversBase.jl:60
x_of_nans(::Array{Int64,1}) at NLSolversBase.jl:60
NLSolversBase.OnceDifferentiable(::Function, ::Function, ::Function, ::Array{Int64,1}, ::Array{Float64,1}, ::Array{Float64,2}; inplace::Bool) at oncedifferentiable.jl:229
NLSolversBase.OnceDifferentiable(::Function, ::Function, ::Function, ::Array{Int64,1}, ::Array{Float64,1}, ::Array{Float64,2}) at oncedifferentiable.jl:225
NLSolversBase.OnceDifferentiable(::NLSolversBase.var"#ff!#1"{LsqFit.var"#18#20"{typeof(model),StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}},Array{Float64,1}}}, ::Array{Int64,1}, ::Array{Float64,1}, ::Array{Float64,2}, ::Symbol, ::ForwardDiff.Chunk{2}) at oncedifferentiable.jl:147
NLSolversBase.OnceDifferentiable(::Function, ::Array{Int64,1}, ::Array{Float64,1}, ::Array{Float64,2}, ::Symbol) at oncedifferentiable.jl:96
NLSolversBase.OnceDifferentiable(::Function, ::Array{Int64,1}, ::Array{Float64,1}, ::Array{Float64,2}; inplace::Bool, autodiff::Symbol) at oncedifferentiable.jl:28
(::Core.var"#Type##kw")(::NamedTuple{(:inplace, :autodiff),Tuple{Bool,Symbol}}, ::Type{NLSolversBase.OnceDifferentiable}, ::Function, ::Array{Int64,1}, ::Array{Float64,1}, ::Array{Float64,2}) at oncedifferentiable.jl:26
(::Core.var"#Type##kw")(::NamedTuple{(:inplace, :autodiff),Tuple{Bool,Symbol}}, ::Type{NLSolversBase.OnceDifferentiable}, ::Function, ::Array{Int64,1}, ::Array{Float64,1}) at oncedifferentiable.jl:26
lmfit(::LsqFit.var"#18#20"{typeof(model),StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}},Array{Float64,1}}, ::Array{Int64,1}, ::Array{Float64,1}; autodiff::Symbol, kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at curve_fit.jl:63
lmfit(::Function, ::Array{Int64,1}, ::Array{Float64,1}) at curve_fit.jl:61
curve_fit(::typeof(model), ::StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}, ::Array{Float64,1}, ::Array{Int64,1}; inplace::Bool, kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at curve_fit.jl:115
curve_fit(::Function, ::StepRangeLen{Float64,Base.TwicePrecision{Float64},Base.TwicePrecision{Float64}}, ::Array{Float64,1}, ::Array{Int64,1}) at curve_fit.jl:106
top-level scope at mcconnel_simulation.jl:71
Why is curve_fit unable to solve this easy case?
p0 needs to have float eltype (for example Float64):
julia> p0=[10.0,50.0]
2-element Array{Float64,1}:
10.0
50.0
julia> fit=curve_fit(model,collect(tdata),ydata,p0)
LsqFit.LsqFitResult{Array{Float64,1},Array{Float64,1},Array{Float64,2},Array{Float64,1}}([10.0, 50.0], [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 … -2.168404344971009e-19, -6.505213034913027e-19, 2.168404344971009e-19, 4.336808689942018e-19, 2.168404344971009e-19, 2.168404344971009e-19, 0.0, 0.0, 0.0, 0.0], [2.4813278524001437e-5 -2.4875467192362816e-6; 2.506222852629442e-5 -2.52523618286487e-6; … ; 9.896618565083517e-5 2.0199959191107846e-5; 9.704930889082663e-5 1.9605920988595578e-5], true, Float64[])
you can also write p0 = Float64[10,50] to make sure its eltype is Float64.
I want to solve a system of seven coupled ODEs in Julia.
My code is:
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)
I get the following error:
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
The various function calls within the ODE function bsys are working correctly. I think the issue is with the arithmetic I am doing in the expressions to assign values to dv.
Any help on this would be appreciated. I am new to Julia and have no idea what might be going wrong.
I think init needs to be an array instead of a tuple. Try init = [ ... ] instead of init = ( ... ).