# Legendre polynomials that evaluated with huge difference

I'm dealing with Legendre polynomials, involving the first kind, second kind, and the associated ones. However, I found this:

In[252]:= N[LegendreQ[30, 0, 3, Cosh[1]]]

Out[252]= -0.0681152


and:

In[251]:= LegendreQ[30, 0, 3, N[Cosh[1]]]

Out[251]= 1.18183*10^-14 + 0. I


I don't know why the result are so different.

I tried to plot the graph of LegendreQ[30, 0, 3, Cosh[x]] with $$x$$ from $$0$$ to $$3$$, and the graph showed me almost zero.

I don't know if this is a bug or what. Need help!

• If you change 30 to 300, the difference is even larger.. Feb 5, 2019 at 23:05
• Please read the info for the tag bugs. The tag is to be added only after a community consensus is reached. Feb 5, 2019 at 23:18
• Thanks for the correction, @MichaelE2 Feb 11, 2019 at 17:03
• In summary: LegendreQ[] automatically evaluates to a "simpler" representation for integer order and degree, which is not necessarily numerically stable for large arguments. Either use high precision or directly feed an inexact argument to LegendreQ[] at the outset. Feb 15, 2019 at 3:08

Use controlled precision, not machine numbers to check:

N[LegendreQ[30, 0, 3, Cosh[1]], 10]
(* 1.181831263*10^-14 *)
LegendreQ[30, 0, 3, N[Cosh[1], 10]]
(* 1.1818312625*10^-14 + 0.*10^-25 I *)


The essential problem is that LegendreQ[30, 0, 3, Cosh[1]] evaluates to a power series of degree 30 in Cosh[1], and power series are not generally numerically stable representations of polynomials. Thus, using N after the expansion without controlled precision is inaccurate.

LegendreQ apparently has a numerically stable method to use when fed a machine number. Thus, LegendreQ[30, 0, 3, N[Cosh[1]]] is accurate even without precision control.

• The OP's second machine-number computation LegendreQ[30, 0, 3, N[Cosh[1]]] is accurate, so I don't understand your first sentence. (Presumably a standard stable algorithm is used to compute LegendreQ on approximate numeric input.) Feb 5, 2019 at 23:27

John answer is the best up-vote here but want to add the trick is where to apply the N. If you have any number inside a function applied to to N, then make sure that any number inside the function ,NOT intended to be an integer, will have the N head applied

      LegendreQ[30, 0, 3, N[Cosh[1]]]
(* Any number not intended to be interger will need the head N *)
LegendreQ[30, 0, 3, Cosh[N@1]]
Plot[{LegendreQ[30, 0, 3, N[Cosh[N@x], 10]], Cosh[x]}, {x, -Pi/10000,
2 Pi/10000}, PlotLabels -> "Expressions"]


*Out[9]= 1.18183*10^-14 + 0. I

Out[10]= 1.18183*10^-14 + 0. I*