fourier series fitting matlab
The cftool is going to use a curve fitting procedure.  Some time of non-linear regression.  In a general sense, it is a trial and error method:
1) guess a value for the coefficients 2) calculate the function with the coefficient, and determine the error between the calculated function and the original data, 3) try a new value for the coefficients, that is slightly different than the original guess. 4) go back to step 2, and keep going until the error is sufficiently small.

cftool is a very general tool that can be used for all kinds of problems, not just fourier series. cftool has no idea that you are trying fit a fourier series. In a general sense, it really is no different than if you just tried a whole bunch of combinations until you found one that worked, it's just that because it is a computer it can try many thousands of combinations in a second.

The formulas you showed, on the other hand, are the actual expression for calculating a fourier series.  Depending on your data, cftool will probably never match these exactly, although it might get close.  The reason it doesn't match is that there may be many different combinations that match the data fairly well, and cftool cannot tell the difference between them.  One way to help cftool get closer would be to give it more data to work with (i.e. a longer sample).  

Depending on what your goal is, you might just want to use the closed form expression for a fourier series.  that will be the most accurate, and probably quicker to compute anyway.