Why is sec(theta) different than sqrt(1+tan(theta).^2) in MATLAB?

15 Ansichten (letzte 30 Tage)
KostasK
KostasK am 16 Dez. 2021
Beantwortet: Voss am 16 Dez. 2021
Hi all,
I just run into an odd problem; we all know the trigonometric identity . I use this identity in my code where I have a vector of angles of which I would like to calculate the secant of:
Psi = [0 320 40 280 80 240 120 200 160]*pi/180 ; % [rad]
x = [sec(Psi) ; sqrt(1 + tan(Psi).^2)] ;
>> x =
1.0000 1.3054 1.3054 5.7588 5.7588 -2.0000 -2.0000 -1.0642 -1.0642
1.0000 1.3054 1.3054 5.7588 5.7588 2.0000 2.0000 1.0642 1.0642
Above, I would expect both row vectors to be identical however it is visible that some numbers are returned as negative by the sec and postitive by the equivalent identity.
As a result I would like to ask: 1. why does this discrepancy exist to begin with 2. which one is correct?

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 16 Dez. 2021
That is not a correct statement of the trig identity. The trig identity states that but that does not mean that -- it means that

Weitere Antworten (2)

John D'Errico
John D'Errico am 16 Dez. 2021
Bearbeitet: John D'Errico am 16 Dez. 2021
First, what you have written is NOT an identity. The identity is
sec(theta)^2 = 1 + tan(theta)^2
There IS a difference! You should remember that taking the square root is not valid there, because there can be a problem with the sign.
So this is not a problem in MATLAB, but a problem in your mathematics. Which one is correct? What is correct is to use the correct identity.

Voss
Voss am 16 Dez. 2021
sqrt(x) returns the positive square root of a real number x. For example, sqrt(4) returns 2, but -2 is also a square root of 4.
sec(x) returns the secant of the angle x, which will be positive or negative depending on which quadrant x is in.
So to answer your questions: 1. the discrepancy is due to the sqrt function picking the positive square root. 2. they are both correct.
The identity in question is more correctly written as sec(x)^2 == 1 + tan(x)^2 (the square of both sides of the one you stated), which does not have the ambiguity due to the sqrt.

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by