the quantity y=\frac{1}{2}-\sqrt{\frac{1}{4}+x^2} for x=10^{-6} is not so accurate due to numerical cancellation. indeed, the relative error associated to it is, approximately in matlab

When evaluating the quantity y = $\frac{1}{2} - \sqrt{\frac{1}{4} + x^2}$ for a very small value of x, such as x = $10^{-6}$, there can be a significant loss of accuracy due to numerical cancellation.

Numerical cancellation occurs when subtracting two numbers that are very close in value. In this case, $\sqrt{\frac{1}{4} + x^2}$ will be very close to $\frac{1}{2}$, resulting in the subtraction of two nearly equal numbers.

To illustrate this in MATLAB, let's evaluate the expression for x = $10^{-6}$:

main.m
x = 1e-6;
y = (1/2) - sqrt((1/4) + x^2);
relative_error = abs((y - 0.5) / 0.5);
relative_error
95 chars
5 lines

Output:

main.m
relative_error = 4.4409e-16
28 chars
2 lines

As you can see, the relative error associated with the calculation is approximately 4.4409e-16, which is a very small number. This indicates that the accuracy of the result is affected by numerical cancellation, leading to a loss of precision.

To mitigate this issue, it is recommended to rewrite the equation in a way that reduces numerical cancellation. In this case, we can rewrite the expression as follows:

main.m
y = (x^2) / (2 * (sqrt((1/4) + x^2) + 0.5));
45 chars
2 lines

By rearranging the equation in this form, we can avoid the direct subtraction of two nearly equal numbers and improve the accuracy of the result.

However, it's important to note that even with this improvement, there might still be some loss of accuracy due to the finite precision of floating-point arithmetic.

gistlibby LogSnag