Mathematica for Fourier Series and Transforms

advertisement
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, - 3 Pi, 3 Pi<, Ticks Ø 88- 3 Pi, - 2 Pi, - Pi, 0, Pi, 2 Pi, 3 Pi<<,
PlotStyle Ø 8Thick, Red<, Exclusions Ø NoneD
0.4
0.2
-3 p
-2 p
-p
p
2p
3p
-0.2
-0.4
Truncated Fourier Series---since odd can use "FourierSinSeries"
In[170]:=
ft6ff0 = FourierSinSeries@ff0@xD, x, 6D
2 Sin@xD
Out[170]=
p
+
2 Sin@3 xD
3p
+
2 Sin@5 xD
5p
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
2
Fourier.nb
ftff0@x, 2D
2 JSin@xD +
1
3
Sin@3 xD +
1
5
Sin@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
0.6
0.4
0.2
Out[160]=
-
-p
p
p
2
2
p
-0.2
-0.4
-0.6
Make animation showing convergence:
In[163]:=
Manipulate@stepplot@nD, 8n, 0, 20, 1<D
n
1
0.6
0.4
Out[163]=
0.2
-p
-
p
p
2
2
p
-0.2
-0.4
-0.6
Comparing 3, 21 and 101 term Fourier Series to original function
Fourier.nb
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
0.6
0.4
0.2
-
-p
p
p
2
2
p
-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.60
0.55
0.50
0.45
-0.10
-0.05
0.05
0.10
Using complex fourier series
In[174]:=
ft6ff0complex = FourierSeries@ff0@xD, x, 6D
 ‰- x
Out[174]=
p
-
 ‰Â x
p
+
 ‰-3  x
3p
-
 ‰3  x
3p
+
 ‰-5  x
5p
-
 ‰5  x
5p
Simplify doesn’t help, but FullSimplify brings result back into Sin form found above
In[178]:=
FullSimplify@ft6ff0complexD
2 H15 Sin@xD + 5 Sin@3 xD + 3 Sin@5 xDL
Out[178]=
15 p
3
4
Fourier.nb
Even "barrier" 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.
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
1.0
0.8
0.6
0.4
0.2
-3 p
-p
-2 p
p
2p
3p
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
3p
+
2 Cos@5 xD
5p
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
1.0
0.8
0.6
0.4
0.2
-3 p
-2 p
-p
p
2p
3p
Fourier.nb
5
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
3p
-
‰3 Â x
3p
+
‰-5 Â x
5p
+
‰5 Â x
5p
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
1.0
0.8
0.6
Out[166]=
0.4
0.2
-p
-
p
p
2
2
p
6
Fourier.nb
In[167]:=
Manipulate@barrierplot@nD, 8n, 0, 20, 1<D
n
3
1.0
0.8
Out[167]=
0.6
0.4
0.2
-
-p
p
p
2
2
p
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
1.0
0.8
0.6
0.4
0.2
-p
-
p
p
2
2
p
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).
Fourier.nb
7
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.10
1.05
1.00
0.95
1.55
1.60
1.65
Using Complex Fourier Series
In[179]:=
ft6ff1complex = FourierSeries@ff1@xD, x, 6D
1
Out[179]=
2
In[180]:=
+
‰-Â x
p
+
‰Â x
p
-
‰-3 Â x
3p
-
‰3 Â x
3p
+
‰-5 Â x
5p
+
‰5 Â x
5p
FullSimplify@ft6ff1complexD
15 p + 60 Cos@xD - 20 Cos@3 xD + 12 Cos@5 xD
Out[180]=
30 p
General real function (neither odd nor even)
In[185]:=
In[186]:=
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
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
0.4
0.2
-3 p
Out[186]=
-2 p
-p
p
2p
-0.2
-0.4
-0.6
-0.8
-1.0
Use “FourierTrigSeries” to get Cosine/Sine series
Here is the result up to 6th order
3p
8
Fourier.nb
Use “FourierTrigSeries” to get Cosine/Sine series
Here is the result up to 6th order
In[188]:=
Out[188]=
ft6ff4@x_D = FourierTrigSeries@ff4@xD, x, 6D
-
1
4
+
4 Cos@xD
p2
4 Cos@5 xD
25 p2
In[189]:=
-
-
2 Cos@2 xD
p2
2 Cos@6 xD
9 p2
-
+
4 Cos@3 xD
9 p2
4 Sin@xD
p2
+
+
4 Sin@3 xD
9 p2
4 Sin@5 xD
-
25 p2
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
0.4
0.2
-3 p
-p
-2 p
p
2p
3p
-0.2
Out[189]=
-0.4
-0.6
-0.8
-1.0
Here’s the complex version:
In[191]:=
Out[191]=
ft6ff4complex = FourierSeries@ff4@xD, x, 6D
-
1
4
+
H2 - 2 ÂL ‰-Â x
2
J9 -
p2
2Â
N
9
In[192]:=
+
J 25 -
p2
2Â
N
25
-
p2
2
‰-5 Â x
p2
‰-2 Â x
+
J 25 +
-
‰2 Â x
2Â
N
25
p2
p2
2
+
J9 +
2Â
N
9
‰-3 Â x
+
p2
‰5 Â x
-
‰-6 Â x
9 p2
-
‰6 Â x
9 p2
FullSimplify@ft6ff4complexD
1
Out[192]=
2
‰3 Â x
p2
+
H2 + 2 ÂL ‰Â x
900 p2
I3600 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
p
Cos@xD - 2 § Mod@x, 2 p, - pD <
0
True
p
2
Fourier.nb
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
1.0
0.8
0.6
0.4
0.2
-3 p
-p
-2 p
p
2p
3p
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
3p
-
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@12 xD
143 p
2 Cos@2 xD
+
-
2 Cos@4 xD
3p
2 Cos@14 xD
195 p
+
2 Cos@6 xD
-
2 Cos@8 xD
+
2 Cos@10 xD
15 p
35 p
63 p
99 p
2 Cos@16 xD 2 Cos@18 xD 2 Cos@20 xD
+
255 p
323 p
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
1.0
0.8
0.6
0.4
0.2
-3 p
-2 p
-p
p
2p
3p
Zooming in, we need > 100 terms to approach the discontinuity in derivative.
But there is no Gibbs phenomenon here.
9
10
Fourier.nb
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
0.10
0.08
0.06
0.04
0.02
1.55
1.60
1.65
1.70
1.75
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
0.589502
0.589500
0.589498
0.589496
0.589494
0.589492
100
200
Fourier transforms
300
400
500
Fourier.nb
11
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
2
1
Out[194]=
-2
-1
1
2
-1
-2
Obtaining the Fourier Transform.
The "FourierParameters" must be set to match our conventions (see the Documentation for their definitions).
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
2 Â H- a alp + Sin@a alpDL
Out[200]=
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
2 H- a alp + Sin@a alpDL
Out[196]=
alp2 p
12
Fourier.nb
In[197]:=
Plot@img10@alpD ê. a Ø 1, 8alp, - 15, 15<, PlotStyle Ø 8Thick, Blue<D
0.2
0.1
Out[197]=
-15
-10
-5
5
10
15
-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]:=
Out[212]=
In[213]:=
invft10@t_D = FullSimplify@
InverseFourierTransform@ft10@alpD, alp, t, FourierParameters Ø 8- 1, - 1<DD
Ha - tL Sign@a - tD + 2 a Sign@tD - Ha + tL Sign@a + tD
Plot@invft10@tD ê. a Ø 1, 8t, - 2, 2<, PlotStyle Ø 8Thick, Red<D
2
1
Out[213]=
-2
-1
1
-1
-2
2
Download