# Passing objective function into JuMP

**URL:** <https://discourse.julialang.org/t/passing-objective-function-into-jump/57357>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [March 17, 2021, 3:30am UTC](https://discourse.julialang.org/t/passing-objective-function-into-jump/57357 "2021-03-17T03:30:45Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![samerb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samerb/32/9242_2.png) [@samerb](https://discourse.julialang.org/u/samerb)\
**Post date:** [March 17, 2021, 3:30am UTC](https://discourse.julialang.org/t/passing-objective-function-into-jump/57357/1 "2021-03-17T03:30:45Z")

</div>

I would like to use JuMP for nonlinear constrained optimization. Specifically I want to be able to pass my objective function into it from outside. How do I do that? What I tried is below.

I am not sure if this is a JuMP-specific thing or just a Julia passing functions thing. Thanks

```julia
f(x) = x^2 + 3x + 4

using JuMP
import Ipopt
function test_obj(obj_func)
    model = Model(Ipopt.Optimizer)
    @variable(model, x, start = -1.0)
    @NLobjective(model, Min, obj_func)
    @NLconstraint(model, x >= 5)

    
    optimize!(model)
    println("""
    termination_status = $(termination_status(model))
    \n x = $(value(x))    
    """)
    return
end

test_obj(f)

> Unexpected object #22 (of type var"#22#23"{typeof(f)} in nonlinear expression.

```

Based on [this](https://discourse.julialang.org/t/passing-a-function-as-an-objective-function-in-jump/10745/2) question I also tried:

```julia
    model = Model(Ipopt.Optimizer)
    @variable(model, x, start = -1.0)
    @NLobjective(model, Min, f(x))
    

    # @NLconstraint(model, sqrt(y-π) - b_1 >= sqrt(y-π-x_2) )
# @NLconstraint(model, π == p_2*(c-x_2) )
    optimize!(model)
> Unrecognized function "f" used in nonlinear expression.

```

---

<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 17, 2021, 4:29am UTC](https://discourse.julialang.org/t/passing-objective-function-into-jump/57357/2 "2021-03-17T04:29:19Z")

</div>

You need to register the function [https://jump.dev/JuMP.jl/stable/nlp/#User-defined-Functions](https://jump.dev/JuMP.jl/stable/nlp/#User-defined-Functions)
