# Optim.jl - How to find local maxima of multivariate function

**URL:** <https://discourse.julialang.org/t/optim-jl-how-to-find-local-maxima-of-multivariate-function/36853>\
**Category:** Optimization (Mathematical)\
**Tags:** optimization\
**Created:** [April 1, 2020, 2:39pm UTC](https://discourse.julialang.org/t/optim-jl-how-to-find-local-maxima-of-multivariate-function/36853 "2020-04-01T14:39:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [April 1, 2020, 2:39pm UTC](https://discourse.julialang.org/t/optim-jl-how-to-find-local-maxima-of-multivariate-function/36853/1 "2020-04-01T14:39:00Z")

</div>

Pardon my ignorance (if you’ve seen any recent posts of mine you’ll know I’ve been studying calculus lately) but I’m trying to understand how to find local maxima of a multivariate function with Optim.jl. Given the following function, it’s pretty easy to pick a starting point and let Optim work its magic to find local minima:

```julia
using Optim
using Plots
using Plots.PlotMeasures
pyplot(size=(1200,600))

f(x,y) = -5*x*y*exp(-x^2-y^2)
res = optimize(x -> f(x[1],x[2]), [1.0,1.0])
res2 = optimize(x -> f(x[1],x[2]), [-0.75,-0.75])

# Contour plot with local minima added
p = contour(
    x,
    y,
    z,
    color=:blues,
    legend=false,
    xlabel="x",
    ylabel="y"
)
plot!([Optim.minimizer(res)[1]], [Optim.minimizer(res)[2]], marker=:circle)
plot!([Optim.minimizer(res2)[1]], [Optim.minimizer(res2)[2]], marker=:circle)

```

 ![local_minima](https://global.discourse-cdn.com/julialang/original/3X/d/e/debebca7818fac26b87db1b57a22114d9735e970.png)

In this case, I can obviously just use the same values from `res` and `res2` by switching the sign for the `y` value (`-(Optim.minimizer(res)[2])` but is there a way to have it find local maxima just like it does for local minima? The docs seem to only discuss minimizing functions.

---

<div class="post-metadata">

**Author:** ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)\
**Post date:** [April 1, 2020, 3:23pm UTC](https://discourse.julialang.org/t/optim-jl-how-to-find-local-maxima-of-multivariate-function/36853/2 "2020-04-01T15:23:44Z")

</div>

Multiply the objective be `-1`.

Maximizing `f(x)` is the same as minimizing `-f(x)`.

---

<div class="post-metadata">

**Author:** ![mthelm85](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mthelm85/32/224164_2.png) [@mthelm85](https://discourse.julialang.org/u/mthelm85)\
**Post date:** [April 1, 2020, 4:09pm UTC](https://discourse.julialang.org/t/optim-jl-how-to-find-local-maxima-of-multivariate-function/36853/3 "2020-04-01T16:09:17Z")

</div>

Thanks! I actually did that after realizing myself it was the same. It would be nice to have a keyword argument that allows you to tell it to maximize rather than minimize but maybe there’s a good reason why that doesn’t exist.

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [April 1, 2020, 4:21pm UTC](https://discourse.julialang.org/t/optim-jl-how-to-find-local-maxima-of-multivariate-function/36853/4 "2020-04-01T16:21:56Z")

</div>

> [@mthelm85](#):
>
> It would be nice to have a keyword argument that allows you to tell it to maximize rather than minimize

Instead, I often wish that the function name was `minimize` instead of `optimize`, so that I don’t have to keep looking up the docs (each library does something different).

Then

```julia
maximize(f, args...) = minimize(x -> -f(x), args...)

```

would be a trivial wrapper (of course for AD etc it would be more complicated).
