Getting wrong values for sin
조회 수: 20 (최근 30일)
이전 댓글 표시
Hello, I was trying to plot a sine wave but was getting wrong values for the sine values:
sin(pi)
ans =
1.2246e-16
But at the same time all the cosine values I was getting were correct:
cos(2*pi)
ans =
1
Where am I going wrong and what am I supposed to do?
댓글 수: 0
답변 (2개)
Stuart Song
2021년 2월 5일
Because in matlab, pi = 3.1416 so you won't get 0 for sin(pi). You can try sinpi(1).
댓글 수: 1
Steven Lord
2021년 2월 5일
Not exactly. The pi function does not return 3.1416. It is just displayed that way in the default display format.
format
x = pi
Changing the display format changes the value that is displayed though the number stored in memory does not change.
format longg
y = pi
x == y % true
But neither does pi return the exact value of the irrational number π. Your computer doesn't have enough memory to store all the (infinite number of) digits of π.
I agree with your suggestion of sinpi if you know you're computing the sine of a multiple of π. Another potential option would be to work in degrees and use sind.
[sin(pi);
sinpi(1);
sind(180)]
KALYAN ACHARJYA
2021년 2월 5일
You are not doing anything wrong here.
Have you see the complate long sin or any other trigonometric series, it all beacuse of long floting points calculation. Please see there are multiple threads related to floating number calulations issue
If you are insisted to get sin(pi) or cos(2pi) to be 0 and 1, you may try following way
>> pi_dat=radtodeg(pi)
pi_dat =
180
>> sind(pi_dat)
ans =
0
>> cosd(2*pi_dat)
ans =
1
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Trigonometry에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!