mathematica for fourier series and transforms

12
Mathematica for Fourier Series and Transforms Fourier Series Periodic odd step function Use built-in function "UnitStep" to define. "Mod" allows one to make the function periodic, with the "-Pi" shifting the fundamental region of the Mod to -Pi to Pi (rather than 0 to 2Pi). The period is taken to be 2 Pi, symmetric around the origin, so the function is even. “Exclusions->None” makes the plot include the steps. ff0@x_D =- 1 ê 2 + UnitStep@Mod@x, 2 Pi, - PiDD; ff0plot = Plot@ff0@xD, 8x, - 3Pi,3Pi<, Ticks Ø 88- 3 Pi, - 2 Pi, - Pi, 0, Pi, 2 Pi, 3 Pi<<, PlotStyle Ø 8Thick, Red<, Exclusions Ø NoneD -3 p -2 p -p p 2 p 3 p -0.4 -0.2 0.2 0.4 Truncated Fourier Series---since odd can use "FourierSinSeries" In[170]:= ft6ff0 = FourierSinSeries@ff0@xD,x,6D Out[170]= 2 Sin@xD p + 2 Sin@3xD 3 p + 2 Sin@5xD 5 p Faster for evaluation to define Fourier Series directly (given that we’ve worked it out by hand in class notes): ftff0@x_, nmax_D := Sum@Sin@H2n + 1L xDêH2n + 1L, 8n, 0, nmax<D 2 ê Pi

Upload: vuongkhuong

Post on 11-Feb-2017

233 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Mathematica for Fourier Series and Transforms

Mathematica for Fourier Series and Transforms

Fourier Series

Periodic odd step function

Use built-in function "UnitStep" to define."Mod" allows one to make the function periodic, with the "-Pi" shiftingthe fundamental region of the Mod to -Pi to Pi (rather than 0 to 2Pi).The period is taken to be 2 Pi, symmetric around the origin, so the function iseven.“Exclusions->None” makes the plot include the steps.

ff0@x_D = -1 ê 2 + UnitStep@Mod@x, 2 Pi, -PiDD;

ff0plot = Plot@ff0@xD, 8x, -3 Pi, 3 Pi<, Ticks Ø 88-3 Pi, -2 Pi, -Pi, 0, Pi, 2 Pi, 3 Pi<<,PlotStyle Ø 8Thick, Red<, Exclusions Ø NoneD

-3 p -2 p -p p 2 p 3 p

-0.4

-0.2

0.2

0.4

Truncated Fourier Series---since odd can use "FourierSinSeries"

In[170]:= ft6ff0 = FourierSinSeries@ff0@xD, x, 6D

Out[170]=2 Sin@xD

p+2 Sin@3 xD

3 p+2 Sin@5 xD

5 p

Faster for evaluation to define Fourier Series directly (given that we’ve worked it out by hand in class notes):

ftff0@x_, nmax_D := Sum@Sin@H2 n + 1L xD ê H2 n + 1L, 8n, 0, nmax<D 2 ê Pi

Page 2: Mathematica for Fourier Series and Transforms

ftff0@x, 2D

2 JSin@xD + 13Sin@3 xD + 1

5Sin@5 xDN

p

Comparing step to truncated Fourier series:

In[156]:= stepplot@n_D :=Plot@8ff0@xD, ftff0@x, nD<, 8x, -Pi, Pi<, Ticks Ø 88-Pi, -Pi ê 2, 0, Pi ê 2, Pi<<,PlotStyle Ø 88Thick, Red<, 8Thick, Blue<<, Exclusions Ø NoneD

In[160]:= stepplot@0D

Out[160]=-p -

p

2

p

2p

-0.6

-0.4

-0.2

0.2

0.4

0.6

Make animation showing convergence:

In[163]:= Manipulate@stepplot@nD, 8n, 0, 20, 1<D

Out[163]=

n

1

-p -p

2

p

2p

-0.6

-0.4

-0.2

0.2

0.4

0.6

Comparing 3, 21 and 101 term Fourier Series to original function

2 Fourier.nb

Page 3: Mathematica for Fourier Series and Transforms

