# Non-linear optimization with Ipopt

**URL:** <https://discourse.julialang.org/t/non-linear-optimization-with-ipopt/113772>\
**Category:** Optimization (Mathematical)\
**Created:** [May 3, 2024, 2:27am UTC](https://discourse.julialang.org/t/non-linear-optimization-with-ipopt/113772 "2024-05-03T02:27:56Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![bada5150](https://avatars.discourse-cdn.com/v4/letter/b/df788c/32.png) [@bada5150](https://discourse.julialang.org/u/bada5150)\
**Post date:** [May 3, 2024, 2:27am UTC](https://discourse.julialang.org/t/non-linear-optimization-with-ipopt/113772/1 "2024-05-03T02:27:56Z")

</div>

Hi everyone I am new to Juila and working on non-linear optimization problem with Ipopt solver. Here are my code and my third constraint is 0.0653 \* (2 \* x[2])^-2 \<= 30. However, I am getting [0, 0] and it seems like Ipopt cant solve x^-2. Any insight would be helpful.  
Thank you

```julia
using JuMP, Ipopt

m = Model(Ipopt. Optimizer)

@ variable(m, x[1:2])

@ NLobjective(m, Min, pi*(x[1] + x[2])^2)

@ NLconstraint(m, x[1]>=.254)

@ NLconstraint(m, 0.020<=x[2]<=0.125)

@ NLconstraint(m, 0.0653 * (2* x[2])^-2 <= 30)

JuMP.optimize!(m)

println(" Optimal Objective function value =", JuMP.objective_value(m))

println(" Optimal solution =", JuMP.value.(x))

```

---

<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:** [May 3, 2024, 2:44am UTC](https://discourse.julialang.org/t/non-linear-optimization-with-ipopt/113772/2 "2024-05-03T02:44:38Z")

</div>

Hi @bada5150, welcome to the forum.

There are a few things going on:

You must always check whether the solver found a solution before querying `value`. See [Solutions · JuMP](https://jump.dev/JuMP.jl/stable/manual/solutions/#Check-if-an-optimal-solution-exists). In this case, `solution_summary(m)` would tell you:

```julia
julia> solution_summary(m)
* Solver : Ipopt

* Status
  Result count : 1
  Termination status : INVALID_MODEL
  Message from the solver:
  "Invalid_Number_Detected"

* Candidate solution (result #1)
  Primal status : UNKNOWN_RESULT_STATUS
  Dual status : UNKNOWN_RESULT_STATUS
  Objective value : 0.00000e+00
  Dual objective value : 0.00000e+00

* Work counters
  Solve time (sec) : 1.16706e-03
  Barrier iterations : 0

```

Ipopt found an invalid number because it tried `x[2] = 0` as a solution, which is undefined because of the `x[2]^-2`.

Ipopt tried this value because the default starting point is `x = 0`, and no bounds were specified.

Instead of writing bounds as `@NLconstraint(m, 0.020<=x[2]<=0.125)`, you should instead declare them when creating the variable: `@variable(m, 0.020 <= x <= 0.125)`.

As another minor point, the `@NL` interface is now legacy. You can use the regular `@objective` and `@constraint` macros. See [Nonlinear Modeling · JuMP](https://jump.dev/JuMP.jl/stable/manual/nonlinear/).

Here is how I would write your model:

```julia
using JuMP, Ipopt
model = Model(Ipopt.Optimizer)
l = [0.254, 0.020]
u = [Inf, 0.125]
@variable(model, l[i] <= x[i in 1:2] <= u[i])
@objective(model, Min, pi * (x[1] + x[2])^2)
@constraint(model, 0.0653 * (2 * x[2])^-2 <= 30)
optimize!(model)
@assert is_solved_and_feasible(model)
println("Optimal Objective function value =", objective_value(model))
println("Optimal solution =", value.(x))

```

It returns:

```julia
julia> println("Optimal Objective function value =", objective_value(model))
Optimal Objective function value =0.24162137335214304

julia> println("Optimal solution =", value.(x))
Optimal solution =[0.2539999914381088, 0.02332738162717634]

```
