# Optim.jl single iteration

**URL:** <https://discourse.julialang.org/t/optim-jl-single-iteration/78619>\
**Category:** Optimization (Mathematical)\
**Tags:** question, optim\
**Created:** [March 28, 2022, 3:05pm UTC](https://discourse.julialang.org/t/optim-jl-single-iteration/78619 "2022-03-28T15:05:21Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![vmmhep](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vmmhep/32/34987_2.png) [@vmmhep](https://discourse.julialang.org/u/vmmhep)\
**Post date:** [March 28, 2022, 3:05pm UTC](https://discourse.julialang.org/t/optim-jl-single-iteration/78619/1 "2022-03-28T15:05:22Z")

</div>

I can’t seem to find this anywhere and I’m sorry in advance if my trivial question is indeed elsewhere…

I have a differentiable periodic function:

```julia
function fitness(x::Vector)
    LV = ones(105)
    QV = ones(105,10)
    Qx = zeros(size(QV,1))
    V = dot(LV,(1 .- cos.(LinearAlgebra.mul!(Qx,QV,x))))
    return V
end

```

with gradient

```julia
function g!(x::Vector, storage::Vector)
    LV = ones(105)
    QV = ones(105,10)
    Qx = zeros(size(QV,1))
    storage::Vector = vcat([dot(LV,QV[:,i] .* sin.(LinearAlgebra.mul!(Qx,QV,x))) for i ∈ 1:size(QV,2)]...)
end

```

and running

```julia
res = optimize(fitness,g!,zeros(10) .+0.01,method = LBFGS())

```

gives a single iteration and then `Optim.minimizer(res)` just spits out a 10-element vector with `0.01` as every entry. What am I doing wrong here? My actual problem is more complicated than this (the LV and QV have the shapes as above, but the values of LV have huge hierarchies – I initially thought this was the issue, but it seems the optimization doesn’t run even for this MWE – and the QV are made up of integers in -10:10). Would be really grateful if someone could point me in the right direction here!

PS I tried to include

```julia
function h!(x::Vector, storage::Matrix)
    LV = ones(105)
    QV = ones(105,10)
    Qx = zeros(size(QV,1))
    storage::Matrix = hcat([[dot(LV,QV[:,i] .* QV[:,j] .* cos.(LinearAlgebra.mul!(Qx,QV,x))) for i ∈ 1:size(QV,2)] for j ∈ size(QV,2)]...)
end

```

and use `method = Newton()` but that threw the error

```julia
no method matching h!(::Matrix{Float64}, ::Vector{Float64}

```

and I don’t really understand why! Thanks in advance 🙂

---

<div class="post-metadata">

**Author:** ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)\
**Post date:** [March 28, 2022, 4:49pm UTC](https://discourse.julialang.org/t/optim-jl-single-iteration/78619/2 "2022-03-28T16:49:40Z")

</div>

Your arguments to `h!` are backwards, no?

---

<div class="post-metadata">

**Author:** ![baggepinnen](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/baggepinnen/32/693_2.png) [@baggepinnen](https://discourse.julialang.org/u/baggepinnen)\
**Post date:** [March 28, 2022, 5:40pm UTC](https://discourse.julialang.org/t/optim-jl-single-iteration/78619/3 "2022-03-28T17:40:40Z")

</div>

> [@vmmhep](#):
>
> `storage::Vector = vcat([dot(LV,QV[:,i] .*`

Note that this does not update the variable storage like it’s supposed to, it rather creates a new variable named storage. Try using `.=` to instead update the existing variable storage.

---

<div class="post-metadata">

**Author:** ![vmmhep](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vmmhep/32/34987_2.png) [@vmmhep](https://discourse.julialang.org/u/vmmhep)\
**Post date:** [March 28, 2022, 5:54pm UTC](https://discourse.julialang.org/t/optim-jl-single-iteration/78619/4 "2022-03-28T17:54:43Z")

</div>

Oh dear! I was reading an older version of the documentation… doh! Thanks so much!

---

<div class="post-metadata">

**Author:** ![vmmhep](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/vmmhep/32/34987_2.png) [@vmmhep](https://discourse.julialang.org/u/vmmhep)\
**Post date:** [March 28, 2022, 5:57pm UTC](https://discourse.julialang.org/t/optim-jl-single-iteration/78619/5 "2022-03-28T17:57:01Z")

</div>

Great catch! Thank you, this indeed works now 🙂
