Why are these sums not the same?
2 views (last 30 days)
What is the difference between these two:
>> for J=0:99
>> S =
Why is there a different result?
Thank you for any help!
Roger Stafford on 16 Jan 2015
Edited: Roger Stafford on 16 Jan 2015
There is no reason they should be the same! In the for-loop method you are not doing a summation, as is done in the first method. All you obtain is the value of the last term, namely cos(0.5*pi)*99.
If you change the last method to
S = S + cos(0.5*pi)*j;
any difference between that value and the one for the first method will be due to differences in round off errors.
Ideally cos(0.5*pi) should be an exact zero, but both because pi cannot be represented exactly and because of errors in the cosine computation, it actually gives 6.1232e-17 as its value.