I try to implement a higher order numerical derivative using recursion.
I don't see what is wrong with my code.

To clarify, i want to compute `dy^n/dx^n`.

```
import numpy as np
import matplotlib.pyplot as plt

def derivative(x,y,n):
    if(n==1):
        return np.gradient(y,x)
    else:
        return derivative(x,y,n-1)

a=0
b=10
N_points=10000

x=np.linspace(a,b,N_points)
y=np.cos(x)

plt.figure()
plt.plot(x,y,label="f(x)")
for n in range(1,8):
    plt.plot(x,derivative(x,y,n),label="fprime(x)")
plt.legend()
plt.title("function and derivative")

```

I improved my code to

```
import numpy as np
import matplotlib.pyplot as plt

def derivative(x,y,n):
    if(n==1):
        return np.gradient(y,x)
    else:
        return np.gradient(derivative(x,y,n-1),x)

a=0
b=10
N_points=10000

x=np.linspace(a,b,N_points, dtype=float)
y=np.cos(x, dtype=float)
plt.figure()
plt.plot(x,y,label="f(x)")

n_max=7
for n in range(1,n_max):
    yprime=derivative(x,y,n)
    yprime[0:n]=yprime[n]
    yprime[-n:]=yprime[-n-1]

    plt.plot(x,yprime,label=f"fprime{n}(x)")

plt.legend()
plt.title("function and derivative")

```

Now, for `n_max<=5` everything looks like it should do. But for `n_max` greater than that, the derivatives begin to look very noisy. What is happening? And could it be a problem with data types?

[![First 4 derivatives look fine](https://i.stack.imgur.com/67sMY.png)](https://i.stack.imgur.com/67sMY.png)

[![After that they look noisy](https://i.stack.imgur.com/L6LNu.png)](https://i.stack.imgur.com/L6LNu.png)