0

I need a non-singular way to evaluate $x\ln(\sin(x))$ as $x \to 0$. The limit is zero,but numerically I'm getting NaNs.

How can I find a rapidly computable approximation to $x\ln(\sin(x))$ as $x\to 0$? It appears from the graph that $-C\sqrt{x}$ might be a reasonable approximation, but I'm looking for something more formal.

user14717
  • 4,902

4 Answers4

1

The behaviour as $x\to 0+$ is $$ x\log (\sin x) = x\log x - \frac{1}{6}x^3 + \mathcal{O}(x^5 ). $$ You can derive this by considering $$ \log \left( {\frac{{\sin x}}{x}} \right). $$ This function is analytic near $x=0$, so it can be approximated by power series.

Gary
  • 31,845
1

You can replace $\sin x$ by $x$. But $x\ln(x)$ is asymptotic to... $x\ln x$, and nothing else.

There is no reason to get NaNs. Because even if $x$ is close to the smallest representable positive number, $|\ln x|$ is larger than $1$ and the product is representable.

Did you evaluate with negative $x$'s ?

  • $\ln(x)$ overflows. Did not evaluate with negative $x$. – user14717 Jun 03 '20 at 16:56
  • @user14717: there is no reason that it overflows. –  Jun 03 '20 at 16:57
  • @user14717: What numerical representation are you using ? What is the order of $x$ ? –  Jun 03 '20 at 17:00
  • @PeterForeman: $x^x$ would be computed by logarithms anyway. And there is no reason that $\ln x$ or $x\ln x$ overflows. –  Jun 03 '20 at 17:06
  • 1
    @PeterForeman: not at all. The smallest $x$ is a power of $2$, say $2^{-1022}\approx 2.22\cdot 10^{-324}$, the logarithm of which is $-708.396\cdots$. Not a really big number.

    In any case, if $x$ is representable, so are $\ln x$ and $x\ln x$.

    –  Jun 03 '20 at 17:12
0

A possible approach might be the following. Even if I have not checked any bounds of convergence for the error, which may actually be very loose and $x$ dependent.

Let $x>0$.

First notice that for $x\to 0$ your problem is equivalent to $x\log x$.

Let $y = 1+x$. Then $x\,\log x = (1-y)\log(1-y)$. On $(-1,1)$ you can write $$x\,\log x = -(1-y)\sum_{k=1}^\infty \frac{y^k}{k}\,.$$ It can be shown that such an expression tends to $0$ for $y\to 0$. So you can get $$x\,\log x = -x\sum_{k=1}^\infty \frac{(1-x)^k}{k}\,.$$ Note however that this is not a Taylor expansion around $x=0$. So that for the trunked sums, it is not true that the rest is of order $o(|x|^N)$.

You may eventually write $$x\,\log (\sin(x)) = -x\sum_{k=1}^\infty \frac{(1-\sin(x))^k}{k}\,.$$

ECL
  • 2,960
0

As written, the numerical problem comes from the order of operations $$x \qquad \sin(x) \qquad \log(\sin(x))\qquad x\log(\sin(x))$$ Writing $$x\log(\sin(x))=x \log(x)+ x \log \left(\frac{\sin (x)}{x}\right)$$ you can use $$\log \left(\frac{\sin (x)}{x}\right)=\sum_{n=1}^\infty (-1)^n \frac{2^{2 n-2} \left(B_{2 n}+B_{2 n}(1)\right)}{n \,(2 n)!}\,x^{2n}$$

For $x \in (0,\frac \pi 2)$, using the summation up to $n=4$ that is to say $$\log \left(\frac{\sin (x)}{x}\right)= -\frac{x^2}{6}-\frac{x^4}{180}-\frac{x^6}{2835}-\frac{x^8}{37800}+O\left(x^{10}\right)$$ is extremely good (maximum error of $2.47 \times 10^{-4}$).

You could make it much better (maximum error of $1.30 \times 10^{-6}$) using the Padé approximant $$\log \left(\frac{\sin (x)}{x}\right)=-x^2\,\frac{\frac{1}{6}-\frac{761 }{57420}x^2+\frac{457 }{3617460}x^4} {1-\frac{36 }{319}x^2+\frac{23 }{9570}x^4 }$$