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

15 次查看(过去 30 天)
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?

采纳的回答

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

更多回答(2 个)

John D'Errico
John D'Errico 2021-12-16
编辑:John D'Errico 2021-12-16
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 2021-12-16
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.

标签

Community Treasure Hunt

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

Start Hunting!

Translated by