sin function strange problem
이전 댓글 표시
I have a strange problem that I do not understand. If I use sin(p1) inside a program and in command window, both gave different result.
case-1
% code
A=dlmread('data.dat',',',[1 0 639*60 22]);
p1=A(1,6)
whos p1
p1=2*pi*70e6*p1
whos p1
p2=sin(p1)
whos p2
end
when I run it in command window all p1, p2 shows double and result shows is
p1 = 3.0058e+008 p2 = 2.0422e-008
but in command window if i enter p1 as 3.0058e+008 then sin(p1) = -0.9611
why this difference. pls help
채택된 답변
추가 답변 (1개)
Ilham Hardy
2012년 11월 28일
Perhaps you don't use the same value of p1..
HINT:
sin(95000)
ans =
-0.9818
sin(9.5e4)
ans =
-0.9818
sin(95454)
ans =
-0.1506
sin(95545)
ans =
0.2545
댓글 수: 7
strunack
2012년 11월 28일
Ilham Hardy
2012년 11월 28일
No, you're not..
See below:
>> format long
>> pi
ans =
3.141592653589793
>> sin(3.141592653589793)
ans =
1.224646799147353e-016
>> sin(3.141)
ans =
5.926535550994539e-004
>> format short g
>> sin(3.141)
ans =
0.00059265
Jan
2012년 11월 28일
I do not see any problem here. Of course PI cannot be represented exactly using a limited precision only. Therefore sin(pi) cannot be exactly zero.
Please explain what results you are expecting. Currently I'm convinced that the problem is found in your expectations, but not in the calculations.
strunack
2012년 11월 28일
Ilham Hardy
2012년 11월 28일
Why do you think
p2=sin(3.0058e+008)
p2 =
-0.96106
is correct?
Are you really sure, that "p1" in your function is exactly 3.0058000000000000e8 and if so, how did you test it? Please note that tiny rounding errors must occur when the binary value is converted to decimal for the display. There are e.g. a lot of sin waves between 3.005800e8 and 3.005799e8, but with 5 digits precision both values are displayed equally.
I'm still convinced that the number inside the function is displayed with 5 digits only and that the following digits are not zero. Therefore I still think that "format long g" allows to see this difference, although it cannot "solve" it. But there is no way to "solve" it, because it is based on a misunderstanding only.
strunack
2012년 11월 29일
카테고리
도움말 센터 및 File Exchange에서 Special Values에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!