Plot@8ff0@xD, ftff0@x, 2D, ftff0@x, 20D, ftff0@x, 100D<,8x, -Pi, Pi<, Ticks Ø 88-Pi, -Pi ê 2, 0, Pi ê 2, Pi<<, PlotStyle Ø88Thick, Red<, 8Thick, Blue<, 8Thick, Green<, 8Thick, Black<<, Exclusions Ø NoneD

-p -p

2

p

2p

-0.6

-0.4

-0.2

0.2

0.4

0.6

Zoom in to see Gibbs phenomenon: lack of convergence near step.Here use 21, 101 and 501 term series.

Plot@8ff0@xD, ftff0@x, 20D, ftff0@x, 100D, ftff0@x, 500D<,8x, -0.1, +0.1<, PlotRange Ø 88-.1, 0.1<, 80.4, .6<<, PlotStyle Ø88Thick, Red<, 8Thick, Green<, 8Thick, Black<, 8Thick, Orange<<, Exclusions Ø NoneD

-0.10 -0.05 0.05 0.10

0.45

0.50

0.55

0.60

Using complex fourier series

In[174]:= ft6ff0complex = FourierSeries@ff0@xD, x, 6D

Out[174]=Â ‰-Â x

p- ‰Â x

p+Â ‰-3 Â x

3 p-Â ‰3 Â x

3 p+Â ‰-5 Â x

5 p-Â ‰5 Â x

5 p

Simplify doesn’t help, but FullSimplify brings result back into Sin form found above

In[178]:= FullSimplify@ft6ff0complexD

Out[178]=2 H15 Sin@xD + 5 Sin@3 xD + 3 Sin@5 xDL

15 p

Fourier.nb 3

Page 4: Mathematica for Fourier Series and Transforms

Even "barrier" function

Use built-in function "UnitStep" to define."Mod" allows one to make the function periodic, with the "-Pi" shiftingthe fundamental region of the Mod to -Pi to Pi (rather than 0 to 2Pi).The period is taken to be 2 Pi, symmetric around the origin, so the function iseven.

ff1@x_D = UnitStep@Mod@x + Pi ê 2, 2 Pi, -PiDD UnitStep@Mod@Pi ê 2 - x, 2 Pi, -PiDD;

ff1plot = Plot@ff1@xD, 8x, -3 Pi, 3 Pi<, Ticks Ø 88-3 Pi, -2 Pi, -Pi, 0, Pi, 2 Pi, 3 Pi<<,PlotStyle Ø 8Thick, Red<, Exclusions Ø NoneD

-3 p -2 p -p p 2 p 3 p

0.2

0.4

0.6

0.8

1.0

For an even function can use Cosine Fourier Series, here up to Cos[6x].We will call this a 4 term series.

ft6ff1@x_D = FourierCosSeries@ff1@xD, x, 6D

1

2+2 Cos@xD

p-2 Cos@3 xD

3 p+2 Cos@5 xD

5 p

The function does a pretty poor job of representing the "barrier" at this order.

Plot@8ff1@xD, ft6ff1@xD<, 8x, -3 Pi, 3 Pi<,Ticks Ø 88-3 Pi, -2 Pi, -Pi, 0, Pi, 2 Pi, 3 Pi<<,PlotStyle Ø 88Thick, Red<, 8Thick, Blue<<, Exclusions Ø NoneD

-3 p -2 p -p p 2 p 3 p

0.2

0.4

0.6

0.8

1.0

4 Fourier.nb

Page 5: Mathematica for Fourier Series and Transforms

If one uses "FourierSeries" one gets the complex form, which is equivalent to that found above:

FourierSeries@ff1@xD, x, 6D

1

2+‰-Â x

p+‰Â x

p-‰-3 Â x

3 p-‰3 Â x

3 p+‰-5 Â x

5 p+‰5 Â x

5 p

Since the general form is known, it is faster (for evaluation) to write out the Fourier Transform explicitly.The number of terms is 2 nmax+2 (including the constant).

