Different results applying DFT (formula only) and FFT on Matlab?
The Fast Fourier Transform (FFT) is a class of algorithms that efficiently implement the Discrete Fourier Transform (DFT). However, what you computed by hand is the Continuous-Time Fourier Transform (CTFT), which is substantially different from the DFT. The DFT is applied to finite length discrete-time sequences, whereas the CTFT is applied to continuous-time functions of possibly infinite duration:

$$\begin{align}X[k]&amp;=\sum_{n=0}^{N-1}x[n]e^{-j2\pi kn/N}\quad&amp;\text{(DFT)}\\
X(j\omega)&amp;=\int_{-\infty}^{\infty}x(t)e^{-j\omega t}dt\quad&amp;\text{(CTFT)}\end{align}$$

However, you can use the DFT to approximately compute the CTFT, but there will always be at least one of the following two errors (and usually both):


Truncation error: if the signal is too long (or has infinite duration as in your example), it must be truncated to fit inside the DFT window.
Aliasing error: the continuous-time signal must be sampled, and sampling will usually introduce aliasing unless the signal is band-limited and the sampling theorem is satisfied.


Since the signal in your example is neither band-limited nor time-limited you will get both errors mentioned above.

More information on approximating the CTFT by the DFT can be found in this answer.

The following Octave script shows how the approximation works for the example in your question.


% choose parameters (sampling frequency and time interval)
Fs = 50;        % sampling frequency
T1 = 2;         % lower and upper
T2 = 20;        % integration limits

Ts = 1/Fs;
DT = T2 - T1;
N = round(DT/Ts);   % DFT length

% sampled time-domain function on [T1,T2]
t = (0:N-1)*DT/(N-1) + T1;
xd = (t-2).*exp(-t);

% DFT approximation of CTFT
Xd = Ts * fft(xd);
fd = (0:N-1)*Fs/N;

% phase correction for offset T1
Xd = Xd .* exp(-1i*T1*2*pi*fd);

% exact expression for CTFT
N2 = round(N/2);
fc = fd(1:N2);
Hc = exp(-2*(1+1i*2*pi*fc))./(1+1i*2*pi*fc).^2;

% plot results
subplot(2,1,1), plot(fc,20*log10(abs(Xd(1:N2))),fc,20*log10(abs(Hc)))
    title('magnitudes (dB)'), xlabel('f'), legend('DFT','CTFT'),
    axis([0,Fs/2,-110,-10]), grid on
subplot(2,2,3), plot(fc,20*log10(abs(Xd(1:N2))) - 20*log10(abs(Hc)))
    title('magnitude error (dB)'), xlabel('f'), grid on
subplot(2,2,4), plot(fc,unwrap(angle(Xd(1:N2))) - unwrap(angle(Hc)))
    title('phase error (rad)'), xlabel('f'), grid on


The plot below shows the magnitudes of the CTFT and its approximation by the DFT, as well as the magnitude and phase approximation errors.