2

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}]

Mathematica graphics

Sjoerd C. de Vries
  • 65,815
  • 14
  • 188
  • 323
night owl
  • 1,719
  • 3
  • 15
  • 27

2 Answers2

9

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)
Thies Heidecke
  • 8,814
  • 34
  • 44
  • 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 05:57
6

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

enter image description here

kglr
  • 394,356
  • 18
  • 477
  • 896