Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have just started using Mathematica with v9.0. I am trying to follow a computation from a book on Fourier series with the function $f(x)=x$ on the interval $-\pi < x < \pi$.

Here is the code I have tried:

f[x_] = x; 
a[0] = 1/2 Pi Integrate[f[x], {x, -Pi, Pi}]
a[n] = 1/Pi Integrate[f[x] Cos (nx), {x, -Pi, Pi}]
b[n] = 1/Pi Integrate[f[x] Sin (nx), {x, -Pi, Pi}]

I got the same result for a[0] and a[n] as per the book; for b[n] the answer generate by Mathematica is 0, but the answer in the book is $\frac{2}{n}(-1)^{n+1}$. Can someone explain why is this the case?

Note: I found a post here Fourier Coefficients in Mathematica but I can't seem to follow it.

share|improve this question
4  
Sin(nx) is the product (or multiplication) of the symbol Sin with the symbol nx. You should be using Sin[n x]. –  jVincent Jan 7 '13 at 19:05
1  
Cos(nx) will be interpreted as Cos*nx try: Cos[n x] (note the space beteen n and x, otherwise it'll be interpreted as variable called nx) –  ssch Jan 7 '13 at 19:05
2  
@Sandra For integer $n$, you have $\sin(n\pi)=0$ and $\cos(n\pi) = (-1)^{n}$. Include the $-$ sign too and the exponent becomes $n+1$. You can do this with Mathematica by telling it that $n\in\mathbb{Z}$ as Simplify[(-2 n Pi Cos[n Pi] + 2 Sin[n Pi])/(n^2 Pi), Assumptions :> {n ∈ Integers}] –  rm -rf Jan 7 '13 at 19:26
1  
Your formula for a[0] should be 1/(2 Pi) Integrate[f[x], {x, -Pi, Pi}]. (1/2 Pi is the same as Pi/2 -- an unlikely coefficient for the integral in this context.) –  Michael E2 Jan 7 '13 at 19:44
1  
Sandra, welcome to Mathematica.SE, and thank you for taking the time to format your question so nicely! –  Simon Woods Jan 7 '13 at 20:27
show 4 more comments

closed as too localized by Artes, rcollyer, Oleksandr R., whuber, Sjoerd C. de Vries Jan 8 '13 at 21:24

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

up vote 10 down vote accepted

I appreciate very much, that you wrote up such a nicely formatted question, although this is your first post. Therefore, let's put the comments into an answer.

Your first issue was that you used ( ) where you should have used [ ]. That's maybe not obvious for starters and I have seen this mistake very often. There are different types of braces and it's important to always use the right ones. Here is an overview

  1. [ ] are always used when you want to call a function like Sin[x].
  2. ( ) are used change the order of evaluation like you would do in normal mathematics too. Therefore, you would use a*(b+c) to add b and c before doing the multiplication.
  3. { } are to construct lists, matrices and other tensors. {1,2,3} is a list or vector and yes, {x,-Pi,-Pi} is a list too.
  4. [[ ]] is used to take parts of lists and vectors. {x,-Pi,Pi}[[3]] would give you the Pi. See the documentation to Part to learn what else is possible.

Your second issue was that you assumed nx is the multiplication of n and x which it is not. You have to understand that when you use var it would be impossible to decide for Mathematica whether you mean the variable name var or the multiplication v*a*r. Therefore, always put explicit multiplication n*x which is the most clear way or leave a space between them. A third option which is not recommended in this situation is to add parenthesis. n(x) is interpreted as n*x too. Looks odd here but this notation is very common in expressions like (x + 1)(x + 2).

Your third issue that the expression is the hardest one because in general it is not obvious and sometimes even not possible to force Mathematica to simplify an expression into a form you prefer. Additional to the solution to Hypnotoad using

Simplify[(-2 n Pi Cos[n Pi] + 2 Sin[n Pi])/(n^2 Pi), 
  Assumptions :> {n \[Element] Integers}]

you can use Refine which

gives the form of expr that would be obtained if symbols in it were replaced by explicit numerical expressions satisfying the assumptions assum.

Refine[(-2 n Pi Cos[n Pi] + 2 Sin[n Pi])/(n^2 Pi), n \[Element] Integers]

Your fourth issue is about the right hand side of your definition of a[n] and b[n]. When your goal is to define a general coefficient where you can change the value of n to calculate for instance b[5], then those lines will not work. Try yourself what happens if you use b[4]. To make it work, you have to use

b[n_] = 1/Pi Integrate[f[x] Sin [n*x], {x, -Pi, Pi}]

Note the underscore! Now it's possible to calculate b[4]. When you want to know why this is, you should start reading the tutorial about defining functions and follow the references. (Thanks to caya to point out that I had forgotten this issue)

share|improve this answer
    
thank you for your graceful recap of the issues and answers. –  Joseph Jan 8 '13 at 3:11
    
@halirutan, I think you forgot to mention the all important missing pattern n_ in LHS. –  caya Jan 8 '13 at 12:19
    
@caya Thanks, I added it as last point. –  halirutan Jan 9 '13 at 8:59
add comment

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