Partial integration in MATLAB I am working on Fourier Analysis and need to implement Fourier Series in MATLAB. What I want is to do the following:


*

*Obtain the Fourier Coefficients as a function of n,  $a(n)$,  so that whenever I input say n=2, I get the numeric value of a(2). I need an analytical expression for $a(n)$ something like for example $a(n) = \frac{1}{1+n}, n \in \mathbb{N}$.


Let $f \in L^{2}[0,1], $ and we are working with the standard orthonormal basis $\{\sqrt2 sin (2k \pi x)\}_{n=1}^{\infty} \cup \{\sqrt2 cos (2k \pi x)\}_{n=1}^{\infty} \cup \{1\}$
The problem that I am facing with implementing the partial integration in the following step 
$a(n) := \int_{0}^{1} f(x).e_{n}(x) dx$ 
where $e_{n}(x)$ is $\sqrt2 sin (2n \pi x), \sqrt2 cos (2n \pi x)$ or 1. 
So for example we take $e_n(x) = \sqrt2 cos(2n\pi x)$. 
Let $g(n,x) = f(x).\sqrt2 cos(2n\pi x)$.
How do I get the expression for $a(n)$ i.e.,
$a(n) = \int_{0}^{1} g(n,x) dx$ ?
I want to be able to give n as input to $a(n)$ and obtain the nth Fourier coefficient, $n \in \mathbb{N}$. 
Now the things that I have tried are the following, 
f = @(x)x;
g = @(n,x)(f(x).*cos(2n$\pi$x));
a_n = @(n)integral(@(x)g,0,1);
So then if I try to obtain a_n(2), I get the following error
Error using integralCalc/finalInputChecks (line 511)
Input function must return 'double' or 'single' values. Found 'function_handle'.
Error in integralCalc/iterateScalarValued (line 315)
                finalInputChecks(x,fx);
Error in integralCalc/vadapt (line 133)
            [q,errbnd] = iterateScalarValued(u,tinterval,pathlen);
Error in integralCalc (line 76)
        [q,errbnd] = vadapt(@AtoBInvTransform,interval);
Error in integral (line 89)
Q = integralCalc(fun,a,b,opstruct);
Error in @(n)integral(@(x)g,0,1)
Thanks for reading my question.
 A: Here is the correct way to do it :
 f = @(x)x;
 gc = @(n,x)(f(x).*cos(2*n*pi*x));
 gs = @(n,x)(f(x).*sin(2*n*pi*x));
 for n=0:5
     a(n+1)=integral(@(x)(gc(n,x)),0,1);
 end;
 for n=0:5
     b(n+1)=integral(@(x)(gs(n,x)),0,1);
 end;
 a
 b

(I have been obliged to make a shift, using $a(n+1)$ instead of $a(n)$ because unfortunately Matlab doesn't accept 0 indices).

Edit 1 : In order to make a step forward in the direction of an explicit expression for the $b_n$ (of course, the $a_n$, beyond $a_0$, are all zero because $f$ is an odd function), I add the idea to change the type of numbers by using rationnals, i.e., I placed a "format rat" right at the beginning of the program. Then I obtained :
$$b_0=0,b_1=-\dfrac{113}{710},b_2=-\dfrac{113}{1420}, b_3=-\dfrac{113}{2130}, b_4=-\dfrac{113}{2840}, b_5=-\dfrac{113}{3550},\cdots $$
A formula of type $b_k=a/k$ was evident, but with which constant $a$ ?
Some minutes later, it became to me evident: 355/113 is the famous approximation of $\pi$ (exact up to 6 decimal places): see (http://turner.faculty.swau.edu/mathematics/materialslibrary/pi/pirat.html). 
Thus the most reasonable guess is that, for $k>0$, $ \ b_k=-\dfrac{1}{2 \pi k}$.

Edit 2 : I have been baffled by the answer of Fabio Somenzi. 
A: You may try something like this:
syms x n f(x)
assume(n,'integer')
assumeAlso(n > 0)
raw = int(f * cos(2*sym(pi)*n*x), 0, 1);
a(n) = simplify(subs(raw,f,x^2))

which produces
$$\frac{1}{2n^2\pi^2} \enspace. $$
