13
Mathematica examples relevant to Legendre functions Legendre Polynomials are built in Here is Legendre’s equation, and Mathematica recognizes as being solved by Legendre polynomials (LegendreP) and the second solution (LegendreQ) DSolve@H1 - x^2L y''@xD - 2xy'@xD + n Hn + 1L y@xD 0, y@xD,xD 88y@xD C@1D LegendreP@n, xD + C@2D LegendreQ@n, xD<< Evaluating and plotting first few Legendre polynomials Table@8n, LegendreP@n, xD<, 8n, 0, 6<D TableForm 0 1 1 x 2 1 2 H- 1 + 3x 2 L 3 1 2 H- 3x + 5x 3 L 4 1 8 H3 - 30x 2 + 35x 4 L 5 1 8 H15x - 70x 3 + 63x 5 L 6 1 16 I- 5 + 105 x 2 - 315 x 4 + 231 x 6 M leg0to3 = Table@LegendreP@n, xD, 8n, 0, 3<D :1, x, 1 2 I- 1 + 3x 2 M, 1 2 I- 3x + 5x 3 M> Plot of first 4 Legendre Polynomials

Mathematica examples relevant to Legendre functionscourses.washington.edu/ph227814/228/nb/Legendre.nb.pdf1 8 x H15-70 x2 +63 x4L 1 8 H15 x-70 x3 +63 x5L 1 16 I-5+105 x2-315 x4 +231

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Mathematica examples relevant to Legendre functionscourses.washington.edu/ph227814/228/nb/Legendre.nb.pdf1 8 x H15-70 x2 +63 x4L 1 8 H15 x-70 x3 +63 x5L 1 16 I-5+105 x2-315 x4 +231

Mathematica examples relevant to Legendre

functions

Legendre Polynomials are built in

Here is Legendre’s equation, and Mathematica recognizes as being solved by Legendre polynomials

(LegendreP) and the second solution (LegendreQ)

DSolve@H1 - x^2L y''@xD - 2 x y'@xD + n Hn + 1L y@xD � 0, y@xD, xD88y@xD ® C@1D LegendreP@n, xD + C@2D LegendreQ@n, xD<<

Evaluating and plotting first few Legendre polynomials

Table@8n, LegendreP@n, xD<, 8n, 0, 6<D �� TableForm

0 1

1 x

21

2H-1 + 3 x2L

31

2H-3 x + 5 x3L

41

8H3 - 30 x2 + 35 x4L

51

8H15 x - 70 x3 + 63 x5L

61

16I-5 + 105 x2 - 315 x4 + 231 x6M

leg0to3 = Table@LegendreP@n, xD, 8n, 0, 3<D:1, x,

1

2

I-1 + 3 x2M,

1

2

I-3 x + 5 x3M>

Plot of first 4 Legendre Polynomials

Page 2: Mathematica examples relevant to Legendre functionscourses.washington.edu/ph227814/228/nb/Legendre.nb.pdf1 8 x H15-70 x2 +63 x4L 1 8 H15 x-70 x3 +63 x5L 1 16 I-5+105 x2-315 x4 +231

Plot@leg0to3, 8x, -1, 1<,PlotStyle ® 88Thick, Red<, 8Thick, Green<, 8Thick, Blue<, 8Thick, Black<<,LabelStyle ® "Medium", AxesLabel ® 8x, P<,PlotLabel ® "First 4 Legendre Polynomials"D

-1.0 -0.5 0.5 1.0

x

-1.0

-0.5

0.5

1.0

P

First 4 Legendre Polynomials

Normalization and orthogonality

LegendreP@n, 1D1

Integrate@LegendreP@1, xD LegendreP@3, xD, 8x, -1, 1<D0

The other Legendre solutions (which diverge at x=1, -1)

These are the second solutions to Legendre’s equation, usually denoted Q

LegendreQ@0, xD-1

2

Log@1 - xD +1

2

Log@1 + xD

LegendreQ@1, xD-1 + x -

1

2

Log@1 - xD +1

2

Log@1 + xD

LegendreQ@2, xD-3 x

2

+1

2

I-1 + 3 x2M -