In[164]:= ftff1@x_, nmax_D := 1 ê 2 + Sum@H-1L^n Cos@H2 n + 1L xD ê H2 n + 1L, 8n, 0, nmax<D 2 ê Pi

Making and manipulating plots:

In[165]:= barrierplot@n_D :=Plot@8ff1@xD, ftff1@x, nD<, 8x, -Pi, Pi<, Ticks Ø 88-Pi, -Pi ê 2, 0, Pi ê 2, Pi<<,PlotStyle Ø 88Thick, Red<, 8Thick, Blue<<, Exclusions Ø NoneD

In[166]:= barrierplot@1D

Out[166]=

-p -p

2

p

2p

0.2

0.4

0.6

0.8

1.0

Fourier.nb 5

Page 6: Mathematica for Fourier Series and Transforms

In[167]:= Manipulate@barrierplot@nD, 8n, 0, 20, 1<D

Out[167]=

n

3

-p -p

2

p

2p

0.2

0.4

0.6

0.8

1.0

Comparing the 4, 22 and 102 term Cosine Series to the function itself (which is no longer visible):

Plot@8ff1@xD, ftff1@x, 2D, ftff1@x, 20D, ftff1@x, 100D<,8x, -Pi, Pi<, Ticks Ø 88-Pi, -Pi ê 2, 0, Pi ê 2, Pi<<, PlotStyle Ø88Thick, Red<, 8Thick, Blue<, 8Thick, Green<, 8Thick, Black<<, Exclusions Ø NoneD

-p -p

2

p

2p

0.2

0.4

0.6

0.8

1.0

Zooming in on the edge of the step, showing the 22, 102 and 502 term series.(Colors match those in the previous plot.)Note the oscillation near the edge with the fixed 9% overshoot (called the Gibbs phenomenon).

6 Fourier.nb

Page 7: Mathematica for Fourier Series and Transforms

Plot@8ff1@xD, ftff1@x, 20D, ftff1@x, 100D, ftff1@x, 500D<, 8x, Pi ê 2 - 0.1, Pi ê 2 + 0.1<,PlotRange Ø 88Pi ê 2 - .1, Pi ê 2 + 0.1<, 80.9, 1.1<<, PlotStyle Ø88Thick, Red<, 8Thick, Green<, 8Thick, Black<, 8Thick, Orange<<, Exclusions Ø NoneD

1.55 1.60 1.65

0.95

1.00

1.05

1.10

Using Complex Fourier Series

In[179]:= ft6ff1complex = FourierSeries@ff1@xD, x, 6D

Out[179]=1

2+‰-Â x

p+‰Â x

p-‰-3 Â x

3 p-‰3 Â x

3 p+‰-5 Â x

5 p+‰5 Â x

5 p

In[180]:= FullSimplify@ft6ff1complexD

Out[180]=15 p + 60 Cos@xD - 20 Cos@3 xD + 12 Cos@5 xD

30 p

General real function (neither odd nor even)

In[185]:= ff4@x_D := Piecewise@883 HMod@x, 2 Pi, -PiD + PiL ê Pi - 1, Mod@x, 2 Pi, -PiD < -Pi ê 2<,8-Mod@x, 2 Pi, -PiD ê Pi, Mod@x, 2 Pi, -PiD ¥ -Pi ê 2<<D

In[186]:= ff4plot = Plot@ff4@xD, 8x, -3 Pi, 3 Pi<, Ticks Ø 88-3 Pi, -2 Pi, -Pi, 0, Pi, 2 Pi, 3 Pi<<,PlotStyle Ø 8Thick, Red<, Exclusions Ø NoneD

Out[186]=

-3 p -2 p -p p 2 p 3 p

-1.0

-0.8

-0.6

-0.4

-0.2

0.2

0.4

Use “FourierTrigSeries” to get Cosine/Sine seriesHere is the result up to 6th order

Fourier.nb 7

Page 8: Mathematica for Fourier Series and Transforms

Use “FourierTrigSeries” to get Cosine/Sine seriesHere is the result up to 6th order

In[188]:= ft6ff4@x_D = FourierTrigSeries@ff4@xD, x, 6D

Out[188]= -1

4+4 Cos@xD

