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

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

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

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)
1 Like