1

2

Log@1 - xD +1

2

Log@1 + xD

LegendreQ@3, xD2

3

-5 x

2

2

-1

2

x I3 - 5 x2M -

1

2

Log@1 - xD +1

2

Log@1 + xD

2 RevisedLegendre.nb

Page 3: Mathematica examples relevant to Legendre functionscourses.washington.edu/ph227814/228/nb/Legendre.nb.pdf1 8 x H15-70 x2 +63 x4L 1 8 H15 x-70 x3 +63 x5L 1 16 I-5+105 x2-315 x4 +231

Plot@8LegendreQ@0, xD, LegendreQ@1, xD, LegendreQ@2, xD, LegendreQ@3, xD<, 8x, -1, 1<,PlotStyle ® 88Thick, Red<, 8Thick, Green<, 8Thick, Blue<, 8Thick, Black<<,PlotLabel ® "Second solutions l=0,1,2,3"D

-1.0 -0.5 0.5 1.0

-3

-2

-1

1

2

3

Second solutions l=0,1,2,3

Checking Generating Function

phi@h_, x_D := 1 � Sqrt@1 - 2 h x + h^2DSeries@phi@h, xD, 8h, 0, 6<D1 + x h + -

1

2

+3 x

2

2

h2

+ -3 x

2

+5 x

3

2

h3

+1

8

I3 - 30 x2

+ 35 x4M h

4+

1

8

I15 x - 70 x3

+ 63 x5M h

5+

1

16

I-5 + 105 x2

- 315 x4

+ 231 x6M h

6+ O@hD7

Compare to see that the coefficient on h^n is indeed P_n

Table@8SeriesCoefficient@phi@h, xD, 8h, 0, l<D �� Simplify, LegendreP@l, xD<,8l, 0, 6<D �� TableForm

1 1

x x

1

2H-1 + 3 x2L 1

2H-1 + 3 x2L

1

2x H-3 + 5 x2L 1

2H-3 x + 5 x3L

1

8H3 - 30 x2 + 35 x4L 1

8H3 - 30 x2 + 35 x4L

1

8x H15 - 70 x2 + 63 x4L 1

8H15 x - 70 x3 + 63 x5L

1

16I-5 + 105 x2 - 315 x4 + 231 x6M 1

16I-5 + 105 x2 - 315 x4 + 231 x6M

The Simple Examples of Legendre Polynomials in Physics

The a single charged particle’s 1

r potential can be written in terms of Legendre Prolynomials:

1

x -x'= S

l=0

l=¥ Hr'Ll

rl+1

PlHcosΓL where cosΓ is the angle between x (the position vector to the point of observation)

and x ' (the position vector to the point where the charge is located).

This series is only convergent for r>r’ (away from the source).

RevisedLegendre.nb 3

Page 4: Mathematica examples relevant to Legendre functionscourses.washington.edu/ph227814/228/nb/Legendre.nb.pdf1 8 x H15-70 x2 +63 x4L 1 8 H15 x-70 x3 +63 x5L 1 16 I-5+105 x2-315 x4 +231

1

x -x'= S

l=0

l=¥ Hr'Ll

rl+1

PlHcosΓL where cosΓ is the angle between x (the position vector to the point of observation)

and x ' (the position vector to the point where the charge is located).

This series is only convergent for r>r’ (away from the source).

A simple case is a point charge shifted off of the origin at point (-1,0).

Looking at the slice at y=0, cosΓ=-/+1 depending if you are on the right or the left of the point charge, in

other words cosΓ=-Sign[x].

Lastly we can simplify because the Legendre Polynomials are normalized PlH1L = 1 and are even/odd for

even/odd l. This means we can replace LegendreP[l,-Sign[x]] with just (-Sign[x])^l

ManipulateBPlotB: 1

Hx + 1L2

, SumB 1

x2

l+1

H-Sign@xDLl, 8l, 0, ll<F>,

8x, -10, 10<, PlotRange ® 8-.1, 2<, AspectRatio ® 1,

PlotLegends ® :" 1

- xÓ'

", " Sl=0

l=ll Hr'Ll

rl+1

PlHcosΓL">,