p2-2 Cos@2 xD

p2+4 Cos@3 xD

9 p2+

4 Cos@5 xD

25 p2-2 Cos@6 xD

9 p2-4 Sin@xD

p2+4 Sin@3 xD

9 p2-4 Sin@5 xD

25 p2

In[189]:= Plot@8ff4@xD, ft6ff4@xD<, 8x, -3 Pi, 3 Pi<,Ticks Ø 88-3 Pi, -2 Pi, -Pi, 0, Pi, 2 Pi, 3 Pi<<,PlotStyle Ø 88Thick, Red<, 8Thick, Blue<<, Exclusions Ø NoneD

Out[189]=

-3 p -2 p -p p 2 p 3 p

-1.0

-0.8

-0.6

-0.4

-0.2

0.2

0.4

Here’s the complex version:

In[191]:= ft6ff4complex = FourierSeries@ff4@xD, x, 6D

Out[191]= -1

4+H2 - 2 ÂL ‰-Â x

p2+H2 + 2 ÂL ‰Â x

p2-‰-2 Â x

p2-‰2 Â x

p2+J29+ 2 Â

9N ‰-3 Â x

p2+

J29- 2 Â

9N ‰3 Â x

p2+J

225

- 2 Â

25N ‰-5 Â x

p2+J

225

+ 2 Â

25N ‰5 Â x

p2-‰-6 Â x

9 p2-‰6 Â x

9 p2

In[192]:= FullSimplify@ft6ff4complexD

Out[192]=1

900 p2I3600 Cos@xD - 1800 Cos@2 xD + 400 Cos@3 xD + 144 Cos@5 xD -

25 I9 p2 + 8 Cos@6 xD + 144 Sin@xD - 16 Sin@3 xDM - 144 Sin@5 xDM

Even function with discontinuity in derivative

Here we consider an example with a discontinuity in derivative but not in the function itself.

ff3@x_D = Piecewise@88Cos@xD, -Pi ê 2 <= Mod@x, 2 Pi, -PiD < Pi ê 2<<D

Cos@xD - p

2§ Mod@x, 2 p, -pD < p

2

0 True

8 Fourier.nb

Page 9: Mathematica for Fourier Series and Transforms

ff3plot = Plot@ff3@xD, 8x, -3 Pi, 3 Pi<, Ticks Ø 88-3 Pi, -2 Pi, -Pi, 0, Pi, 2 Pi, 3 Pi<<,PlotStyle Ø 8Thick, Red<, Exclusions Ø NoneD

-3 p -2 p -p p 2 p 3 p

0.2

0.4

0.6

0.8

1.0

Starting with the Cos[2x] term, the number in the denominator is n^2-1.So the coefficients fall like 1/n^2, compared to the 1/n we saw with a discontinuity in the function itself. This is general.

ft6ff3@x_D = FourierCosSeries@ff3@xD, x, 6D

1

p+Cos@xD

2+2 Cos@2 xD

3 p-2 Cos@4 xD

15 p+2 Cos@6 xD

35 p

ft20ff3@x_D = FourierCosSeries@ff3@xD, x, 20D

1

p+Cos@xD

2+2 Cos@2 xD

3 p-2 Cos@4 xD

15 p+2 Cos@6 xD

35 p-2 Cos@8 xD

63 p+2 Cos@10 xD

99 p-

2 Cos@12 xD

143 p+2 Cos@14 xD

195 p-2 Cos@16 xD

255 p+2 Cos@18 xD

323 p-2 Cos@20 xD

399 p

ft100ff3@x_D = FourierCosSeries@ff3@xD, x, 100D;

From this view, 20 terms is enough to do a good job, with 6 showing oscillations:

ff3plot = Plot@8ff3@xD, ft6ff3@xD, ft20ff3@xD<,8x, -3 Pi, 3 Pi<, Ticks Ø 88-3 Pi, -2 Pi, -Pi, 0, Pi, 2 Pi, 3 Pi<<,PlotStyle Ø 88Thick, Red<, 8Thick, Blue<, 8Thick, Green<<, Exclusions Ø NoneD

