# Calculating high-order derivatives using FFT in Matlab

I'm trying to calculate the derivative of a function using FFT in Matlab. I've coded the following function to calculate it:

function dfdx = derivative(x,psi_0,n)

Nx = max(size(psi_0));
k = 2*pi/(range(x))*[0:Nx/2-1 0 -Nx/2+1:-1];
dfdx = ifft(((1i*k).^n).*fft(psi_0));

end


For higher derivatives (let's say for the $20$th derivative) it doesn't work. More precisely, the plot of the $20$th derivative doesn't make sense.

Any idea what causes this, and how this can be corrected?

• Can you explain why you need a derivative of such high order? – Bort Apr 8 '15 at 10:03

Let's suppose you have a function $f$ in $L^{2}$, and for simplicity, let's suppose it's a single-variable function defined over the whole real line. If its Fourier transform $\mathcal{F}(f)$ is $\hat{f}$, then $\mathcal{F}(f')(\xi) = 2\pi i \xi\hat{f}(\xi)$.

If you're calculating the $n$th derivative, iterating the process above yields $\mathcal{F}(f^{(n)})(\xi) = (2 \pi i \xi)^{n}\hat{f}(\xi)$, suggesting that the calculation of higher derivatives becomes ill-conditioned. Assuming $\hat{f}$ is a well-conditioned function at $\xi$, the large coefficient in the derivative expression implies that small perturbations in $\xi$ perturb values of the Fourier transform of the $n$th derivative significantly for large $n$.

For instance, if $n = 20$, the magnitude of the constant pre-factor will be $(2\pi)^{20}$, or roughly $10^{16}$. With such a large constant factor, it would be surprising if you obtained meaningful results in floating-point arithmetic. You could compare this approach to calculating derivatives numerically through finite-difference approximations; calculating a 20th derivative with this approach would yield garbage in most cases.

If you used arbitrary precision arithmetic, you would probably see more accurate results. As $n$ increases, you'll require more digits of precision in your input to obtain an output accurate to at least 8 significant figures (this cutoff is arbitrary, but presumably, you want many accurate significant figures).

• and how do i solve it??,or better, do you know any other code that uses fft for derivative??? – user9519 Jun 24 '14 at 7:14
• @user9519: As I stated above, you would need to use arbitrary precision arithmetic. You could use the arbitrary precision FFT in FFTW, if you know C. Supposedly, there is an FFT for Julia being developed that supports arbitrary precision inputs. You cannot get accurate results with your approach for 20th derivatives in double precision arithmetic; it is a fundamental limitation of the technology you are trying to use. I do not know if there is an arbitrary precision datatype in MATLAB (there probably is), and I do not know if there is a MATLAB FFT that will operate on such a datatype. – Geoff Oxberry Jun 24 '14 at 8:26
• thank you. but i still need to make the 20'th derivative. – user9519 Jun 29 '14 at 11:39
• and i have to do it using matlab. i have found that if i use fewer grid points, i can find a higher derivative.(half the grid points will let me find two times the derivative). – user9519 Jun 29 '14 at 11:44
• any body knows why??? and how can i use this information to do it right??? – user9519 Jun 29 '14 at 11:45