why sin(x) is different to sqrt(1-cos(x).^2)

1 view (last 30 days)
Dear, I'm new at matlab programming
I do not undestand why the following results s and s2 are different:
teta=0:pi/10:2*pi;
s=sin(teta);
s2=sqrt(1-cos(teta).^2);
sum(abs(s-s2))
The answer is
ans =
12.6275
But I think that the result should be 0 because sin(x)=sqrt(1-cos(x)^2) Why am I wrong?, where is my mistake?
Sorry for this question and thanks in advance
  1 Comment
Luis Isaac
Luis Isaac on 30 Dec 2015
Sorry for the question: The answer is the absolute value: sqrt(x^2)~=x and sqrt(x^2)==abs(x)!!!

Sign in to comment.

Accepted Answer

Walter Roberson
Walter Roberson on 30 Dec 2015
sum(sin(teta).^2 + cos(teta).^2 -1) is 0 to within roundoff error.
What you are missing is the adjustment for sign, because sqrt() returns the positive square root only

More Answers (0)

Categories

Find more on Mathematics in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!