-3 p -2 p -p p 2 p 3 p

0.2

0.4

0.6

0.8

1.0

Zooming in, we need > 100 terms to approach the discontinuity in derivative.But there is no Gibbs phenomenon here.

Fourier.nb 9

Page 10: Mathematica for Fourier Series and Transforms

Zooming in, we need > 100 terms to approach the discontinuity in derivative.But there is no Gibbs phenomenon here.

ff3plot = Plot@8ff3@xD, ft6ff3@xD, ft20ff3@xD, ft100ff3@xD<,8x, Pi ê 2 - 0.1, Pi ê 2 + 0.2<, PlotStyle Ø88Thick, Red<, 8Thick, Blue<, 8Thick, Green<, 8Thick, Black<<, Exclusions Ø NoneD

1.55 1.60 1.65 1.70 1.75

0.02

0.04

0.06

0.08

0.10

Integrals needed in discussion of Gibbs phenomenon (see lecture notes)Integrate@Sin@xD ê x, 8x, 0, Infinity<D ê Pi

1

2

Integrate@Sin@xD ê x, 8x, 0, Pi<D ê Pi êê N

0.58949

Showing that the sum indeed asymptotes to the integral above:

ss@N_D := Sum@Sin@H2 n + 1L Pi ê H2 HN + 1LLD ê H2 n + 1L,8n, 0, N<, Assumptions Ø 8N œ Integers, N > 0<D 2 ê Pi

ListPlot@Table@ss@nD, 8n, 5, 500<DD

100 200 300 400 500

0.589492

0.589494

0.589496

0.589498

0.589500

0.589502

Fourier transforms

10 Fourier.nb

Page 11: Mathematica for Fourier Series and Transforms

Fourier transforms

Section 7.12 problem number 10

This is one way of writing the function in Mathematica

In[193]:= f10@t_D = Piecewise@88-2 Ha + tL, -a § t < 0<, 82 Ha - tL, 0 < t < a<<D;

Plotting: note that the "/. a->1" command tells Mathematica to set the parameter a to 1 in the function.

In[194]:= f10plot = Plot@f10@tD ê. a Ø 1, 8t, -2, 2<, PlotStyle Ø 8Thick, Red<D

Out[194]=-2 -1 1 2

-2

-1

1

2

Obtaining the Fourier Transform. The "FourierParameters" must be set to match our conventions (see the Documentation for their defini-tions).The “Assumptions -> a>0” allows Mathematica to ignore the case where a<0 in which case, given the definitions above, the function vanishes.“FullSimplify” leads to a more compact answer than “Simplify”

In[200]:= ft10@alp_D = FullSimplify@FourierTransform@f10@tD, t, alp,FourierParameters Ø 8-1, -1<, Assumptions Ø a > 0DD

Out[200]=2 Â H-a alp + Sin@a alpDL

alp2 p

Since our function is odd, we can also use the "FourierSinTransform", which gives the imaginary part of the previous result

In[196]:= img10@alp_D = FourierSinTransform@f10@tD,t, alp, FourierParameters Ø 8-1, -1<, Assumptions Ø a > 0D

Out[196]=2 H-a alp + Sin@a alpDL

alp2 p

Fourier.nb 11

Page 12: Mathematica for Fourier Series and Transforms

In[197]:= Plot@img10@alpD ê. a Ø 1, 8alp, -15, 15<, PlotStyle Ø 8Thick, Blue<D

Out[197]=-15 -10 -5 5 10 15

-0.2

-0.1

0.1

0.2

Here is the inverse Fourier Transform which brings us back to the original function (expressed in a different way).

In[212]:= invft10@t_D = FullSimplify@InverseFourierTransform@ft10@alpD, alp, t, FourierParameters Ø 8-1, -1<DD

Out[212]= Ha - tL Sign@a - tD + 2 a Sign@tD - Ha + tL Sign@a + tD

In[213]:= Plot@invft10@tD ê. a Ø 1, 8t, -2, 2<, PlotStyle Ø 8Thick, Red<D

Out[213]=-2 -1 1 2

-2

-1

1

2

12 Fourier.nb