# Problem optimizing a 2 variable function (Optim .jl)

**URL:** <https://discourse.julialang.org/t/problem-optimizing-a-2-variable-function-optim-jl/65655>\
**Category:** Optimization (Mathematical)\
**Tags:** optim, optimization\
**Created:** [August 2, 2021, 1:52am UTC](https://discourse.julialang.org/t/problem-optimizing-a-2-variable-function-optim-jl/65655 "2021-08-02T01:52:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![AndresCasillas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrescasillas/32/27133_2.png) [@AndresCasillas](https://discourse.julialang.org/u/AndresCasillas)\
**Post date:** [August 2, 2021, 1:52am UTC](https://discourse.julialang.org/t/problem-optimizing-a-2-variable-function-optim-jl/65655/1 "2021-08-02T01:52:53Z")

</div>

Hi! I want to optimize a 2 variable function using Optim.jl. I’ve read the documentation but I still can’t figure it out. I hope someone can help me. (I’m using Optim and using MittagLeffler on a Jupyter notebook with Julia 1.5.3).

I have two arrays of data x\_1 and y\_1. I have defined the following function which I want to optimize:

```
function distancia2(α, m)    

distancias = 0.0

for j in 1:length(x_1) #length(x) = length(y)
    
    distancias += (9.387683485474406*mittleff(α, m*x_1[j]) + 71.89597427356132 - y_1[j])^2
                                               
end

return distancias

```

end

which is basically the square residues of the Mittag-Leffler function to the set of data points that I have.

I want to find \alpha and m such that minimize distancias2(\alpha, m). I have tried: optimize(distancia2, [0.9900284232219172, -0.0067826190463882875]) (which are my initial guesses), but get the following error:

MethodError: no method matching distancia2(::Array{Float64,1})  
Closest candidates are:  
distancia2(::Any, ::Any) at In[24]:3

Stacktrace:  
[1] value!!(::NonDifferentiable{Float64,Array{Float64,1}}, ::Array{Float64,1}) at C:\Users\progr.julia\packages\NLSolversBase\geyh3\src\interface.jl:9  
[2] initial\_state(::NelderMead{Optim.AffineSimplexer,Optim.AdaptiveParameters}, ::Optim.Options{Float64,Nothing}, ::NonDifferentiable{Float64,Array{Float64,1}}, ::Array{Float64,1}) at C:\Users\progr.julia\packages\Optim\3K7JI\src\multivariate\solvers\zeroth\_order\nelder\_mead.jl:171  
[3] optimize(::NonDifferentiable{Float64,Array{Float64,1}}, ::Array{Float64,1}, ::NelderMead{Optim.AffineSimplexer,Optim.AdaptiveParameters}, ::Optim.Options{Float64,Nothing}) at C:\Users\progr.julia\packages\Optim\3K7JI\src\multivariate\optimize\optimize.jl:35  
[4] optimize(::Function, ::Array{Float64,1}; inplace::Bool, autodiff::Symbol, kwargs::Base.Iterators.Pairs{Union{},Union{},Tuple{},NamedTuple{(),Tuple{}}}) at C:\Users\progr.julia\packages\Optim\3K7JI\src\multivariate\optimize\interface.jl:90  
[5] optimize(::Function, ::Array{Float64,1}) at C:\Users\progr.julia\packages\Optim\3K7JI\src\multivariate\optimize\interface.jl:84  
[6] top-level scope at In[29]:1  
[7] include\_string(::Function, ::Module, ::String, ::String) at .\loading.jl:1091

Any help would be truly appreciated!! 🙂

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [August 2, 2021, 2:01am UTC](https://discourse.julialang.org/t/problem-optimizing-a-2-variable-function-optim-jl/65655/2 "2021-08-02T02:01:33Z")

</div>

notice that `Optim` optimizes functions defined over vectors. An easy way to modify your function would be:

```julia
function distancia3(x)
    α = x[1]
    m = x[2]
    distancia2(α, m)
end

```

If you are worried about performance, you’d want to read the [performance tips](https://docs.julialang.org/en/v1/manual/performance-tips/) in the documentation.  
Finally, perhaps you’d also would want to know about [`ComponentArrays.jl`](https://github.com/jonniedie/ComponentArrays.jl). I’m a big fan of that package.

---

<div class="post-metadata">

**Author:** ![AndresCasillas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrescasillas/32/27133_2.png) [@AndresCasillas](https://discourse.julialang.org/u/AndresCasillas)\
**Post date:** [August 2, 2021, 1:54pm UTC](https://discourse.julialang.org/t/problem-optimizing-a-2-variable-function-optim-jl/65655/3 "2021-08-02T13:54:35Z")

</div>

Thanks a lot! I copied that code and it works perfectly. I’m only worried about the fact that I’m not getting a candidate solution back. An example of an output is the following:

- Status: success

- Candidate solution  
Final objective value: 5.038829e+00

- Found with  
Algorithm: Nelder-Mead

- Convergence measures  
√(Σ(yᵢ-ȳ)²)/n ≤ 1.0e-08

- Work counters  
Seconds run: 1 (vs limit Inf)  
Iterations: 42  
f(x) calls: 83

Why is this?

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [August 2, 2021, 2:02pm UTC](https://discourse.julialang.org/t/problem-optimizing-a-2-variable-function-optim-jl/65655/4 "2021-08-02T14:02:00Z")

</div>

Do you mean the optimal values? Optim returns an object that has those values, you can extract them like this:

```julia
res = optimize(...)
Optim.minimizer(res)

```

[The documentation](https://julianlsolvers.github.io/Optim.jl/stable/#user/minimization/#obtaining-results) lists everything inside the object returned.

---

<div class="post-metadata">

**Author:** ![AndresCasillas](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/andrescasillas/32/27133_2.png) [@AndresCasillas](https://discourse.julialang.org/u/AndresCasillas)\
**Post date:** [August 2, 2021, 2:05pm UTC](https://discourse.julialang.org/t/problem-optimizing-a-2-variable-function-optim-jl/65655/5 "2021-08-02T14:05:50Z")

</div>

Perfect. You are very kind.