PlotStyle ® 8Orange, Blue<F, 8ll, 0, 20, 1<F

ll

-10 -5 5 10

0.5

1.0

1.5

2.0

1

x -x'

Sl=0

l=ll Hr'Ll

rl+1

PlHcosΓL

As we add higher moments the approximation becomes better in the region r>r’.

To see this in 2D:

4 RevisedLegendre.nb

Page 5: Mathematica examples relevant to Legendre functionscourses.washington.edu/ph227814/228/nb/Legendre.nb.pdf1 8 x H15-70 x2 +63 x4L 1 8 H15 x-70 x3 +63 x5L 1 16 I-5+105 x2-315 x4 +231

ManipulateBPlot3DB: 1

Hx + 1L2+ y

2

, SumB 1

x2

+ y2

l+1

LegendrePBl, -x

x2

+ y2

F, 8l, 0, ll<F>,

8x, -10, 10<, 8y, -10, 10<, AspectRatio ® 1,

PlotRange ® 80, .5<, PlotStyle ® 8Orange, Blue<,PlotLegends ® :" 1

- xÓ'

", " Sl=0

l=ll Hr'Ll

rl+1

PlHcosΓL">F, 8ll, 0, 10, 1<F

ll

-10

-5

0

5

10

-10

-5

0

5

10

0.0

0.2

0.41

x -x'

Sl=0

l=ll Hr'Ll

rl+1

PlHcosΓL

For a slightly more interesting example, we can look at a physical dipole, with a positive charge at (-1,0)

and a negative charge at (1,0). Again in a y=0 slice, adding the Legendre polynomial for each term by

term looks like

PlH-Sign@xDL - PlHSign@xDLFor any even l, this will vanish and for odd l it will equal -2*Sign[x]. This makes sense because the

charge distribution is odd along the x axis. Considering there is no net charge it makes sense there will

be no monopole term.

RevisedLegendre.nb 5

Page 6: Mathematica examples relevant to Legendre functionscourses.washington.edu/ph227814/228/nb/Legendre.nb.pdf1 8 x H15-70 x2 +63 x4L 1 8 H15 x-70 x3 +63 x5L 1 16 I-5+105 x2-315 x4 +231

ManipulateBPlotB: 1

Hx + 1L2

-1

Hx - 1L2

, SumB 1

x2

l+1

H-2 * Sign@xDL, 8l, 1, ll, 2<F>,

8x, -10, 10<, PlotRange ® 8-5, 5<, AspectRatio ® 1, PlotStyle ® 8Orange, Blue<,PlotLegends ® 8"Exact", "Sum up to ll"<F, 8ll, 1, 11, 2<F

ll

-10 -5 5 10

-4

-2

2

4

Exact

Sum up to ll

6 RevisedLegendre.nb

Page 7: Mathematica examples relevant to Legendre functionscourses.washington.edu/ph227814/228/nb/Legendre.nb.pdf1 8 x H15-70 x2 +63 x4L 1 8 H15 x-70 x3 +63 x5L 1 16 I-5+105 x2-315 x4 +231

ManipulateBPlot3DB: 1

Hx + 1L2+ y

2

-1

Hx - 1L2+ y

2

, SumB

1

x2

+ y2

l+1

LegendrePBl, -x

x2

+ y2

F - LegendrePBl, x

x2

+ y2

F , 8l, 1, ll, 2<F>,

8x, -10, 10<, 8y, -10, 10<, AspectRatio ® 1, PlotRange ® 8-.5, .5<,PlotStyle ® 8Orange, Blue<,PlotLegends ® 8"Exact", "Sum up to ll"<F, 8ll, 1, 11, 2<F

ll

-10

-5

0

5

10

-10

-5

0

5

10

-0.5

0.0

0.5

Exact

Sum up to ll

To get an idea of how quickly the sum converges, lets look at the Log of the absolute value of the

differences,

dipoleres@x_, y_, ll_D := LogBAbsB 1

Hx + 1L2+ y

2

-1

Hx - 1L2+ y

2

- SumB

1

x2

+ y2

l+1

LegendrePBl, -x

x2

+ y2

