# MethodError: no method matching adjoint(::typeof(f))

**URL:** <https://discourse.julialang.org/t/methoderror-no-method-matching-adjoint-typeof-f/81155>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [May 16, 2022, 3:11pm UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-adjoint-typeof-f/81155 "2022-05-16T15:11:31Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![MoKent](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@MoKent](https://discourse.julialang.org/u/MoKent)\
**Post date:** [May 16, 2022, 3:11pm UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-adjoint-typeof-f/81155/1 "2022-05-16T15:11:31Z")

</div>

I’m trying to optimize quadratic function to learn julia as below, but dind’t work.  
Can anyone tell me what is wrong?

input

```julia
using PyPlot
using ForwardDiff

x_opt = 0.50
f(x) = -2(x - x_opt)^2
xs = range(-3, 3, length = 100)

fig, ax = subplots()
ax.plot(xs, f.(xs))
ax = grid()

function gradient_method_dim1(f, x_init, eta, maxiter)
    x_seq = Array{typeof(x_init), 1}(undef, maxiter)

    Dxf(x) = ForwardDiff.derivative(f, x)
    x_seq[1] = x_init
    
    for i in 2:maxiter
        x_seq[i] = x_seq[i-1] + eta*f'(x_seq[i-1])
    end

    x_seq
end

x_init = -2.5
maxiter = 100
eta = 0.1
x_seq = gradient_method_dim1(f, x_init, eta, maxiter)
f_seq = f.(x_seq)
println(f_seq)

```

output

```julia
ERROR: LoadError: MethodError: no method matching adjoint(::typeof(f))
Closest candidates are:
  adjoint(::Union{LinearAlgebra.QR, LinearAlgebra.QRCompactWY, LinearAlgebra.QRPivoted}) at /opt/julia-1.7.2/share/julia/stdlib/v1.7/LinearAlgebra/src/qr.jl:509
  adjoint(::Union{LinearAlgebra.Cholesky, LinearAlgebra.CholeskyPivoted}) at /opt/julia-1.7.2/share/julia/stdlib/v1.7/LinearAlgebra/src/cholesky.jl:538
  adjoint(::LinearAlgebra.SVD) at /opt/julia-1.7.2/share/julia/stdlib/v1.7/LinearAlgebra/src/svd.jl:262
  ...
Stacktrace:
 [1] gradient_method_dim1(f::Function, x_init::Float64, eta::Float64, maxiter::Int64)
   @ Main ~/path/to/optimization:22
 [2] top-level scope
   @ ~/path/to/optimization.jl:32
 [3] include(fname::String)
   @ Base.MainInclude ./client.jl:451
 [4] top-level scope
   @ REPL[49]:1
in expression starting at /path/to/optimization.jl:32

```

---

<div class="post-metadata">

**Author:** ![StevenWhitaker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevenwhitaker/32/9749_2.png) [@StevenWhitaker](https://discourse.julialang.org/u/StevenWhitaker)\
**Post date:** [May 16, 2022, 5:54pm UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-adjoint-typeof-f/81155/2 "2022-05-16T17:54:08Z")

</div>

Welcome to Julia Discourse, and thanks for providing your code and stacktrace!

> [@MoKent](#):
>
> `ERROR: LoadError: MethodError: no method matching adjoint(::typeof(f))`

This means you tried to call `adjoint` on the function `f` (whose type is `typeof(f)`), but that method isn’t defined. Even though you didn’t explicitly write `adjoint(f)` anywhere, you do have

> [@MoKent](#):
>
> ` x_seq[i] = x_seq[i-1] + eta*f'(x_seq[i-1])`

Specifically, `f'` is parsed as `adjoint(f)`, so an equivalent statement to the above is

```julia
x_seq[i] = x_seq[i-1] + eta * adjoint(f)(x_seq[i-1])

```

It looks like you meant to take the derivative of `f`, so you should instead write

```julia
x_seq[i] = x_seq[i-1] + eta * Dxf(x_seq[i-1])

```

---

<div class="post-metadata">

**Author:** ![MoKent](https://avatars.discourse-cdn.com/v4/letter/m/5fc32e/32.png) [@MoKent](https://discourse.julialang.org/u/MoKent)\
**Post date:** [May 17, 2022, 1:27am UTC](https://discourse.julialang.org/t/methoderror-no-method-matching-adjoint-typeof-f/81155/3 "2022-05-17T01:27:21Z")

</div>

Thank you for your kind instruction.  
I’ve noticed my super easy mistake ☹
