# Optim.jl optimize 2 variable function with initial boundaries

**URL:** https://discourse.julialang.org/t/optim-jl-optimize-2-variable-function-with-initial-boundaries/75497
**Category:** Optimization (Mathematical)
**Created:** [January 31, 2022, 11:40am UTC](https://discourse.julialang.org/t/optim-jl-optimize-2-variable-function-with-initial-boundaries/75497 "2022-01-31T11:40:52Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![Iulian.Cioarca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iulian.cioarca/32/30166_2.png) [@Iulian.Cioarca](https://discourse.julialang.org/u/Iulian.Cioarca)
#### Post date: [January 31, 2022, 11:40am UTC](https://discourse.julialang.org/t/optim-jl-optimize-2-variable-function-with-initial-boundaries/75497/1 "2022-01-31T11:40:52Z")

</div>

I’m struggling to accomplish a basic task with Optim.  
Let’s say I defined a function `f(a,x,y) = a + x^2 + y^2`. I want to minimize this function given initial boundaries: `x0 = [-10, 10]`, `y0 = [-10, 10]` and `a = 10, as constant`.

---

<div class="post-metadata">

### Author: ![lostella](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lostella/32/356_2.png) [@lostella](https://discourse.julialang.org/u/lostella)
#### Post date: [January 31, 2022, 12:09pm UTC](https://discourse.julialang.org/t/optim-jl-optimize-2-variable-function-with-initial-boundaries/75497/2 "2022-01-31T12:09:32Z")

</div>

What do you mean by “initial boundary”? Are you looking for the minimum value of `f` within those bounds?

---

<div class="post-metadata">

### Author: ![Iulian.Cioarca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iulian.cioarca/32/30166_2.png) [@Iulian.Cioarca](https://discourse.julialang.org/u/Iulian.Cioarca)
#### Post date: [January 31, 2022, 12:15pm UTC](https://discourse.julialang.org/t/optim-jl-optimize-2-variable-function-with-initial-boundaries/75497/3 "2022-01-31T12:15:13Z")

</div>

Yes. I want the optimizer to find the minimum value of `f` for `x, y` within the bounds, as well as return the corresponding `x, y` values.

---

<div class="post-metadata">

### Author: ![cvanaret](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cvanaret/32/11594_2.png) [@cvanaret](https://discourse.julialang.org/u/cvanaret)
#### Post date: [January 31, 2022, 3:51pm UTC](https://discourse.julialang.org/t/optim-jl-optimize-2-variable-function-with-initial-boundaries/75497/4 "2022-01-31T15:51:58Z")

</div>

Have you tried [this](https://julianlsolvers.github.io/Optim.jl/stable/#user/minimization/#box-constrained-optimization)?

---

<div class="post-metadata">

### Author: ![lostella](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lostella/32/356_2.png) [@lostella](https://discourse.julialang.org/u/lostella)
#### Post date: [February 1, 2022, 7:38pm UTC](https://discourse.julialang.org/t/optim-jl-optimize-2-variable-function-with-initial-boundaries/75497/5 "2022-02-01T19:38:50Z")

</div>

You can also use ProximalAlgorithms, see [this example](https://juliafirstorder.github.io/ProximalAlgorithms.jl/dev/guide/getting_started/#box_qp).

**Note** that the linked documentation refers to the `master` branch (a new release will be coming soon, which will then be covered by the documentation).

For fast convergence to high accuracy you can replace the `ffb` algorithm from the example with something like

```julia
ProximalAlgorithms.PANOC(verbose=true)

```

([here](https://juliafirstorder.github.io/ProximalAlgorithms.jl/dev/guide/implemented_algorithms/#ProximalAlgorithms.PANOC) is the relevant documentation for the algorithm)

---

<div class="post-metadata">

### Author: ![Iulian.Cioarca](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/iulian.cioarca/32/30166_2.png) [@Iulian.Cioarca](https://discourse.julialang.org/u/Iulian.Cioarca)
#### Post date: [February 2, 2022, 10:20am UTC](https://discourse.julialang.org/t/optim-jl-optimize-2-variable-function-with-initial-boundaries/75497/6 "2022-02-02T10:20:42Z")

</div>

Thank you, but the docs don’t specify exactly how to handle multiple variables.

---

<div class="post-metadata">

### Author: ![lostella](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lostella/32/356_2.png) [@lostella](https://discourse.julialang.org/u/lostella)
#### Post date: [February 2, 2022, 1:16pm UTC](https://discourse.julialang.org/t/optim-jl-optimize-2-variable-function-with-initial-boundaries/75497/7 "2022-02-02T13:16:32Z")

</div>

You can use a vector of length 2 as variable: in your example, you’ll be optimizing `f(x) = x[1]^2 + x[2]^2`.
