# How to maximize a scalar function of a scalar argument?

**URL:** <https://discourse.julialang.org/t/how-to-maximize-a-scalar-function-of-a-scalar-argument/115318>\
**Category:** Optimization (Mathematical)\
**Tags:** question, optimization, maxima, constraint\
**Created:** [June 7, 2024, 10:19am UTC](https://discourse.julialang.org/t/how-to-maximize-a-scalar-function-of-a-scalar-argument/115318 "2024-06-07T10:19:52Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![homocomputeris](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/homocomputeris/32/8933_2.png) [@homocomputeris](https://discourse.julialang.org/u/homocomputeris)\
**Post date:** [June 7, 2024, 10:19am UTC](https://discourse.julialang.org/t/how-to-maximize-a-scalar-function-of-a-scalar-argument/115318/1 "2024-06-07T10:19:52Z")

</div>

I want to maximize a scala function, say

```wolfram
NMaximize[{1/(x - 1), -10 < x < 0}, {x}]

```

I tried:

```julia
module test
using Optimization, OptimizationOptimJL, NonlinearSolve

function f(x, p)
    return 1 / (x - 1)
end

u0 = -0.5
p = nothing

prob = OptimizationProblem(f, u0, p, lb=-10, ub=0, sense = Optimization.MaxSense)
@show sol = solve(prob, NelderMead())

end

```

but it returns

```julia
ERROR: MethodError: no method matching fill!(::Float64, ::Float64)

Closest candidates are:
  fill!(::OffsetArrays.OffsetArray, ::Any)
   @ OffsetArrays ~/.julia/packages/OffsetArrays/E3eDu/src/OffsetArrays.jl:408
  fill!(::Array{T}, ::Any) where T
   @ Base array.jl:392
  fill!(::SA, ::Any) where SA<:StaticArraysCore.StaticArray
   @ StaticArrays ~/.julia/packages/StaticArrays/85pEu/src/arraymath.jl:139
  ...

```

How do I maximize this function?

---

<div class="post-metadata">

**Author:** ![gdalle](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gdalle/32/27854_2.png) [@gdalle](https://discourse.julialang.org/u/gdalle)\
**Post date:** [June 7, 2024, 12:24pm UTC](https://discourse.julialang.org/t/how-to-maximize-a-scalar-function-of-a-scalar-argument/115318/2 "2024-06-07T12:24:53Z")

</div>

Optimization packages are generally designed to work with arrays, not numbers. A generic solution would be to use 1-element arrays in most places, ideally with StaticArrays.jl for optimal perfomance.
