# How to use ForwardDiff?

**URL:** <https://discourse.julialang.org/t/how-to-use-forwarddiff/77434>\
**Category:** General Usage\
**Created:** [March 5, 2022, 4:41am UTC](https://discourse.julialang.org/t/how-to-use-forwarddiff/77434 "2022-03-05T04:41:42Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![samerb](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/samerb/32/9242_2.png) [@samerb](https://discourse.julialang.org/u/samerb)\
**Post date:** [March 5, 2022, 4:41am UTC](https://discourse.julialang.org/t/how-to-use-forwarddiff/77434/1 "2022-03-05T04:41:42Z")

</div>

I’m trying to learn how to use autodifferentiation for the first time since that came up in something else. What am I doing wrong?

```julia
using ForwardDiff
f(x) = 2x
g = x -> ForwardDiff.gradient(f, x)
g(2)
# DimensionMismatch("gradient(f, x) expects that x is an array. Perhaps you meant derivative(f, x)?")

```

Why does this give a dimension mismatch error? Thanks

---

<div class="post-metadata">

**Author:** ![amrods](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/amrods/32/2543_2.png) [@amrods](https://discourse.julialang.org/u/amrods)\
**Post date:** [March 5, 2022, 5:29am UTC](https://discourse.julialang.org/t/how-to-use-forwarddiff/77434/2 "2022-03-05T05:29:48Z")

</div>

you probably want the derívative, rather than the gradient.
