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

3 ビュー (過去 30 日間)
KostasK 2021 年 12 月 16 日
回答済み: Voss 2021 年 12 月 16 日
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
  1 件のコメント
Steven Lord
Steven Lord 2021 年 12 月 16 日
Many of the entries in the the Pythagorean identities section on this Wikipedia page use ±.


その他の回答 (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 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