# Conditions for Piecewise[ ] Command

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.

Clear[f,a,b,F]
f[t_]=Piecewise[{{-Sqrt[9+t+t^2],-2<t<0},{Exp[Sin[t]],0<=t<=2}}]
a0=23/9;
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}];
Plot[{f[t],F[t]},{t,-1,1}]


-
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

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)

-
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))


or

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;
Plot[{f[t],F[t]},{t,-1,1}]


gives

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