# ForwardDiff.jacobian Method error

**URL:** <https://discourse.julialang.org/t/forwarddiff-jacobian-method-error/112979>\
**Category:** Numerics\
**Tags:** question\
**Created:** [April 15, 2024, 8:13pm UTC](https://discourse.julialang.org/t/forwarddiff-jacobian-method-error/112979 "2024-04-15T20:13:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Domenico\_Lahaye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/domenico_lahaye/32/203728_2.png) [@Domenico\_Lahaye](https://discourse.julialang.org/u/Domenico_Lahaye)\
**Post date:** [April 15, 2024, 8:13pm UTC](https://discourse.julialang.org/t/forwarddiff-jacobian-method-error/112979/1 "2024-04-15T20:13:56Z")

</div>

I get a method error with ForwardDiff.jacobian that I fails to understand. Below a MWE.

```julia
function fooworks(cvec,xmesh)
    residual = cvec 
    return residual
end 

function foofails(cvec,xmesh)
    residual = zeros(length(cvec)) 
    N = length(cvec)

    for i=1:N 
        residual[i] = cvec[i] 
    end 

    return residual
end 

# generate the mesh with N elements (intervals) and N+1 nodes
N = 10; h = 1/N; Np1 = N+1; 
xmesh = Vector(0:h:1)

cvec = ones(size(xmesh))
h = cvec->fooworks(cvec,xmesh);
h = cvec->foofails(cvec,xmesh)
ForwardDiff.jacobian(h,cvec)

```

The error message is:

MethodError: no method matching Float64(::ForwardDiff.Dual{ForwardDiff.Tag{var"#127#128", Float64}, Float64, 11})  
Closest candidates are:  
(::Type{T})(::Real, ::RoundingMode) where T\<:AbstractFloat at rounding.jl:200  
(::Type{T})(::T) where T\<:Number at boot.jl:772  
(::Type{T})(::AbstractChar) where T\<:Union{AbstractChar, Number} at char.jl:50

Help is much appreciated.

---

<div class="post-metadata">

**Author:** ![avikpal](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/avikpal/32/6550_2.png) [@avikpal](https://discourse.julialang.org/u/avikpal)\
**Post date:** [April 15, 2024, 8:55pm UTC](https://discourse.julialang.org/t/forwarddiff-jacobian-method-error/112979/2 "2024-04-15T20:55:56Z")

</div>

```
residual = zeros(length(cvec)) 

```

creates an array of Float64 which is not Dual type. create the vector as `zeros(eltype(cvec), length(cvec))`

---

<div class="post-metadata">

**Author:** ![Domenico\_Lahaye](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/domenico_lahaye/32/203728_2.png) [@Domenico\_Lahaye](https://discourse.julialang.org/u/Domenico_Lahaye)\
**Post date:** [April 15, 2024, 8:59pm UTC](https://discourse.julialang.org/t/forwarddiff-jacobian-method-error/112979/3 "2024-04-15T20:59:41Z")

</div>

Hurray! Sincere thx!
