3
$\begingroup$

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!

$\endgroup$
4
  • $\begingroup$ If you change 30 to 300, the difference is even larger.. $\endgroup$
    – Ying Zhang
    Feb 5 '19 at 23:05
  • 1
    $\begingroup$ Please read the info for the tag bugs. The tag is to be added only after a community consensus is reached. $\endgroup$
    – Michael E2
    Feb 5 '19 at 23:18
  • $\begingroup$ Thanks for the correction, @MichaelE2 $\endgroup$
    – Ying Zhang
    Feb 11 '19 at 17:03
  • $\begingroup$ 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. $\endgroup$
    – J. M.'s torpor
    Feb 15 '19 at 3:08
6
$\begingroup$

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.

$\endgroup$
1
  • 3
    $\begingroup$ 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.) $\endgroup$
    – Michael E2
    Feb 5 '19 at 23:27
1
$\begingroup$

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*

![enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.