Reconstructing a signal from FFT by adding individual signal components I'm attempting to reconstruct a signal from the DFT of the signal. I tried to do it by extracting the individual sinusoids and adding them up, but the answer I get is incorrect.

s = [3 1 4 1 5 9 2 6]#Some random signal(first few digits of pi in this case)
t = 0:(1/7):1;#Sampling at each of these equally spaced time intervals
f = fft(s);
a = abs(f);#Magnitude of each DFT component
an = arg(f);#Phase of each DFT component

recs = (a(1)/8)*cos(0*pi*t + arg(1)) + (a(2)/8)*cos(2*pi*t + arg(2)) + (a(3)/8)*cos(4*pi*t + arg(3)) + (a(4)/8)*cos(6*pi*t + arg(4)) + (a(1)/8)*cos(8*pi*t + arg(5));
#The above equation is derived(hopefully correctly) from the theory given in the question description
recs
s


This is a program I wrote in GNU Octave(should work on Matlab too). And the basis for using that particular expression for recs is based on some info I got from http://www.robots.ox.ac.uk/~sjrob/Teaching/SP/l7.pdf. 

I don't understand if my method for getting the individual components was wrong or is it some basic step I'm missing here.
There are a couple of mistakes in your calculations:  


The t array is incorrectly specified. It should be 0:1/8:7/8 as the time positions at which the signal is assumed to be sampled for the default FFT calculation performed by fft() are 0, 1, ..., (N-1) where N = 8 in your case, the signal length. Since in your formula you are using t as the ratio n/N (where n is the array of sample times) you get the aforementioned expression to use for t.
You wrote a(1) instead of a(5) at the last term of your recs expression.
You are missing 2* multiplying each term except the first (k=0) and last (k=N/2) ones, where k is the index of the frequency axis of the Fourier transform F (recall that this 2* comes from leveraging the symmetry of the Fourier transform associated to a real signal).


Therefore, your reconstruction expression should read:

recs =  (1*a(1)/8)*cos(0*pi*t + an(1)) + ...
        (2*a(2)/8)*cos(2*pi*t + an(2)) + ...
        (2*a(3)/8)*cos(4*pi*t + an(3)) + ...
        (2*a(4)/8)*cos(6*pi*t + an(4)) + ...
        (1*a(5)/8)*cos(8*pi*t + an(5));


which gives the original signal s as verified by computing:

recs - s

ans =

   1.0e-14 *

         0   -0.1332         0   -0.0444   -0.2665    0.3553    0.1332         0