# How to fix this error when I use JUMP to maximize a function?

**URL:** <https://discourse.julialang.org/t/how-to-fix-this-error-when-i-use-jump-to-maximize-a-function/98155>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump, optimization\
**Created:** [May 1, 2023, 9:15am UTC](https://discourse.julialang.org/t/how-to-fix-this-error-when-i-use-jump-to-maximize-a-function/98155 "2023-05-01T09:15:10Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![maoc](https://avatars.discourse-cdn.com/v4/letter/m/58956e/32.png) [@maoc](https://discourse.julialang.org/u/maoc)\
**Post date:** [May 1, 2023, 9:15am UTC](https://discourse.julialang.org/t/how-to-fix-this-error-when-i-use-jump-to-maximize-a-function/98155/1 "2023-05-01T09:15:10Z")

</div>

I’m trying to optimize a likelihood function a little different from the traditional ones. I’m going to call this likelihood function of mine “faux”. To show my problem, first see how well this example (I took this example from the Jump tutorial) works:

```julia
using Distributions
using JuMP, Ipopt

n = 1_000
data = randn(n)
model = Model(Ipopt.Optimizer)
set_silent(model)

@variable(model, μ, start = 0.0)
@variable(model, σ >= 0.0, start = 1.0)
@NLobjective( model , Max, n / 2 * log(1 / (2 * π * σ^2)) -sum((data[i] - μ)^2 for i in 1:n) / (2 * σ^2) )
optimize!(model)   
# declare solution
@show value(μ)
@show value(σ)

```

As I said, my function is a little more complicated to the point of writing it directly like the example I’m giving. So, as I have faux built out and want to maximize it using the same script as above. For this, assume that

```julia
faux(μ,σ) = n / 2 * log(1 / (2 * π * σ^2)) -sum((data[i] - μ)^2 for i in 1:n) / (2 * σ^2)

```

But, when I do this

```julia
n = 1_000
data = randn(n)
model = Model(Ipopt.Optimizer)
set_silent(model)

@variable(model, μ, start = 0.0)
@variable(model, σ >= 0.0, start = 1.0)
@NLobjective( model , Max, faux(μ,σ) )
optimize!(model)   

```

I have an error: Unrecognized function “faux” used in nonlinear expression.  
Why is this happening?  
Any solution?

---

<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 1, 2023, 9:53am UTC](https://discourse.julialang.org/t/how-to-fix-this-error-when-i-use-jump-to-maximize-a-function/98155/2 "2023-05-01T09:53:57Z")

</div>

What version of JuMP are you using? You should have received the warning:

````julia
julia> faux(μ,σ) = n / 2 * log(1 / (2 * π * σ^2)) -sum((data[i] - μ)^2 for i in 1:n) / (2 * σ^2)
faux (generic function with 1 method)

julia> @NLobjective( model , Max, faux(μ,σ) )
┌ Warning: Function faux automatically registered with 2 arguments.
│ 
│ Calling the function with a different number of arguments will result in an
│ error.
│ 
│ While you can safely ignore this warning, we recommend that you manually
│ register the function as follows:
│ ```Julia
│ model = Model()
│ register(model, :faux, 2, faux; autodiff = true)
│ ```
└ @ MathOptInterface.Nonlinear ~/.julia/packages/MathOptInterface/RbvtA/src/Nonlinear/operators.jl:430

````

You need to use a registered function [Nonlinear Modeling · JuMP](https://jump.dev/JuMP.jl/stable/manual/nlp/#User-defined-Functions)

```julia
using JuMP, Ipopt, Distributions
n = 1_000
data = randn(n)
model = Model(Ipopt.Optimizer)
set_silent(model)
@variable(model, μ, start = 0.0)
@variable(model, σ >= 0.0, start = 1.0)
faux(μ,σ) = n / 2 * log(1 / (2 * π * σ^2)) -sum((data[i] - μ)^2 for i in 1:n) / (2 * σ^2)
register(model, :faux, 2, faux; autodiff = true)
@NLobjective(model , Max, faux(μ,σ) )
optimize!(model)

```
