# Higher derivative gives NAN

**URL:** <https://discourse.julialang.org/t/higher-derivative-gives-nan/116763>\
**Category:** New to Julia\
**Tags:** autodiff\
**Created:** [July 8, 2024, 10:03am UTC](https://discourse.julialang.org/t/higher-derivative-gives-nan/116763 "2024-07-08T10:03:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![gkeee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gkeee/32/13964_2.png) [@gkeee](https://discourse.julialang.org/u/gkeee)\
**Post date:** [July 8, 2024, 10:03am UTC](https://discourse.julialang.org/t/higher-derivative-gives-nan/116763/1 "2024-07-08T10:03:11Z")

</div>

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) ?

```julia
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)

```

---

<div class="post-metadata">

**Author:** ![nsajko](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nsajko/32/221187_2.png) [@nsajko](https://discourse.julialang.org/u/nsajko)\
**Post date:** [July 8, 2024, 10:32am UTC](https://discourse.julialang.org/t/higher-derivative-gives-nan/116763/2 "2024-07-08T10:32:10Z")

</div>

First things first, take a look at:

> [@Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757):
>
> Welcome to the Julia Discourse! We are enthusiastic about helping Julia programmers, both beginner and experienced. This public service announcement (PSA) outlines best practices when asking for help. Following these points makes it easier for us to help you and more likely you’ll get a prompt, useful answer. Keywords are highlighted to make it easier to refer to specific points. Choose a descriptive title that captures the key part of your question, eg “plots with multiple axes” instead of …

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

---

<div class="post-metadata">

**Author:** ![Maysam\_Gholampour](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maysam_gholampour/32/218184_2.png) [@Maysam\_Gholampour](https://discourse.julialang.org/u/Maysam_Gholampour)\
**Post date:** [July 8, 2024, 10:55am UTC](https://discourse.julialang.org/t/higher-derivative-gives-nan/116763/3 "2024-07-08T10:55:47Z")

</div>

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

```julia
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

```

---

<div class="post-metadata">

**Author:** ![gkeee](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gkeee/32/13964_2.png) [@gkeee](https://discourse.julialang.org/u/gkeee)\
**Post date:** [July 8, 2024, 1:31pm UTC](https://discourse.julialang.org/t/higher-derivative-gives-nan/116763/4 "2024-07-08T13:31:31Z")

</div>

Taylor series does not give a result for my function

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

```

ı try

```julia
derivative(f,1,3)

```

It give “false”.

---

<div class="post-metadata">

**Author:** ![Maysam\_Gholampour](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/maysam_gholampour/32/218184_2.png) [@Maysam\_Gholampour](https://discourse.julialang.org/u/Maysam_Gholampour)\
**Post date:** [July 8, 2024, 3:39pm UTC](https://discourse.julialang.org/t/higher-derivative-gives-nan/116763/5 "2024-07-08T15:39:26Z")

</div>

please first read :  
[Please read: make it easier to help you](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757)

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:**

2. **Imaginary Part:**

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

- The derivative of f(x) is:

- Separating the derivative into real and imaginary parts:

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.
