# Problem with Optim.optimize

**URL:** <https://discourse.julialang.org/t/problem-with-optim-optimize/135368>\
**Category:** General Usage\
**Tags:** question, optim\
**Created:** [January 31, 2026, 8:25am UTC](https://discourse.julialang.org/t/problem-with-optim-optimize/135368 "2026-01-31T08:25:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dnldlg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnldlg/32/209465_2.png) [@dnldlg](https://discourse.julialang.org/u/dnldlg)\
**Post date:** [January 31, 2026, 8:25am UTC](https://discourse.julialang.org/t/problem-with-optim-optimize/135368/1 "2026-01-31T08:25:31Z")

</div>

Consider the minimal example

```julia-auto
using Optim

f(x) = (x[1]-2)^2 + (x[2]-3)^2 + sin(3*x[1] + 1) + cos(5*x[2])

optimize(f, [1., 1.], BFGS())

```

For reasons that I don’t comprehend, I get the following error:

```julia-auto
ERROR: BoundsError: attempt to access Float64 at index [2]
Stacktrace:
  [1] indexed_iterate(I::Float64, i::Int64, state::Nothing)
    @ Base .\tuple.jl:171
  [2] (::LineSearches.var"#ϕdϕ#make_ϕdϕ##0"{Optim.ManifoldObjective{…}, Vector{…}, Vector{…}, Vector{…}})(α::Float64)
    @ LineSearches C:\Users\xyz\.julia\packages\LineSearches\2BCHN\src\LineSearches.jl:32
  [3] (::LineSearches.HagerZhang{…})(ϕ::Function, ϕdϕ::LineSearches.var"#ϕdϕ#make_ϕdϕ##0"{…}, c::Float64, phi_0::Float64, dphi_0::Float64)
    @ LineSearches C:\Users\xyz\.julia\packages\LineSearches\2BCHN\src\hagerzhang.jl:147
  [4] HagerZhang
    @ C:\Users\xyz\.julia\packages\LineSearches\2BCHN\src\hagerzhang.jl:103 [inlined]
  [5] perform_linesearch!(state::Optim.BFGSState{…}, method::Optim.BFGS{…}, d::Optim.ManifoldObjective{…})
    @ Optim C:\Users\xyz\.julia\packages\Optim\7krni\src\utilities\perform_linesearch.jl:58
  [6] update_state!(d::NLSolversBase.OnceDifferentiable{…}, state::Optim.BFGSState{…}, method::Optim.BFGS{…})
    @ Optim C:\Users\xyz\.julia\packages\Optim\7krni\src\multivariate\solvers\first_order\bfgs.jl:143
  [7] optimize(d::NLSolversBase.OnceDifferentiable{…}, initial_x::Vector{…}, method::Optim.BFGS{…}, options::Optim.Options{…}, state::Optim.BFGSState{…})       
    @ Optim C:\Users\xyz\.julia\packages\Optim\7krni\src\multivariate\optimize\optimize.jl:65
  [8] optimize
    @ C:\Users\xyz\.julia\packages\Optim\7krni\src\multivariate\optimize\optimize.jl:43 [inlined]
  [9] #optimize#67
    @ C:\Users\xyz\.julia\packages\Optim\7krni\src\multivariate\optimize\interface.jl:274 [inlined]
 [10] optimize
    @ C:\Users\xyz\.julia\packages\Optim\7krni\src\multivariate\optimize\interface.jl:264 [inlined]
 [11] optimize(f::Function, initial_x::Vector{…}, method::Optim.BFGS{…})
    @ Optim C:\Users\xyz\.julia\packages\Optim\7krni\src\multivariate\optimize\interface.jl:264
 [12] top-level scope
    @ c:\Users\xyz\Desktop\debug.jl:5
Some type information was truncated. Use `show(err)` to see complete types.

```

I’m using Optim v1.13.2

---

<div class="post-metadata">

**Author:** ![rokke](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rokke/32/222691_2.png) [@rokke](https://discourse.julialang.org/u/rokke)\
**Post date:** [January 31, 2026, 10:30am UTC](https://discourse.julialang.org/t/problem-with-optim-optimize/135368/2 "2026-01-31T10:30:33Z")

</div>

are you able to try from a clean julia instance? I can’t reproduce from master

```julia-auto
               _
   _ _ _(_)_ | Documentation: https://docs.julialang.org
  (_) | (_) (_) |
   _ _ _| |_ __ _ | Type "?" for help, "]?" for Pkg help.
  | | | | | | |/ _` | |
  | | |_| | | | (_| | | Version 1.12.4 (2026-01-06)
 _/ |\ __'_|_|_|\__'_| |
|__/ |

julia> using Optim

julia> f(x) = (x[1]-2)^2 + (x[2]-3)^2 + sin(3*x[1] + 1) + cos(5*x[2])
f (generic function with 1 method)

julia> optimize(f, [1., 1.], BFGS())
 * Status: success

 * Candidate solution
    Final objective value: 3.649729e+00

 * Found with
    Algorithm: BFGS

 * Convergence measures
    |x - x'| = 1.10e-06 ≰ 0.0e+00
    |x - x'|/|x'| = 7.98e-07 ≰ 0.0e+00
    |f(x) - f(x')| = 7.52e-12 ≰ 0.0e+00
    |f(x) - f(x')|/|f(x')| = 2.06e-12 ≰ 0.0e+00
    |g(x)| = 5.35e-09 ≤ 1.0e-08

 * Work counters
    Seconds run: 0 (vs limit Inf)
    Iterations: 6
    f(x) calls: 16
    ∇f(x) calls: 16
    ∇f(x)ᵀv calls: 0

julia> versioninfo()
Julia Version 1.12.4
Commit 01a2eadb04* (2026-01-06 16:56 UTC)
Build Info:

    Note: This is an unofficial build, please report bugs to the project
    responsible for this build and not to the Julia project unless you can
    reproduce the issue using official builds available at https://julialang.org

Platform Info:
  OS: Linux (x86_64-linux-gnu)
  CPU: 14 × Intel(R) Core(TM) Ultra 7 165U
  WORD_SIZE: 64
  LLVM: libLLVM-18.1.7 (ORCJIT, alderlake)
  GC: Built with stock GC
Threads: 1 default, 1 interactive, 1 GC (on 14 virtual cores)

```

---

<div class="post-metadata">

**Author:** ![ufechner7](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ufechner7/32/51363_2.png) [@ufechner7](https://discourse.julialang.org/u/ufechner7)\
**Post date:** [January 31, 2026, 1:45pm UTC](https://discourse.julialang.org/t/problem-with-optim-optimize/135368/3 "2026-01-31T13:45:23Z")

</div>

> [@dnldlg](#):
>
> I’m using Optim v1.13.2

The latest version of Optim is v2.0.0.

If you do:

```bash
mkdir test
cd test
julia --project=.

```

and then, in Julia:

```julia-auto
using Pkg
Pkg.add("Optim")
using Optim
f(x) = (x[1]-2)^2 + (x[2]-3)^2 + sin(3*x[1] + 1) + cos(5*x[2])
optimize(f, [1., 1.], BFGS())

```

it works fine.
