How to compute y=1-cos(x) for x=10^(-5)
Contents
Unavoidable error
For
compute
.
The condition number is (using Taylor approximation)

Hence the unvoidable error is

Therefore we should be able to achieve about 16 digits of accuracy in Matlab if we use a "good" algorithm.
format long g % show results with 15 significant digits x = 1e-5; cf = x*sin(x)/(1-cos(x)) epsM = 1e-16; unavoid_error = abs(cf)*epsM + epsM
cf =
1.99999983448594
unavoid_error =
2.99999983448594e-16
Algorithm 1: "Naive evaluation" of y = 1-cos(x)
We compare yhat with the extra precision value ye and obtain a relative error of about
.
Since the actual error is much larger than the unavoidable error, algorithm 1 is numerically unstable.
Note that the computed value is larger than
, but the correct value is less than
.
x = 1e-5; yhat = 1-cos(x) % naive evaluation result xe = vpa('10^-5'); ye = vpa(1-cos(xe)) % extra precision result relerr = double((yhat-ye)/ye) % relative error of yhat
yhat =
5.00000041370185e-11
ye =
0.000000000049999999999583333333334722222277
relerr =
8.27487043331132e-08
Algorithm 2: Evaluate y = sin(x)^2/(1+cos(x))
We compare yhat with extra precision value ye and obtain a relative error of about
.
Since the actual error is not much larger than the unavoidable error, algorithm 2 is numerically stable.
x = 1e-5; yhat = sin(x)^2/(1+cos(x)) % using sin(x)^2+cos(x)^2=1 relerr = double((yhat-ye)/ye) % relative error of yhat
yhat =
4.99999999995833e-11
relerr =
4.20819139632208e-17
Algorithm 3: Evaluate y = 2 sin(x/2)^2
We compare yhat with the extra precision value ye and obtain a relative error of about
.
Since the actual error is not much larger than the unavoidable error, algorithm 3 is numerically stable.
x = 1e-5; yhat = 2*sin(x/2)^2 % using formula for cos(a+b) relerr = double((yhat-ye)/ye) % relative error of yhat
yhat =
4.99999999995833e-11
relerr =
4.20819139632208e-17
Algorithm 4: Approximate y by Taylor approximation p_3(x)
The Taylor series for f(x) = 1-cos(x) is

We use
. This introduces an approximation error: The absolute error
is bounded by

the relative error
is therefore bounded by

We compare yhat with the extra precision value ye and obtain a relative error of about
which is caused by the approximation error.
Since the actual error is much larger than the unavoidable error, algorithm 4 is numerically unstable (but it is much better than algorithm 1).
x = 1e-5; yhat = x^2/2 % Taylor approximation p_3(x) relerr = double((yhat-ye)/ye) % relative error of yhat
yhat =
5e-11
relerr =
8.33349901254303e-12
Algorithm 5: Approximate y by Taylor approximation p_5(x)
Now we use the Taylor approximation
. This introduces an approximation error: The absolute error
is bounded by

the relative error
is therefore bounded by

We compare yhat with the extra precision value ye and obtain a relative error of about
.
Since the actual error is not much larger than the unavoidable error, algorithm 5 is numerically stable.
x = 1e-5; yhat = x^2/2 - x^4/24 % Taylor approximation p_5(x) relerr = double((yhat-ye)/ye) % relative error of yhat
yhat =
4.99999999995833e-11
relerr =
1.71328884675708e-16