# Advice on using ModelingToolkit to solve Lagrange Problems

**URL:** https://discourse.julialang.org/t/advice-on-using-modelingtoolkit-to-solve-lagrange-problems/71473
**Category:** Optimization (Mathematical)
**Created:** [November 15, 2021, 1:21am UTC](https://discourse.julialang.org/t/advice-on-using-modelingtoolkit-to-solve-lagrange-problems/71473 "2021-11-15T01:21:09Z")
**Posts on this page:** 10
**Page:** 2

<div class="post-metadata">

### Author: ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)
#### Post date: [November 16, 2021, 12:35am UTC](https://discourse.julialang.org/t/advice-on-using-modelingtoolkit-to-solve-lagrange-problems/71473/21 "2021-11-16T00:35:36Z")

</div>

eqs = [0 ~ 5x-f, 0 ~ -log(x)-f]

should be

eqs = [0 ~ -5x-f, 0 ~ -log(x)-f]

but that creates a problem with complex numbers.

---

<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: [November 16, 2021, 12:41am UTC](https://discourse.julialang.org/t/advice-on-using-modelingtoolkit-to-solve-lagrange-problems/71473/22 "2021-11-16T00:41:25Z")

</div>

There is no solution if it’s `-5x`.

I suggest you step back a bit and come up with the full formulation (on paper) of the problem you are trying to solve. In addition, take a look at the documentation for JuMP and Modeling Toolkit. There are lots of examples and tutorials.

If there specific questions with syntax, people may be able to help. But it’s hard to provide general advice about how to plot a solution or interact with data.

---

<div class="post-metadata">

### Author: ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)
#### Post date: [November 16, 2021, 12:44am UTC](https://discourse.julialang.org/t/advice-on-using-modelingtoolkit-to-solve-lagrange-problems/71473/23 "2021-11-16T00:44:44Z")

</div>

> [@odow](#):
>
> tead do is write a function that takes as input the prices, formulates and solves the problem, and then returns the `x` and `y` solutions.

OK, that is the problem. I might get an intermediate econ textbook, to come up with a better example.

---

<div class="post-metadata">

### Author: ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)
#### Post date: [November 16, 2021, 2:56am UTC](https://discourse.julialang.org/t/advice-on-using-modelingtoolkit-to-solve-lagrange-problems/71473/24 "2021-11-16T02:56:22Z")

</div>

here are my two equations

```julia
eqs = [u ~ 16log(x_1)+9log(x_2), 100 ~ (x_1^2/100)+(x_2^2/36)]

```

the variables are u x\_1 and x\_2, and i want to maximize u which is a function u(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: [November 16, 2021, 3:56am UTC](https://discourse.julialang.org/t/advice-on-using-modelingtoolkit-to-solve-lagrange-problems/71473/25 "2021-11-16T03:56:42Z")

</div>

```nohighlight
julia> using JuMP, Ipopt

julia> function solve()
           model = Model(Ipopt.Optimizer)
           @variable(model, x[1:2] >= 0.001, start = 1)
           @NLobjective(model, Max, 16 * log(x[1]) + 9 * log(x[2]))
           @NLconstraint(model, x[1]^2 / 100 + x[2]^2 / 36 == 100)
           optimize!(model)
           @assert termination_status(model) == LOCALLY_SOLVED
           return value.(x)
       end
solve (generic function with 1 method)

julia> solve()
This is Ipopt version 3.13.4, running with linear solver mumps.
NOTE: Other linear solvers might be more efficient (see Ipopt documentation).

Number of nonzeros in equality constraint Jacobian...: 2
Number of nonzeros in inequality constraint Jacobian.: 0
Number of nonzeros in Lagrangian Hessian.............: 4

Total number of variables............................: 2
                     variables with only lower bounds: 2
                variables with lower and upper bounds: 0
                     variables with only upper bounds: 0
Total number of equality constraints.................: 1
Total number of inequality constraints...............: 0
        inequality constraints with only lower bounds: 0
   inequality constraints with lower and upper bounds: 0
        inequality constraints with only upper bounds: 0

iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls
   0 0.0000000e+00 1.00e+02 1.19e+01 -1.0 0.00e+00 - 0.00e+00 0.00e+00 0
   1 1.6922650e+02 7.28e+04 6.03e+07 -1.0 1.58e+03 - 6.27e-04 1.00e+00f 1
   2 1.5946715e+02 1.86e+04 3.79e+08 -1.0 8.15e+02 6.0 1.00e+00 1.00e+00h 1
   3 1.4227187e+02 4.62e+03 9.46e+07 -1.0 3.78e+02 - 1.00e+00 1.00e+00h 1
   4 1.2546702e+02 1.13e+03 2.36e+07 -1.0 1.87e+02 - 1.00e+00 1.00e+00h 1
   5 1.1009062e+02 2.60e+02 5.87e+06 -1.0 8.97e+01 - 1.00e+00 1.00e+00h 1
   6 9.8892524e+01 4.69e+01 1.35e+06 -1.0 3.81e+01 - 1.00e+00 1.00e+00h 1
   7 9.4544237e+01 3.74e+00 1.82e+05 -1.0 1.08e+01 - 1.00e+00 1.00e+00h 1
   8 9.4089100e+01 3.38e-02 3.22e+03 -1.0 1.02e+00 - 1.00e+00 1.00e+00h 1
   9 9.4091188e+01 5.90e-06 7.70e-01 -1.0 1.33e-02 - 1.00e+00 1.00e+00h 1
iter objective inf_pr inf_du lg(mu) ||d|| lg(rg) alpha_du alpha_pr ls
  10 1.0032943e+02 5.03e+00 1.77e-01 -1.7 2.08e+01 - 7.29e-01 1.00e+00f 1
  11 1.0359036e+02 1.03e+01 2.84e-02 -1.7 2.50e+01 - 1.00e+00 1.00e+00f 1
  12 1.0239742e+02 2.68e-01 4.61e-03 -1.7 2.93e+00 - 1.00e+00 1.00e+00h 1
  13 1.0236419e+02 7.69e-04 1.14e-05 -1.7 2.52e-01 - 1.00e+00 1.00e+00h 1
  14 1.0236410e+02 6.26e-06 1.01e-07 -3.8 1.47e-02 - 1.00e+00 1.00e+00h 1
  15 1.0236410e+02 4.59e-10 2.27e-11 -5.7 1.26e-04 - 1.00e+00 1.00e+00h 1
  16 1.0236410e+02 5.68e-14 2.56e-14 -8.6 1.29e-06 - 1.00e+00 1.00e+00h 1

Number of Iterations....: 16

                                   (scaled) (unscaled)
Objective...............: -1.0236409660088709e+02 1.0236409660088709e+02
Dual infeasibility......: 2.5600289097858465e-14 2.5600289097858465e-14
Constraint violation....: 5.6843418860808015e-14 5.6843418860808015e-14
Complementarity.........: 2.5059564600335962e-09 -2.5059564600335962e-09
Overall NLP error.......: 2.5059564600335962e-09 5.6843418860808015e-14

Number of objective function evaluations = 17
Number of objective gradient evaluations = 17
Number of equality constraint evaluations = 17
Number of inequality constraint evaluations = 0
Number of equality constraint Jacobian evaluations = 17
Number of inequality constraint Jacobian evaluations = 0
Number of Lagrangian Hessian evaluations = 16
Total CPU secs in IPOPT (w/o function evaluations) = 0.009
Total CPU secs in NLP function evaluations = 0.000

EXIT: Optimal Solution Found.
2-element Vector{Float64}:
 79.99999999824524
 36.00000000140383

```

---

<div class="post-metadata">

### Author: ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)
#### Post date: [November 16, 2021, 5:56pm UTC](https://discourse.julialang.org/t/advice-on-using-modelingtoolkit-to-solve-lagrange-problems/71473/26 "2021-11-16T17:56:02Z")

</div>

@ChrisRackauckas I got JuMP to work but MTK is a mess, I need to have 3 equations for 3 variables, and I’m not sure if there is a way to specify which equation is maximized.

---

<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: [November 16, 2021, 6:19pm UTC](https://discourse.julialang.org/t/advice-on-using-modelingtoolkit-to-solve-lagrange-problems/71473/27 "2021-11-16T18:19:02Z")

</div>

> but MTK is a mess

That’s a little uncharitable. JuMP and MTK serve different use-cases. If you’re doing constrained nonlinear optimization and JuMP works, use JuMP.

---

<div class="post-metadata">

### Author: ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)
#### Post date: [November 16, 2021, 6:32pm UTC](https://discourse.julialang.org/t/advice-on-using-modelingtoolkit-to-solve-lagrange-problems/71473/28 "2021-11-16T18:32:59Z")

</div>

I meant my code in MTK. I didn’t mean to imply a problem with MTK.

---

<div class="post-metadata">

### Author: ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)
#### Post date: [November 16, 2021, 7:32pm UTC](https://discourse.julialang.org/t/advice-on-using-modelingtoolkit-to-solve-lagrange-problems/71473/29 "2021-11-16T19:32:08Z")

</div>

Is there a way to plot this?

---

<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: [November 16, 2021, 7:41pm UTC](https://discourse.julialang.org/t/advice-on-using-modelingtoolkit-to-solve-lagrange-problems/71473/30 "2021-11-16T19:41:19Z")

</div>

What do you mean plot? The solution is a vector with two elements? It’s up to you how you plot the solution:

[https://github.com/JuliaPlots/Plots.jl](https://github.com/JuliaPlots/Plots.jl)

[Previous page](https://discourse.julialang.org/t/advice-on-using-modelingtoolkit-to-solve-lagrange-problems/71473.md?page=1)
