Skip to content

Instantly share code, notes, and snippets.

@airicbear
Created January 25, 2022 17:34
Show Gist options
  • Save airicbear/ca13303932922da2f0ba8d83991fd7f9 to your computer and use it in GitHub Desktop.
Save airicbear/ca13303932922da2f0ba8d83991fd7f9 to your computer and use it in GitHub Desktop.
Derivatives and Taylor series in Julia using ForwardDiff.jl
using FowardDiff
# Derivative of f
D(f) = x -> FowardDiff.derivative(f, x)
# nth Derivative of f
D(f, n) = let
F = f
while n > 0
F = D(F)
n -= 1
end
F
end
# Taylor Series approximation of f at x0 with n terms
T(f, x0, N) = x -> sum([D(f, n)(x0) * (x - x0)^n / factorial(n) for n ∈ 0:N])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment