I am getting this error randomly while running optimization using NLopt

Hello Julia experts. I am getting the following error message randomly while running an optimization in a for loop.
Can someone help me to identify the issue?

julia> status = solve(m2)
in callback catch
ERROR: Base.LinAlg.LAPACKException(1)
Stacktrace:
[1] chklapackerror(::Int64) at ./linalg/lapack.jl:34
[2] trtrs!(::Char, ::Char, ::Char, ::Array{Float64,2}, ::Array{Float64,2}) at ./linalg/lapack.jl:3291
[3] inv at ./linalg/triangular.jl:536 [inlined]
[4] inv(::Array{Float64,2}) at ./linalg/dense.jl:655
[5] myf2(::Float64, ::Float64, ::Float64, ::Float64, ::Float64, ::Float64, ::Float64, ::Float64, ::Float64, ::Float64) at ./REPL[94]:13
[6] (::JuMP.##166#168{#myf2})(::SubArray{Float64,1,Array{Float64,1},Tuple{UnitRange{Int64}},true}) at /Users/jangikcho/.julia/v0.6/JuMP/src/nlp.jl:1363
[7] eval_f(::JuMP.UserFunctionEvaluator, ::SubArray{Float64,1,Array{Float64,1},Tuple{UnitRange{Int64}},true}) at /Users/jangikcho/.julia/v0.6/JuMP/src/nlp.jl:1355
[8] #forward_eval#7(::ReverseDiffSparse.UserOperatorRegistry, ::Function, ::Array{Float64,1}, ::Array{Float64,1}, ::Array{ReverseDiffSparse.NodeData,1}, ::SparseMatrixCSC{Bool,Int64}, ::Array{Float64,1}, ::Array{Float64,1}, ::Array{Float64,1}, ::Array{Float64,1}, ::Array{Float64,1}, ::Array{Float64,1}) at /Users/jangikcho/.julia/v0.6/ReverseDiffSparse/src/forward.jl:128
[9] (::ReverseDiffSparse.#kw##forward_eval)(::Array{Any,1}, ::ReverseDiffSparse.#forward_eval, ::Array{Float64,1}, ::Array{Float64,1}, ::Array{ReverseDiffSparse.NodeData,1}, ::SparseMatrixCSC{Bool,Int64}, ::Array{Float64,1}, ::Array{Float64,1}, ::Array{Float64,1}, ::Array{Float64,1}, ::Array{Float64,1}, ::Array{Float64,1}) at ./:0
[10] forward_eval_all(::JuMP.NLPEvaluator, ::Array{Float64,1}) at /Users/jangikcho/.julia/v0.6/JuMP/src/nlp.jl:447
[11] eval_f(::JuMP.NLPEvaluator, ::Array{Float64,1}) at /Users/jangikcho/.julia/v0.6/JuMP/src/nlp.jl:480
[12] (::NLopt.#f#4{JuMP.NLPEvaluator})(::Array{Float64,1}, ::Array{Float64,1}) at /Users/jangikcho/.julia/v0.6/NLopt/src/NLoptSolverInterface.jl:123
[13] nlopt_callback_wrapper(::UInt32, ::Ptr{Float64}, ::Ptr{Float64}, ::Ptr{Void}) at /Users/jangikcho/.julia/v0.6/NLopt/src/NLopt.jl:427
[14] optimize!(::NLopt.Opt, ::Array{Float64,1}) at /Users/jangikcho/.julia/v0.6/NLopt/src/NLopt.jl:526
[15] optimize!(::NLopt.NLoptMathProgModel) at /Users/jangikcho/.julia/v0.6/NLopt/src/NLoptSolverInterface.jl:203
[16] #solvenlp#165(::Bool, ::Function, ::JuMP.Model, ::JuMP.ProblemTraits) at /Users/jangikcho/.julia/v0.6/JuMP/src/nlp.jl:1271
[17] (::JuMP.#kw##solvenlp)(::Array{Any,1}, ::JuMP.#solvenlp, ::JuMP.Model, ::JuMP.ProblemTraits) at ./:0
[18] #solve#116(::Bool, ::Bool, ::Bool, ::Array{Any,1}, ::Function, ::JuMP.Model) at /Users/jangikcho/.julia/v0.6/JuMP/src/solvers.jl:172
[19] solve(::JuMP.Model) at /Users/jangikcho/.julia/v0.6/JuMP/src/solvers.jl:150

That means something you’re doing a linear solve on is not invertable. Looks like it’s an inv call in your function myf2. Generally you shouldn’t use inv anyways, and instead use matrix factorizations and \. Could you share myf2?

Thank you for the response. I do have an inverse function within myf2.
Here is myf2. In the function,

function myf2(a1,a2,a3,a4,a5,b1,b2,b3,b4,b5)
w1 = [a1;a2;a3;a4;a5]' * xcrit
w2 = [b1;b2;b3;b4;b5]' * xcrit
z1 = [a1;a2;a3;a4;a5]' * ycrit
z2 = [b1;b2;b3;b4;b5]' * ycrit

S1 = [var(w1) cov(w1',w2'); cov(w2',w1') var(w2)]
S2 = [var(z1) cov(z1',z2'); cov(z2',z1') var(z2)]

dist = inv(S1) * S2 - eye(2)

result = sqrt(sum(dist.*dist))
return result
end

No you don’t. That’s the error. For some reason you’re ending up in a place in parameter space where it’s not invertable. You may want to use pinv instead, or put some constraints on the model.

Since it’s inverse of a 2x2 matrix, I wrote out the formula of inverse matrix and it works without the error.
Thank you very much for the help!