Note: this question is not strictly related to Matlab or any other environment (even though I would prefer that you refer to my Matlab code).
I've n = 10000
equally spaced elements between -pi
and pi
in an array produced like this (in Matlab):
n = 10000;
x = linspace(-pi, pi, n);
Then I create a second array x_r
, which is basically contains the elements of x
rounded to the third decimal place:
x_r = chop(x, 3);
Then, I calculate the sin
of all elements of x
and I call the resulting array y
. I do the same for x_r
.
y = sin(x);
y_r = sin(x_r);
Now, I try to sum all elements of y
and then all elements of y_r
, that is:
s = sum(y);
s_r = sum(y_r);
If I try to print s
and s_r
with 100
decimal digits after the dot, I obtain these huge numbers (and probably the first one is not finished):
s = -0.0000000000003170828901848210175171876477281318270923932134408573801920283585786819458007812500000000
s_r = -0.0000000000001331495677603378169351344695314764976501464843750000000000000000000000000000000000000000
Now, I want to find the mean of y
and y_r
, which I save respectively in m
and m_r
:
m = mean(y);
m_r = mean(y_r);
If I try to print m
and m_r
with 100 decimal digits, I obtain other two huge numbers:
m = -0.0000000000000000317082890184820990794524483366356918770088064486473850012160369260527659207582473755
m_r = -0.0000000000000000133149567760337822432508902790402516613129923689416538035157344666004064492881298065
My question is (as the title of this question indicates) which of the two operations is more robust to digit cancellation, the sum
or the mean
operation? And why?
My guess is that the mean operation is less robust to digit cancellation because it does more calculations, but I'm not sure, and I would not know if this would even be a right explanation. From the results, I'm not able to deduce anything useful honestly.
sum
andmean
is that the latter involves a single additional division by a (in your case) moderate-sized number, so the stability properties can't be very different. Without knowing how Matlab implements themean
, it's hard to say much more -- but there are more stable approaches than the simple "sum-and-divide" approach, see, e.g., stackoverflow.com/questions/21918641/… $\endgroup$