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
[ ]
are always used when you want to call a function like Sin[x]
.
( )
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.
{ }
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.
[[ ]]
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)
Sin(nx)
is the product (or multiplication) of the symbolSin
with the symbolnx
. You should be usingSin[n x]
. $\endgroup$Cos(nx)
will be interpreted asCos*nx
try:Cos[n x]
(note the space beteen n and x, otherwise it'll be interpreted as variable called nx) $\endgroup$Simplify[(-2 n Pi Cos[n Pi] + 2 Sin[n Pi])/(n^2 Pi), Assumptions :> {n ∈ Integers}]
$\endgroup$a[0]
should be1/(2 Pi) Integrate[f[x], {x, -Pi, Pi}]
. (1/2 Pi
is the same asPi/2
-- an unlikely coefficient for the integral in this context.) $\endgroup$