Higher derivative gives NAN

I want to compute the higher derivatives of f1(x) = exp(im * 2π * x) / (im * x). For example 8th derivative, ı used nth_derivative function with ForwardDiff, but 8th derivatives gives NAN. I changed the function as f2(x) = exp(im * 2π * x) * (im * x)^(-1). f2 function given NAN if I choose x=1/1000. I changed the function as f3(x) = -im * exp(im * 2π * x) * x^(-1), it is calculated. But the nth_derivative so slow.

I don’t understand the effect of the functions and how can I compute fast the nth derivative of exp(im * 2π * x) / (im * x) ?

using ForwardDiff

function nth_derivative(f, x, n)
    if n == 0
        return f(x)
    else
        return ForwardDiff.derivative(x -> nth_derivative(f, x, n - 1), x)
    end
end

f3(x) = -im * exp(im * 2π * x) * x^(-1)
nth_derivative(f3, 1/1000, 8)

First things first, take a look at:

In particular, please use code block Markdown to make your message readable.

1 Like

I think you can use TaylorDiff.jl. this is a MWE.

using TaylorDiff

x = 0.1
derivative(sin, x, 10) # scalar derivative
v, direction = [3.0, 4.0], [1.0, 0.0]
derivative(x -> sum(exp.(x)), v, direction, 2) # directional derivative
2 Likes

Taylor series does not give a result for my function

function f(w)
    return -im * exp(im * 2π * w) / w^
end

ı try

derivative(f,1,3)

It give “false”.

please first read :
Please read: make it easier to help you

If x is real, you can simplify f(x) to f(x) = h(x) + i * q(x), where h(x) and q(x) are real-valued functions, and i is the imaginary unit.

To derive f(x) into its real and imaginary parts separately:

  1. Real Part:

    • Re(f(x)) = h(x)
  2. Imaginary Part:

    • Im(f(x)) = q(x)

When differentiating f(x) with respect to x, and treating the real and imaginary parts separately:

  • The derivative of f(x) is:

    • d/dx [f(x)] = d/dx [h(x) + i * q(x)] = dh(x)/dx + i * dq(x)/dx
  • Separating the derivative into real and imaginary parts:

    • d/dx [f(x)] = dh(x)/dx + i * dq(x)/dx

Here, dh(x)/dx is the derivative of the real part h(x) with respect to x, and dq(x)/dx is the derivative of the imaginary part q(x) with respect to x.