Cos function with Array

조회 수: 13 (최근 30일)
Nhan Nguyen
Nhan Nguyen 2020년 10월 25일
댓글: Nhan Nguyen 2020년 10월 25일
Hi, I am using cos function to calculate the cosine of an array as following:
a = th4_s1(2); %second element of my "th4_s1" array, which equals 1.5708
b = cos(a); %Matlab gives answer as 6.123e-17
c = 1.5708;
d = cos(1.5708); %Matlab gives answer as -3.6732e-6
Why are they so different? How can I avoid this error?
Thank you.

채택된 답변

Walter Roberson
Walter Roberson 2020년 10월 25일
>> acos(6.123e-17)
ans =
1.5707963267949
>> acos(-3.6732e-6)
ans =
1.5707999999949
>> cos(1.5708)
ans =
-3.67320510334657e-06
>> acos(cos(1.5708))
ans =
1.5708
>> acos(cos(1.5708)) - 1.5708
ans =
0
In other words, your th4_s1(2) is not 1.5708: it only rounds to that for display purposes, and is closer to 1.50796 .
And that cos(1.5708) is not 6.123e-17 like you show: that value is cos(1.5707999999949) not cos(1.5708) .
My recommendation would be that you go into Preferences -> Command Window -> Text Display, and choose numeric display "long g", and also Preferences -> Variables -> Default array format: and select "long g" (this affects the Variable Browser)
  댓글 수: 1
Nhan Nguyen
Nhan Nguyen 2020년 10월 25일
Thank you so much.

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Array Geometries and Analysis에 대해 자세히 알아보기

태그

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by