# Minimizing a numerical function

**URL:** <https://discourse.julialang.org/t/minimizing-a-numerical-function/47139>\
**Category:** General Usage\
**Created:** [September 23, 2020, 3:57pm UTC](https://discourse.julialang.org/t/minimizing-a-numerical-function/47139 "2020-09-23T15:57:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![DShiu](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@DShiu](https://discourse.julialang.org/u/DShiu)\
**Post date:** [September 23, 2020, 3:57pm UTC](https://discourse.julialang.org/t/minimizing-a-numerical-function/47139/1 "2020-09-23T15:57:09Z")

</div>

I am using Optim.jl to minimize a function:

```julia
optimize(δ-> nls_obj(δ, theta2, y, c, d, p0, B), 0, LBFGS(), Optim.Options(g_tol = 1e-2, iterations=100_000, show_trace=true, show_every=50))

```

Given other input values, function nls\_obj maps delta to a real number, but this function does not have an analytical form. When I feed a value of delta into it, the function works as expected. However, the optimization reports error message:

```julia
ERROR: MethodError: no method matching optimize(::var"#382#383", ::Int64, ::LBFGS{Nothing,LineSearches.InitialStatic{Float64},LineSearches.HagerZhang{Float64,Base.RefValue{Bool}},Optim.var"#19#21"}, ::Optim.Options{Float64,Nothing})

```

Is it because I call the optimization in a wrong way?

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [September 23, 2020, 4:18pm UTC](https://discourse.julialang.org/t/minimizing-a-numerical-function/47139/2 "2020-09-23T16:18:56Z")

</div>

I think Optim.jl expects your function to take a vector input. Does it work if you do:

```julia
optimize(δ-> nls_obj(δ[1], theta2, y, c, d, p0, B), [0.0], LBFGS(), Optim.Options(g_tol = 1e-2, iterations=100_000, show_trace=true, show_every=50))

```

or, in other words, if you treat the function being optimized as a function of a vector of length 1?

---

<div class="post-metadata">

**Author:** ![DShiu](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@DShiu](https://discourse.julialang.org/u/DShiu)\
**Post date:** [September 23, 2020, 4:23pm UTC](https://discourse.julialang.org/t/minimizing-a-numerical-function/47139/3 "2020-09-23T16:23:50Z")

</div>

It reports a different error message:

```julia
ERROR: MethodError: no method matching -(::Tuple{Float64,Array{Float64,1}}, ::Tuple{Float64,Array{Float64,1}})

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [September 23, 2020, 4:26pm UTC](https://discourse.julialang.org/t/minimizing-a-numerical-function/47139/4 "2020-09-23T16:26:08Z")

</div>

Ok, seems like an issue with the function you are minimizing, but it’s impossible to say without more information. Please at least post the _full_ stacktrace and ideally a minimal reproducible example. See [Please read: make it easier to help you](https://discourse.julialang.org/t/psa-make-it-easier-to-help-you/14757) for more information about making it easier for us to help you.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 23, 2020, 5:09pm UTC](https://discourse.julialang.org/t/minimizing-a-numerical-function/47139/6 "2020-09-23T17:09:59Z")

</div>

I think that it’s a problem that `nls_obj` returns two outputs (well, actually, it returns a length-2 tuple. How could you find the minimum among tuples?). Try making your anonymous objective function only return the first output (if that is what you need).

---

<div class="post-metadata">

**Author:** ![DShiu](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@DShiu](https://discourse.julialang.org/u/DShiu)\
**Post date:** [September 23, 2020, 5:16pm UTC](https://discourse.julialang.org/t/minimizing-a-numerical-function/47139/7 "2020-09-23T17:16:36Z")

</div>

Good catch! I made a stupid mistake. It works once I make nls\_obs returns only obj and incorporate rdeits’ suggestion.

Thank you!

---

<div class="post-metadata">

**Author:** ![DShiu](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@DShiu](https://discourse.julialang.org/u/DShiu)\
**Post date:** [September 23, 2020, 5:19pm UTC](https://discourse.julialang.org/t/minimizing-a-numerical-function/47139/8 "2020-09-23T17:19:07Z")

</div>

But it seems that the solution differs when I set different start values of delta.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [September 23, 2020, 5:28pm UTC](https://discourse.julialang.org/t/minimizing-a-numerical-function/47139/9 "2020-09-23T17:28:21Z")

</div>

You don’t have to change `nls_obj`, just make the anonymous function capture the first output, like here

```julia
optimize(x->foo(x, a)[1], ...) 

```

Note the `[1]`

---

<div class="post-metadata">

**Author:** ![DShiu](https://avatars.discourse-cdn.com/v4/letter/d/8e8cbc/32.png) [@DShiu](https://discourse.julialang.org/u/DShiu)\
**Post date:** [September 23, 2020, 5:30pm UTC](https://discourse.julialang.org/t/minimizing-a-numerical-function/47139/10 "2020-09-23T17:30:41Z")

</div>

This is a plot for the objective of minimization. Depending on initial value, the solution is either the global minimum or a very negative value which gives a local minimum.  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/9/9/9950cb402565119c6a2ad506dabab3d118dd053a.png)
