condition number involving $\cos x$

1.6k Views Asked by At

The function $f(x)=\frac{1-\cos x}{x}$ is to be evaluated at $x\approx 0$.

a) Calculate the condition number of $f$ at $x$ and thus find out whether $f$ is well-conditioned or not.
b) Write $f$ in such a way that loss of significance doesn't occur at $x\approx 0$.

My problem is that $f$ is not defined at $x=0$, so $$ \kappa_f(x) = \left|\frac{x f'(x)}{f(x)}\right| = \frac{(\frac{1-\cos x}{x})'\cdot x}{\frac{1-\cos x}{x}}$$ is not defined at $x=0$. While it's true that $f$ is to be evaluated at $x\approx 0$, I still don't know how that can help me with this problem.

2

There are 2 best solutions below

1
On BEST ANSWER

There remains a few issues which have not be addressed by the previous answer or the comments.

Specifically, the condition number $\kappa_f(x)$ is not defined for $x=0$ and we are not obligated to consider this point. The fact that $\kappa_f(x)$ can be extended continuously to $x=0$ is convenient, but irrelevant. As stated by @PierreCarre, we say that $f$ is well-conditioned near $x=0$ because $\kappa_f(x)$ is bounded in a neighborhood of $0$.

Moreover, the notation used to state the original problem is unfortunate. Specifically, the use of $x\approx 0$ is not appropriate when the intention is to indicate small, nonzero values of $x$. The correct notation is $0<|x|<\delta$ where $\delta \ll 1$. This is not fault of the OP, but an abuse of notation which is common.


There are many different types of conditions numbers. The condition number $\kappa_f(x)$ is the so-called (component-wise) relative condition number of $f$ at the point $x$. Here the objective is to measure the sensitivity of the relative error $\frac{f(x+\Delta x)-f(x)}{f(x)}$ to relatively small changes $\Delta x$ of the input $x$. This precludes the possibility of $f(x)=0$ and invites the restriction that $x \not = 0$.

The exact definition of the relative condition number is as follows. Let $\kappa_f(x,\epsilon)$ be given by $$\kappa_f(x,\epsilon) = \sup \left\{ \frac{1}{\epsilon}\frac{|f(x+\Delta x) - f(x)|}{|f(x)|} \: : \: |\Delta x| \leq \epsilon |x| \right\}.$$ This is a nonnegative and increasing function of $\epsilon$. It follows that $$ \kappa_f(x) = \underset{\epsilon \rightarrow 0_+}{\lim} \kappa_f(x,\epsilon)$$ exists. If $|\Delta x| \leq \epsilon |x|$, then by the definition of $\kappa_f(x,\epsilon)$ we have $$ \frac{|f(x+\Delta x)-f(x)|}{|f(x)|} \leq \epsilon \kappa_f(x,\epsilon) $$ and for sufficiently small values of $\epsilon$ we have $$\kappa_f(x,\epsilon) \approx \kappa_f(x).$$


As stated by @WimC and @Lutz Lehmann it is possible to rewrite the function $f$ to avoid subtractive cancellation. The rewrites are necessarily problem dependent and you need to build your own library of techniques. Another useful procedure is to obtain the Taylor expansion of $f$ directly. We have $$\cos(x) = 1 - \frac{1}{2}x^2 + \frac{1}{24}x^4 + O(x^6).$$ It follows that $$f(x) = \frac{1}{2}x - \frac{1}{24}x^3 + O(x^5).$$ We will not suffer from subtractive cancellation here because the terms have vastly different magnitude for small values of $x$. The downside of this procedure is that we are left with the question of determining how many terms to include in the Taylor expansion.

0
On

As it was mentioned in the comments to the OP, $x=0$ is a removable singularity. Also, $K_f(x)$ is bounded in a neighborhood of $x=0$, so $f$ is actually well conditioned.

The loss of significance is related to stability, which will depend on the actual algorithm that you use to compute values of $f$. The original expression for $f$ suggests the following algorithm:

$$ z_1 = \cos x, \quad z_2 = 1-z_1, \quad z_3 = z_2/x.$$

If you expand the relative error in each step and introduce round-off errors, you get

\begin{align*} \varepsilon_{z_1} = &\frac{-x \sin x}{\cos x} \varepsilon_{x} + \varepsilon_{1}\\ \varepsilon_{z_2} = &\frac{z_1 (-1)}{1-z_1} \varepsilon_{z_1}+\varepsilon_{2}=\frac{-\cos x}{1-\cos x}\left(\frac{-x \sin x}{\cos x} \varepsilon_{x} + \varepsilon_{1}\right)+\varepsilon{_2}\\ =& \frac{x \sin x}{1-\cos x} \varepsilon_{x} +\frac{-\cos x}{1-\cos x} \varepsilon_{1} + \varepsilon_{2}\\ \varepsilon_{z_3} = & \cdots \end{align*}

Looking at the coefficients of $\varepsilon_1, \varepsilon_2, \varepsilon_3$ in the final expression of $\varepsilon_{z_3}$, you will see that not all of them are bounded near "x=0$, meaning that even small round-off errors in intermediate steps can lead to large relative errors in the final result. This means that this algorithm is unstable.

You need to reformulate the algorithm (look at the suggestions in the comments), and repeat thtis analysis in order to show that the new algorithm is stable.