14
$\begingroup$

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.

$\endgroup$
10
  • 4
    $\begingroup$ Sin(nx) is the product (or multiplication) of the symbol Sin with the symbol nx. You should be using Sin[n x]. $\endgroup$
    – jVincent
    Commented Jan 7, 2013 at 19:05
  • 2
    $\begingroup$ 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) $\endgroup$
    – ssch
    Commented Jan 7, 2013 at 19:05
  • 2
    $\begingroup$ @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}] $\endgroup$
    – rm -rf
    Commented Jan 7, 2013 at 19:26
  • 1
    $\begingroup$ 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.) $\endgroup$
    – Michael E2
    Commented Jan 7, 2013 at 19:44
  • 2
    $\begingroup$ Sandra, welcome to Mathematica.SE, and thank you for taking the time to format your question so nicely! $\endgroup$ Commented Jan 7, 2013 at 20:27

1 Answer 1

22
$\begingroup$

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)

$\endgroup$
5
  • $\begingroup$ thank you for your graceful recap of the issues and answers. $\endgroup$
    – Joseph
    Commented Jan 8, 2013 at 3:11
  • $\begingroup$ @halirutan, I think you forgot to mention the all important missing pattern n_ in LHS. $\endgroup$
    – carlosayam
    Commented Jan 8, 2013 at 12:19
  • $\begingroup$ @caya Thanks, I added it as last point. $\endgroup$
    – halirutan
    Commented Jan 9, 2013 at 8:59
  • 2
    $\begingroup$ One more alternative notation for multiplication that you didn't mention is whitespace. It's a nice way to make things look more like traditional mathematical notation. $\endgroup$ Commented May 7, 2018 at 19:26
  • 1
    $\begingroup$ @silvascientist why didn't? "or leave a space between them", right there. $\endgroup$
    – Ruslan
    Commented Jan 20, 2021 at 20:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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