# How to set a Non-linear function with double summatory

**URL:** https://discourse.julialang.org/t/how-to-set-a-non-linear-function-with-double-summatory/105542
**Category:** Optimization (Mathematical)
**Tags:** jump, ipopt, nonlinear-optimizati
**Created:** [October 29, 2023, 4:39pm UTC](https://discourse.julialang.org/t/how-to-set-a-non-linear-function-with-double-summatory/105542 "2023-10-29T16:39:26Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![PatrickeTownsend](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/patricketownsend/32/50028_2.png) [@PatrickeTownsend](https://discourse.julialang.org/u/PatrickeTownsend)
#### Post date: [October 29, 2023, 4:39pm UTC](https://discourse.julialang.org/t/how-to-set-a-non-linear-function-with-double-summatory/105542/1 "2023-10-29T16:39:26Z")

</div>

Hello Again!  
I’m trying to solve this optimization problem:  
 ![image](https://global.discourse-cdn.com/julialang/original/3X/6/f/6f20133ac2bbe38df587db8d79599e5469b1b8de.png)  
But i’m not really sure about how to write down that objective function, I tried doing this:

```julia
using JuMP
using Ipopt

model = Model(Ipopt.Optimizer)
set_attribute(model, "print_level",4)
set_optimizer_attribute(model, "max_iter",20000)
set_attribute(model, "print_timing_statistics","yes")

n = 32 #number of charges

@variables(model, begin
    -1 ≤ x[1:n] ≤ 1
    -1 ≤ y[1:n] ≤ 1
    -1 ≤ z[1:n] ≤ 1
end)

@NLobjective(model, Min, sum(sum(1/sqrt((x[i] - x[j])^2 + (y[i] - [j])^2 + (z[i] - z[j])^2) for j=i+1:n) for i=1:n-1 ))
optimize!(model)

```

It’s slightly different since I’m not using spherical coordinates, but what really matters is how to set the objective function with the double summatory.  
This will be used for generating 32 equidistant points over an unitary sphere surface.  
How do you all recommend me to solve this optimization problem with JuMP ?

---

<div class="post-metadata">

### Author: ![abelsiqueira](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abelsiqueira/32/47269_2.png) [@abelsiqueira](https://discourse.julialang.org/u/abelsiqueira)
#### Post date: [October 29, 2023, 6:45pm UTC](https://discourse.julialang.org/t/how-to-set-a-non-linear-function-with-double-summatory/105542/2 "2023-10-29T18:45:29Z")

</div>

Looks like you missed a `y`: `y[i] - [j]`. Otherwise, I think your code should work. You could probably drop `@NLobjective` and use `@objective` directly if you’re on JuMP 1.15.

Also, you could probably write with a single sum:

```julia
d(x, y, z, i, j) = 1 / sqrt(...)
@objective(model, Min, sum(d(x, y, z, i, j) for i = 1:n, j = 1:n if j > i))

```

---

<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: [October 29, 2023, 8:40pm UTC](https://discourse.julialang.org/t/how-to-set-a-non-linear-function-with-double-summatory/105542/3 "2023-10-29T20:40:15Z")

</div>

You also need to be careful, because when `x[i] == x[j]` (e.g., if all variables are `0`), then the objective is `1 / sqrt(0)` which is undefined. You need to ensure that the objective value is always defined, so maybe use something `1 / sqrt(0.001 + ...)`?

---

<div class="post-metadata">

### Author: ![PatrickeTownsend](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/patricketownsend/32/50028_2.png) [@PatrickeTownsend](https://discourse.julialang.org/u/PatrickeTownsend)
#### Post date: [October 29, 2023, 9:17pm UTC](https://discourse.julialang.org/t/how-to-set-a-non-linear-function-with-double-summatory/105542/4 "2023-10-29T21:17:53Z")

</div>

It worked, thank you very much

---

<div class="post-metadata">

### Author: ![PatrickeTownsend](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/patricketownsend/32/50028_2.png) [@PatrickeTownsend](https://discourse.julialang.org/u/PatrickeTownsend)
#### Post date: [October 29, 2023, 9:18pm UTC](https://discourse.julialang.org/t/how-to-set-a-non-linear-function-with-double-summatory/105542/5 "2023-10-29T21:18:54Z")

</div>

I’ll check it, thank you Odow
