# Optimization help: porting fmincon() to Julia

**URL:** https://discourse.julialang.org/t/optimization-help-porting-fmincon-to-julia/60739
**Category:** Modelling & Simulations
**Tags:** optim
**Created:** [May 7, 2021, 6:14pm UTC](https://discourse.julialang.org/t/optimization-help-porting-fmincon-to-julia/60739 "2021-05-07T18:14:14Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![longemen3000](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/longemen3000/32/7298_2.png) [@longemen3000](https://discourse.julialang.org/u/longemen3000)
#### Post date: [May 8, 2021, 3:56am UTC](https://discourse.julialang.org/t/optimization-help-porting-fmincon-to-julia/60739/2 "2021-05-08T03:56:44Z")

</div>

Hi, maybe this can help:

> [@Optim: gradient and Hessian of constraints](https://discourse.julialang.org/t/optim-gradient-and-hessian-of-constraints/31493):
>
> Hello, I am using Optim.jl to solve a constrained optimization problem. The gradient is not specified, so finite differences are the default. This works nicely for the objective, but not for the constraints. Thank you! Here ist my mwe: using Optim, Test function fun(x) # objective (1.0 - x[1])^2 + 100.0 \* (x[2] - x[1]^2)^2 + (x[3]-x[1])^2 end function con\_c!(c, x) c[1]= x[1]^2 + x[2]^2 # 1st constraint c[2]= x[2]\* sin(x[1])-x[1] # 2nd constraint c end function c…

Basically:

- cons is a function that,given a vector of variables,gives a vector of constraints (inplace)
- con\_Jacobian is the Jacobian of cons
- con\_hessian is a little different,it basically is an inplace adding of the Lagrange multipliers

Also, there is AD implemented for constraints, you can try using:  
`TwiceDifferentiableConstraints(c!, lx,ux,lc,uc,autodiff=:forward)` (the documentation seems to lack behind, as the support of AD in constraints was in NLSolversBase.jl, not in Optim)

---

_[View the full topic](https://discourse.julialang.org/t/optimization-help-porting-fmincon-to-julia/60739)._
