# Nonlinear optimization in JuMP adding the analytic gradient expression

**URL:** https://discourse.julialang.org/t/nonlinear-optimization-in-jump-adding-the-analytic-gradient-expression/96867
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [March 31, 2023, 12:26am UTC](https://discourse.julialang.org/t/nonlinear-optimization-in-jump-adding-the-analytic-gradient-expression/96867 "2023-03-31T00:26:38Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![mb96](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mb96/32/45786_2.png) [@mb96](https://discourse.julialang.org/u/mb96)
#### Post date: [March 31, 2023, 12:26am UTC](https://discourse.julialang.org/t/nonlinear-optimization-in-jump-adding-the-analytic-gradient-expression/96867/1 "2023-03-31T00:26:38Z")

</div>

Hi, I am trying to solve a highly dimensional nonlinear optimization problem for which I have a function that maps a vector into a value and a its jacobian. I want to solve the problem providing the gradient but I can not find good resources that explain how to do so using the Optim package. Can anyone point to resources or provide a very simple example illustrating how to set up the problem?

Thanks in advance,  
Miguel.

---

<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: [March 31, 2023, 1:51am UTC](https://discourse.julialang.org/t/nonlinear-optimization-in-jump-adding-the-analytic-gradient-expression/96867/2 "2023-03-31T01:51:41Z")

</div>

Do you have only a single function to optimize? Are there other constraints? Integrality or bounds on the variables?

You can register a user-defined function in JuMP and provide the analytic gradient: [Nonlinear Modeling · JuMP](https://jump.dev/JuMP.jl/stable/manual/nlp/#Multivariate-functions)

---

<div class="post-metadata">

### Author: ![mb96](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mb96/32/45786_2.png) [@mb96](https://discourse.julialang.org/u/mb96)
#### Post date: [March 31, 2023, 2:58pm UTC](https://discourse.julialang.org/t/nonlinear-optimization-in-jump-adding-the-analytic-gradient-expression/96867/3 "2023-03-31T14:58:17Z")

</div>

Thanks!

I am trying to adapt the example in the documentation so that it takes in a vector instead its elements explicitly because in my problem I am solving a 15 dimensional problem. However, when I do this it assumes it is univariate function. I illustrate this below:

 ![Captura de Pantalla 2023-03-31 a la(s) 10.57.11](https://global.discourse-cdn.com/julialang/original/3X/4/3/431493c0fc76a26d571b4cafaf2cba240975b52a.png)

When I call the function I run into this error:

 ![Captura de Pantalla 2023-03-31 a la(s) 10.57.19](https://global.discourse-cdn.com/julialang/original/3X/3/6/3612393eb2b621e9b79cee5226f03966625f6f29.png)

Any ideas on how to fix this?

Thanks a lot!

---

<div class="post-metadata">

### Author: ![Adedayo\_Yusuff](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/adedayo_yusuff/32/47595_2.png) [@Adedayo\_Yusuff](https://discourse.julialang.org/u/Adedayo_Yusuff)
#### Post date: [April 9, 2023, 4:15am UTC](https://discourse.julialang.org/t/nonlinear-optimization-in-jump-adding-the-analytic-gradient-expression/96867/4 "2023-04-09T04:15:54Z")

</div>

I think you should use splat f(x…).

There is an example on this in [Nonlinear Modeling · JuMP](https://jump.dev/JuMP.jl/stable/manual/nlp/#Multivariate-functions).

```julia
f(x...) = (1 - x[1])^2 + 100 * (x[2] - x[1]^2)^2
function ∇f(g, x...)
    g[1] = 400 * x[1]^3 - 400 * x[1] * x[2] + 2 * x[1] - 2
    g[2] = 200 * (x[2] - x[1]^2)
    return
end
function ∇²f(H, x...)
    H[1, 1] = 1200 * x[1]^2 - 400 * x[2] + 2
    # H[1, 2] = -400 * x[1] <-- Not needed. Fill the lower-triangular only.
    H[2, 1] = -400 * x[1]
    H[2, 2] = 200.0
    return
end

model = Model()
register(model, :rosenbrock, 2, f, ∇f, ∇²f)
@variable(model, x[1:2])
@NLobjective(model, Min, rosenbrock(x[1], x[2]))

```

---

<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 9, 2023, 8:50pm UTC](https://discourse.julialang.org/t/nonlinear-optimization-in-jump-adding-the-analytic-gradient-expression/96867/5 "2023-04-09T20:50:04Z")

</div>

Ooops. I never replied to this.

@Adedayo_Yusuff is correct. You need to use the splatted syntax. You cannot pass a `Vector` as a single argument.
