sin function strange problem

1 次查看(过去 30 天)
strunack
strunack 2012-11-28
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

采纳的回答

Jan
Jan 2012-11-28
编辑:Jan 2012-11-28
The command format controls the precision of the output. Try this to show more digits of the values:
p1 = 3.0058e+008
p2 = p1 + 1
format long g
p1
p2
format short g
p1
p2
  3 个评论
Jan
Jan 2012-11-28
编辑:Jan 2012-11-28
I claim it would help to enable "format long g", stop the program and display the value of p1. Or set a break point in the function and type:
sprintf('%.16g\n', p1 - 3.0058e8)
I'm sure, that this is not zero.
Remark: SIN is not accurate for such large input.
strunack
strunack 2012-11-29
your last remark is the actual answer I guess. sin should be calculated for radians around 2*pi
tks

请先登录,再进行评论。

更多回答(1 个)

Ilham Hardy
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 个评论
Jan
Jan 2012-11-28
编辑:Jan 2012-11-28
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
strunack 2012-11-29
Accepted! format long g shows both result similar. thank you.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by