I want to compute $f(x)$to avoid loss of significance$$f(x)=\frac{1-\cos x}{x^2}$$
One way is writing Taylor series approximation for $\cos x$ about $x=0$. what about this approach:
$$f(x)=\frac{2\sin^2(\frac x2)}{x^2}$$
Is this ok ?
I want to compute $f(x)$to avoid loss of significance$$f(x)=\frac{1-\cos x}{x^2}$$
One way is writing Taylor series approximation for $\cos x$ about $x=0$. what about this approach:
$$f(x)=\frac{2\sin^2(\frac x2)}{x^2}$$
Is this ok ?
Yes, this is okay as the sine close to zero is computed as $xh(x^2)$, so that computing $\frac{\sin x}{x}$ is (almost) as exact as the implementation of $h(x^2)$. Plotting shows that indeed the original formula has large errors for $x\approx 10^{-8}$.
The difference plot to the 8th degree Taylor polynomial
shows that both alternative formulas have errors in the magnitude of the machine precision.
Better than Taylor series would be the $[2n,2n]$ Padé approximants. Foe example, for $n=2$ you will have $$f(x)=\frac{1-\cos (x)}{x^2}\sim\frac {65520-3780 x^2+59 x^4 } {131040+3360 x^2+34 x^4 } $$ Compared to a Taylor expansion, the difference is $$\text{Padé- Taylor}=\frac{127 }{87178291200}x^{10}+O\left(x^{12}\right)$$
For example, for $x=10^{-6}$, the difference between the exact and approximated values is $1.46\times 10^{-69}$.
Using the next approximant $(n=3)$,it would be $$f(x)=\frac{1-\cos (x)}{x^2}\sim\frac {5491886400-346666320 x^2+7038360 x^4-45469 x^6} {24(457657200+9249240 x^2+86030 x^4+389 x^6 )} $$ Compared to a Taylor expansion, the difference is $$\text{Padé- Taylor}=\frac{11321 }{438437062103040000}x^{14}+O\left(x^{16}\right)$$ For example, for $x=10^{-6}$, the difference between the exact and approximated values is $2.58\times 10^{-98}$.
You are already mindful of rounding errors, but that is not the only issue here.
In general, we seek to compute a target value $T$. We most then choose an approximation $A$ as well as an algorithm for evaluating $A$. In general, the computed value $\hat{A}$ of $A$ will be different from $A$. The total error is $$T-\hat{A} = (T - A) + (A - \hat{A}).$$ Ideally, the two contributions, i.e., the approximation error $T-A$ and the rounding error $A - \hat{A}$ should have the same size and the total error should be less than the user's tolerance $\tau$. Suppose $|T-\hat{A}| < \tau$.
when you say "compute" I assume you mean without using standardised functions, so how would you then continue from this $\sin^2$ term for which the series will not be as pleasant. In terms of using the first series we know that: $$1-\cos(x)=\sum_{n=1}^\infty\frac{(-1)^{n+1}}{(2n)!}x^{2n}$$ and so: $$f(x)=\sum_{n=1}^\infty\frac{(-1)^{n+1}}{(2n)!}x^{2(n-1)}$$ However if you mean to take values where $x$ is very small I think the second method is good as approximations will show the value at $x=0$
Yes, your formulation is perfect for the job. While the Taylor series approach is also good, using the half-angle sine is easier to program and equally accurate.
As a further exercise, apply the Taylor series expansion
$\sin(x/2)=(x/2)-(x/2)^3/3!+(x/2)^5/5!-...$
and square the results through the $x^6$ term. Compare this result with what the direct Taylor series expansion for $1-\cos x$ would give.