# Numeric Derivative Julia Diff, State Of Automatic Differentiation In Julia

I am stuck on a seemingly simple problem of evaluating the first-order derivative of a function at a series of fixed values. Can't figure out the syntax for some reason and can't find any actual examples either. The example for ForwardDiff.gradient() on the package's GitHub page doesn't help.

Đang xem: Derivative julia

For instance, what would be the Julia code for evaluating the derivative of x2 at x = <2, 3, 5>?

If the function you want to evaluate is

f(x)

then you get its derivative with

ForwardDiff.derivative(f, x)

but your example doesn't exactly make sense. You can't square a vector, nor can you differentiate with respect to one (or, if you do, then you're taking a gradient or a jacobian), so it doesn't make sense to take the derivative of x2 at x = <2, 3, 5>. Or do you want to take the derivative at each of those points? That would be:

julia> f(x) = x^2julia> using ForwardDiffjulia> ForwardDiff.derivative.(f, <2, 3, 5>) # note the . before ( to indicate that the function should act elementwise on the vector <2, 3, 5>3-element Array{Int64,1}: 4 6 10
8
Share
ReportSave
level 1
Op · 3y
Thank you for the examples! It's helped me get the idea of what the syntax should look like, and everything works now.

4
Share
ReportSave
level 1
· 3y · edited 3y

I'd recommend always glancing over the documentation of any library you plan you using, since you may not only find extra examples but also functions you didn't know existed and may end up solving your problem! You can find the documentation of ForwardDiff here.

You'll see a function called derivative! Hence,

x0 = <2, 3, 5> derivative(x0::Number) = ForwardDiff.derivative(x -> x^2, x0) map(derivative, x0)should give you the derivative of x2 at all x0 points

3
Share
ReportSave
level 2
· 3y

Thanks for the answer. It's helped me out a lot.Would you be able to let me know if it is possible to use this method to calculate second derivatives too? Or how, I'm really stuck here.

1
Share
ReportSave

Continue browsing in r/Julia

r/Julia
Julia is a high-level, high-performance dynamic programming language for technical computing, with syntax that is familiar to users of other technical computing environments.
17.3k
Members

54

Online

Created Dec 11, 2008
Join

Top posts april 6th 2018Top posts of april, 2018Top posts 2018