Analyze error for \(y = 1 - \cos x\)

We want to compute \(f(x)=1-\cos x\) for \(x = 2\cdot 10^{-6}\).

We use the Matlab code

      x=2e-6; y=1-cos(x)

This means we first compute \(y_1:=\cos x\) and then \(y:=1-y_1\). Here the second step causes a large error because of subtractive cancellation. The error propagation formula for \(z=x+y\) is \[ |\epsilon_z| \le \left|\frac{x}{x+y}\right| \cdot |\epsilon_x| + \left|\frac{y}{x+y}\right| \cdot |\epsilon_y| \] Applying this to \(y:=1-y_1\) gives \[ |\epsilon_y| \le \left|\frac{1}{1-y_1}\right| \cdot 0 + \left|\frac{y_1}{1-y_1}\right| \cdot |\epsilon_{y_1}| \] (as the number 1 is represented exactly).

Since \(y_1=1-\cos x \approx 1-\frac{x^2}{2}\) we have \[ \left|\frac{y_1}{1-y_1}\right| \approx \frac{1}{x^2/2} = \frac{2}{x^2} = \tfrac{1}{2}\cdot 10^{12} \] If \( |\epsilon_{y_1}| \le 10^{-16} \) we obtain \[ |\epsilon_y| \le \tfrac{1}{2}\cdot 10^{12} \cdot 10^{-16} = 5 \cdot 10^{-5}. \]

Note that we obtained in Matlab an actual error \(|\epsilon_y| = 2.21 \cdot 10^{-5} \).