Message Boards Message Boards

0
|
2638 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
Share this post:

Is There an Issue with the LegendreP function?

I am trying to evaluate a sum over a large number of terms, each of which includes a Legendre polynomial. At fairly low numbers of the order parameter, I get enormous values for the polynomial.

I have created a simple Notebook to illustrate the issue. You can see that I can plot the Legendre polynomial for orders between 0 and 100

0 and get seemingly sensible answers. If I then do a single call for the final value, I get an error.

I am using MMA 13.2 on a MacBook Pro with Apple silicon.

Any suggestions will be much appreciated.

Thanks, David

Attachments:
POSTED BY: David Mackay
2 Replies

One way is:

n = 20000;
th = \[Pi]/7;
$MaxExtraPrecision = 10000;(* Increase this value if necessary.*)
N[LegendreP[n, th], 20]
(*-0.0059312818372221032034*)

Second way:

n = 2000000;
th = \[Pi]/7;
LegendrePPP[n_, z_] := (1/Pi) NIntegrate[(z - Sqrt[z^2 - 1] Cos[t])^n, {t, 0, Pi}, 
   WorkingPrecision -> 30, MaxRecursion -> 20]
LegendrePPP[n, th]

(*0.000475301944163975616743822092080*)

Third way is the best it's very fast:

 ClearAll["`*"]; Remove["`*"];
 S[n_, z_] = (Series[LegendreP[n, z], {n, Infinity, 1}] // Normal // Simplify)
 n = 10000000;(* Try: n = 10^100 *)
 th = \[Pi]/7;
 N[S[n, th], 20]
 (*-0.00018112363719941377947*)
POSTED BY: Mariusz Iwaniuk

Many thanks for all of these ideas.

The issue, it turns out, is that LegendreP, by default, attempts to return an answer in polynomial form. As the order increases, the coefficients in the polynomial get enormous and all sorts of numerical horrors appear.

Applying //N after the function call is not the solution, in fact the answer is to put the N wrapper around the argument as follows:

LegendreP [1000,N[x]]

which internally invokes a purely numerical solution using the recurrence relationship I imagine.

Once again, thank you for your prompt and helpful ideas. David

POSTED BY: David Mackay
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract