My homework problem

enter image description here

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"

enter image description here

  • $\begingroup$ Thank you!, still getting similar outputs though. $\endgroup$ – Melendowski Nov 11 '16 at 2:28
  • $\begingroup$ 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. $\endgroup$ – Melendowski Nov 11 '16 at 2:47
  • $\begingroup$ Oh BOY!, THANK YOU! I thought it would just do that automatically. $\endgroup$ – 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:


Here's the first 10 a's:

a[#] & /@ Range[10]

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

(-1 + Cos[n \[Pi]] + n \[Pi] Sin[n \[Pi]])/(n^2 \[Pi]^2)
  • $\begingroup$ Got it, thank you. Mr. Moo answered this but deleted is responses. $\endgroup$ – Melendowski Nov 11 '16 at 3:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.