F - LegendrePBl, x

x2

+ y2

F , 8l, 1, ll, 2<FFF

RevisedLegendre.nb 7

Page 8: Mathematica examples relevant to Legendre functionscourses.washington.edu/ph227814/228/nb/Legendre.nb.pdf1 8 x H15-70 x2 +63 x4L 1 8 H15 x-70 x3 +63 x5L 1 16 I-5+105 x2-315 x4 +231

Manipulate@Plot3D@dipoleres@x, y, llD,8x, -10, 10<, 8y, -10, 10<, AspectRatio ® 1, PlotRange ® 8-40, 1<,PlotLegends ® 8"Log@Abs@Exact - Sum up to llDD"<D, 8ll, 1, 10, 2<D

ll

-10

-5

0

5

10

-10

-5

0

5

10

-40

-30

-20

-10

0

Log@Abs@Exact - Sum up to llDD

We see it converges quickly and tends to be a better approximation for larger r.

We can also see if we bring the two point charges closer to the origin, the l=1 dipole term becomes

exact. This is the same as if we were zooming out, observing from greater r.

8 RevisedLegendre.nb

Page 9: Mathematica examples relevant to Legendre functionscourses.washington.edu/ph227814/228/nb/Legendre.nb.pdf1 8 x H15-70 x2 +63 x4L 1 8 H15 x-70 x3 +63 x5L 1 16 I-5+105 x2-315 x4 +231

ManipulateBPlotB: 1

Hx + ΕL2

-1

Hx - ΕL2

,

Ε

x2

2

H-2 Sign@xDL>, 8x, -20, 20<,

PlotRange ® 8-3, 3<, AspectRatio ® 1, PlotStyle ® 8Orange, Blue<,PlotLegends ® 8"Exact", "Sum up to ll"<F, 8Ε, 10, 0<F

Ε

-20 -10 10 20

-3

-2

-1

1

2

3

Exact

Sum up to ll

Example of Legendre series for step function (Boas Sec. 9.1)

Here are the series coefficients.

Step function is implemented by integrating from 0 to 1.

In[332]:= c@n_D := Integrate@LegendreP@n, xD, 8x, 0, 1<D H2 n + 1L � 2

RevisedLegendre.nb 9

Page 10: Mathematica examples relevant to Legendre functionscourses.washington.edu/ph227814/228/nb/Legendre.nb.pdf1 8 x H15-70 x2 +63 x4L 1 8 H15 x-70 x3 +63 x5L 1 16 I-5+105 x2-315 x4 +231

In[333]:= Table@8n, c@nD<, 8n, 0, 7<D �� TableForm

Out[333]//TableForm=

01

2

13

4

2 0

3 -7

16

4 0

511

32

6 0

7 -75

256

In[334]:= legsum@x_, m_D := Expand@Sum@c@nD LegendreP@n, xD, 8n, 0, m<DDIn[335]:= legsum2@x_D = legsum@x, 2D

Out[335]=

1

2

+3 x

4

In[336]:= legsum7@x_D = legsum@x, 7DOut[336]=

1

2

+11025 x

4096

-40425 x

3

4096

+63063 x

5

4096

-32175 x

7

4096

In[337]:= legsum50@x_D = legsum@x, 50D;Caution! When Mathematica is working with large polynoimials with large coefficents that require exact

cancelations, sometimes Mathematica will not give the correct answer due to not working to high

enough numerical precision.

When working with a decimal,

In[338]:= [email protected][338]= 2.32813

When keeping it as a fraction

In[339]:= legsum50@19 � 20DOut[339]= 4412664084786418075117366953446267009373973852723086696484023480134262786�

618007554367814793 �4460149039706124628307143654529672301196083200000000000000000000000000�

000000000000000000000

In[340]:= % �� N

Out[340]= 0.989354

10 RevisedLegendre.nb

Page 11: Mathematica examples relevant to Legendre functionscourses.washington.edu/ph227814/228/nb/Legendre.nb.pdf1 8 x H15-70 x2 +63 x4L 1 8 H15 x-70 x3 +63 x5L 1 16 I-5+105 x2-315 x4 +231

