필터 지우기
필터 지우기

Why are these sums not the same?

조회 수: 1 (최근 30일)
Steve
Steve 2015년 1월 16일
편집: Roger Stafford 2015년 1월 16일
Hello together,
What is the difference between these two:
1:
>>sum(cos(0.5*pi)*(0:99))
ans =
3.0310e-13
and 2:
>>S=0;
>> for J=0:99
S=cos(0.5*pi)*j;
end;
>> S =
6.0620e-15
Why is there a different result?
Thank you for any help!

채택된 답변

Roger Stafford
Roger Stafford 2015년 1월 16일
편집: Roger Stafford 2015년 1월 16일
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.

추가 답변 (1개)

Image Analyst
Image Analyst 2015년 1월 16일

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

태그

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by