loss-of-significance error

Reduce loss of significance error in the following equation by re-arranging terms:

$f_1(x) = \frac{1- \cos(x)}{x^2}$ , assuming $x$ is near $0$.

Let $f_2(x)$ be the function rewritten to reduce loss of error.

So far I have obtained

$f_1(x) = \frac{1- \cos(x)}{x^2} \bigg(\frac{1+\cos(x)}{1+\cos(x)}\bigg) = \frac{\sin^2(x)}{x^2(1+\cos(x))}$

But I'm having difficulty understanding how one would figure out one which steps are reducing error. Apparently the answer is $f_2(x) = \frac{1}{x^2}2 \sin^2{\frac{x}{2}}$ but I have don't know why or how that conclusion has been reached.

-

A pretty standard trig identity is $\sin^2(u)=\frac{1}{2}-\frac{1}{2}\cos(2u)$. It shouldn't be too hard to find that in a text book or on the internet somewhere. Let $u=x/2$ and rearrange terms and you get the given expression. You didn't say that you have a problem understanding how this reduces error, but I'm in an atypically garrulous mood so, the first expression, as x goes to zero, involves two mutually cancelling infinities, $1/x^2$ from the first term and $-1/x^2$ from the leading term of the $\cos()$ expansion. Re-writing as done here gets rid fo those terms before any division has to take place.

-
thanks. that's not really the trouble though. the trouble is that numerically one answer is superior to the other i.e. it returns more significant figures. i don't know how you would determine which function returns more sig-figs – franklin Feb 14 '13 at 0:33
But that's what bob is saying. In the original form, you are subtracting two large quantities, and that's where you lose significant figures. – Gerry Myerson Feb 14 '13 at 0:38
this may be an unrelated request but what would you suggest as a pre-requisite to numerical analysis? i feel that real analysis would be useful to study before numerical analysis. but my school only requires the calculus sequence along with elementary algorithms – franklin Feb 14 '13 at 0:45
@franklin, better to post this as a new question --- no one will look for it here. My answer would be that the people in the best position to answer this question are the faculty members at your school. – Gerry Myerson Feb 14 '13 at 12:28

Loss of significant digits is almost always due to subtracting almost equal numbers (for example, $x = 1.01$ and $y = 1.02$ to 3 digits, but $y - x = 0.01$ with 1 digit only). In your example, the problem is that when $x \approx 0$, $\cos x \approx 1$. So you try to get rid of that $1 - \cos x$, here multiplying by $1 + \cos x$. That factor has no problem, is is ${} \approx 2$

-

From the sine half-angle formula:

$$1-\cos{x} = 2 \sin^2{\left ( \frac{x}{2} \right )}$$

-

The problem comes from the $1-\cos x$ term. If $x$ is small, this is about $\frac {x^2}2$ from the Taylor series. But using six place decimal precision, if $x=0.01,\ \ \cos x \approx 0.999500$ Subtracting $1-0.999500=0.000500$ leaves only three places of accuracy. Using $\frac {x^2}2$ gives full accuracy as there is no cancellation- we know the answer is $0.000500000$. When you multiplied by $1+ \cos x$ and changed the $1-\cos^2 x$ to $\sin^2 x$ you analytically and exactly did the subtraction and got rid of the cancellation.

-