Plot of approximation with up to P_2, P_7 and P_50

In[341]:= PlotB8legsum2@xD, legsum7@xD, legsum50@xD<, 8x, -.99, .99<, WorkingPrecision ® ¥,

PlotStyle ® 88Thick, Blue<, 8Thick, Green<, 8Thick, Red<<, PlotRange -> 8-1, 2<,LabelStyle ® "Large", PlotLabel ® "Legendre series for a step",

PlotLegends ® :" Sl=2

l=0

clPlHxL", " Sl=7

l=0

clPlHxL", " Sl=50

l=0

clPlHxL">F

Out[341]=

-1.0 -0.5 0.5 1.0

-1.0

-0.5

0.5

1.0

1.5

2.0

Legendre series for a step

Sl=2

l=0

clPlHxL

Sl=7

l=0

clPlHxL

Sl=50

l=0

clPlHxL

Lets play with this a little more (caution, very large values of l may cause Mathematica to slow down as

the lth

polynomial has ~l

2+ 1 terms (rounded down)

RevisedLegendre.nb 11

Page 12: Mathematica examples relevant to Legendre functionscourses.washington.edu/ph227814/228/nb/Legendre.nb.pdf1 8 x H15-70 x2 +63 x4L 1 8 H15 x-70 x3 +63 x5L 1 16 I-5+105 x2-315 x4 +231

In[342]:= ManipulateBPlotB8Evaluate@legsum@x, lDD<, 8x, -1, 1<, WorkingPrecision ® ¥,

PlotStyle ® 88Thick, Blue<, 8Thick, Green<, 8Thick, Red<<,PlotRange -> 8-1, 2<, PlotLegends ® " S

l=ll

l=0

clPlHxL"F, 8l, 0, 100, 1<F

Out[342]=

l

-1.0 -0.5 0.5 1.0

-1.0

-0.5

0.5

1.0

1.5

2.0

Sl=ll

l=0

clPlHxL

Integrate::ilim : Invalid integration variable or limitHsL in 8-0.999959, 0, 1<. �

Integrate::ilim : Invalid integration variable or limitHsL in 8-0.959143, 0, 1<. �

Integrate::ilim : Invalid integration variable or limitHsL in 8-0.918326, 0, 1<. �

General::stop : Further output of Integrate::ilim will be suppressed during this calculation. �

Associated Legendre polynomials

Pn

mHxL is given by LegendreP[n,m,x]

Note that Pn

0HxL is the same as PnHxL, and that Pn

mHxL and Pn

-mHxL are proportional.

tabassoc@n_D := Table@8n, m, LegendreP@n, m, xD<, 8m, -n, n<Dtabassoc@1D �� TableForm

1 -11-x2

2

1 0 x

1 1 - 1 - x2

12 RevisedLegendre.nb

Page 13: Mathematica examples relevant to Legendre functionscourses.washington.edu/ph227814/228/nb/Legendre.nb.pdf1 8 x H15-70 x2 +63 x4L 1 8 H15 x-70 x3 +63 x5L 1 16 I-5+105 x2-315 x4 +231

tabassoc@2D �� TableForm

2 -21

8H1 - x2L

2 -11

2x 1 - x2

2 01

2H-1 + 3 x2L

2 1 -3 x 1 - x2

2 2 -3 H-1 + x2L

Plot@8LegendreP@1, 0, xD, LegendreP@1, 1, xD<, 8x, -1, 1<,PlotStyle ® 88Thick, Blue<, 8Thick, Green<, 8Thick, Red<<,PlotLabel ® "Associated Legendres for n=1"D

-1.0 -0.5 0.5 1.0

-1.0

-0.5

0.5

1.0

Associated Legendres for n=1

Plot@8LegendreP@2, 0, xD, LegendreP@2, 1, xD, LegendreP@2, 2, xD<,8x, -1, 1<, PlotStyle ® 88Thick, Blue<, 8Thick, Green<, 8Thick, Red<<,PlotLabel ® "Associated Legendres for n=2"D

-1.0 -0.5 0.5 1.0

-1

1

2

3

Associated Legendres for n=2

RevisedLegendre.nb 13