# Trouble defining function for homework

My homework problem I realize mathematica has fourier coefficient functions built in, but I am new and trying to learn Mathematica. So, I am doing it the harder way. I am having trouble getting outputs, and I do not know why.

My code,

Line1: Defining f(x)

Line2: defining L for coefficients

Line3&4: defining coefficients

f[x_] := Piecewise[{{x, 0 < x < 1}, {0, -1 < x < 0}}]
L = 1
a[n_] := 1/L*Integrate[f[x]*cos[n*Pi*x/L], {x, -L, L}]
b[n_] := 1/L*Integrate[f[x]*sin[n*Pi*x/L], {x, -L, L}]


picture of my output

Is my problem a conceptual one or a Mathematica error? I am not sure.

UPDATE Had to delete original output due to limit on links. Only difference is I replaced "cos" & "sin" with "Cos" & "Sin" • Thank you!, still getting similar outputs though. – Melendowski Nov 11 '16 at 2:28
• Yeah, so I get a numeric value. I guess what I was expecting Mathematica to do is give a closed form solution, not leave it in integral form. When I did it by hand, I got a closed form solution, which is in the back of the book. Mathematica just isn't giving it to me. – Melendowski Nov 11 '16 at 2:47
• Oh BOY!, THANK YOU! I thought it would just do that automatically. – Melendowski Nov 11 '16 at 2:50

It will easily calculate the a[n] and b[n]:

f[x_] := Piecewise[{{x, 0 < x < 1}, {0, -1 < x < 0}}]
L = 1;
a[n_] := 1/L*Integrate[f[x]*Cos[n*Pi*x/L], {x, -L, L}]
b[n_] := 1/L*Integrate[f[x]*Sin[n*Pi*x/L], {x, -L, L}]


But these have no output. To see the output:

a
-2/Pi^2


Here's the first 10 a's:

a[#] & /@ Range


If you want to see a[n] in symbolic form:

a[n]
(-1 + Cos[n \[Pi]] + n \[Pi] Sin[n \[Pi]])/(n^2 \[Pi]^2)

• Got it, thank you. Mr. Moo answered this but deleted is responses. – Melendowski Nov 11 '16 at 3:22