Mathematica Stack Exchange is a question and answer site for users of Mathematica. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to plot a function in which is piecewise defined. I found the Fourier coefficients of this, and my question is how to define even & odd conditions for the Piecewise command? I used 2n and 2n+1 for even & odd respectively, but this does not suffice. I have a MWE below.

a[k_]=Piecewise[{{20/(k^4Pi^4),2k},{-83/(k Pi),2k+1}}]
b[k_]=Piecewise[{{-92/(k^3 Pi^3),2k},{-53/(k Pi)+14/(k^2 Pi^2),2k+1}}]
F[t_]=a0/2+Sum[a[k]Cos[(k Pi t)/2]+b[k]Sin[(k Pi t)/2],{k,1,20}];

Mathematica graphics

share|improve this question
Try using OddQ[n] and EvenQ[n] to test values of n. – Mr.Wizard Jun 23 '12 at 23:50
@Mr.Wizard: Does not seem to work correctly I think. – night owl Jun 24 '12 at 0:30
Here is a related question. – J. M. Jun 24 '12 at 0:40
@Mr.Wizard: Sorry, that did work. Sorry again. It was problems with my kernel. I had used Clear but it was not clearing my variables, so when I used Remove that had done the trick. Along with a pair of parentheses I was missing :). – night owl Jun 24 '12 at 5:17
up vote 9 down vote accepted

Instead of using Piecewise you can also use Mathematicas pattern matching, which i think makes for more idiomatic Mathematica code in your case:

a0 = 23/9;
a[k_?EvenQ] := 20/(k^4 Pi^4)
a[k_?OddQ]  := -83/(k Pi)
b[k_?EvenQ] := -92/(k^3 Pi^3)
b[k_?OddQ]  := -53/(k Pi) + 14/(k^2 Pi^2)

F[t_] = a0/2 + Sum[a[k] Cos[(k Pi t)/2] + b[k] Sin[(k Pi t)/2], {k, 1, 1000}];
Plot[F[t], {t, -1, 1}]

You could even get rid of the OddQ tests and let the pattern matcher just 'fall through' the EvenQ test to get to the second definition:

a[k_?EvenQ] := 20/(k^4 Pi^4)
a[k_]       := -83/(k Pi)                 (* odd case*)
b[k_?EvenQ] := -92/(k^3 Pi^3)
b[k_]       := -53/(k Pi) + 14/(k^2 Pi^2) (* odd case*)

alternatively you could use /; for stating the condition

a[k_] := 20/(k^4 Pi^4) /; EvenQ[k]
a[k_] := -83/(k Pi)    /; OddQ[k]
b[k_] := -92/(k^3 Pi^3)             /; EvenQ[k]
b[k_] := -53/(k Pi) + 14/(k^2 Pi^2) /; OddQ[k]

. The same with 'fall through':

a[k_] := 20/(k^4 Pi^4) /; EvenQ[k]
a[k_] := -83/(k Pi)
b[k_] := -92/(k^3 Pi^3)             /; EvenQ[k]
b[k_] := -53/(k Pi) + 14/(k^2 Pi^2)
share|improve this answer
Thanks! This was very useful information to learn. I like having different ways to do the same task instead of having to conform to one specific style. I always like learning how to do a problem and alternative ways also. – night owl Jun 25 '12 at 5:57

Change your definition of a[k] and b[k] as (as Mr.W suggested in comments)

a[k_] := Piecewise[{{20/(k^4 Pi^4), EvenQ[k]}, {-83/(k Pi), OddQ[k]}}]
b[k_] := Piecewise[{{-92/(k^3 Pi^3), EvenQ[k]}, {-53/(k Pi) + 14/(k^2 Pi^2), OddQ[k]}}]

or define them as

a[k_] := (1 - Mod[k/2, 2]) (20/(k^4 Pi^4)) + Mod[k/2, 2] (-83/(k Pi))
b[k_] := (1 - Mod[k/2, 2]) (-92/(k^3 Pi^3)) + Mod[k/2, 2] (-53/(k Pi) + 14/(k^2 Pi^2))


a[k_] := Boole[EvenQ[k]] (20/(k^4 Pi^4)) + Boole[OddQ[k]] (-83/(k Pi))
b[k_] :=  Boole[EvenQ[k]] (-92/(k^3 Pi^3)) + Boole[OddQ[k]] (-53/(k Pi) + 14/(k^2 Pi^2))

and change summation index n to k in definition of F[t]:

F[t_] := a0 +  Sum[a[k] Cos[(k Pi t)/5] + b[k] Sin[(k Pi t)/5], {k, 1, 20}]

With these changes:

f[t_] := Piecewise[{{-Sqrt[9 + t + t^2], -2 < t < 0}, {Exp[Sin[t]],    0 <= t <= 2}}];
a0 = 23/9;


enter image description here

share|improve this answer
Thanks for those. I will look into how Mod and Boole works on these expressions. – night owl Jun 25 '12 at 6:08

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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