フィルターのクリア

ArcCosine function

18 ビュー (過去 30 日間)
Zelda Luxenburry
Zelda Luxenburry 2012 年 2 月 6 日
コメント済み: John D'Errico 2021 年 1 月 13 日
this is the code i have so far. i need to write a function m-file that can solve the arc cosine function for [0, 2pi]. it is giving me an error when i try acosfull(0,0), it does not give me the error....what am i missing?
function [theta] = acosfull(x,y)
%acossfull: find output angle of arccos over 0<theta<2*pi
% find value of acos(z) in the correct quadrant over the full range [0
% 2pi]
if y==x==0
theta=0
disp('Error.')
else r=sqrt(x^2 + y^2);
z=x/r;
acos(z)
end
  2 件のコメント
Victoria Walker
Victoria Walker 2020 年 10 月 29 日
One of my variables is a lowercase a, will this interfere?
Walter Roberson
Walter Roberson 2020 年 10 月 29 日
Not at all. MATLAB does not have any implicit multiplication, not anywhere, so there is no danger that acos(z) might be interpreted as a*cos(z)

サインインしてコメントする。

採用された回答

Walter Roberson
Walter Roberson 2012 年 2 月 6 日
y==x==0
is parsed as
((y==x)==0)
So it compares x to y to give 0 (false) or 1 (true), and then it compares that 0 or 1 to 0. Effectively what you coded is
if y ~= x
Try
if y == x && y == 0
  1 件のコメント
Zelda Luxenburry
Zelda Luxenburry 2012 年 2 月 6 日
perfect, thank you!!

サインインしてコメントする。

その他の回答 (2 件)

Wolfgang Garn
Wolfgang Garn 2013 年 8 月 8 日
編集済み: Wolfgang Garn 2013 年 8 月 8 日
In short theta = acos(x./sqrt(x.*x+y.*y)) + (y<0)*pi or below with a couple of tests.
function theta = acosfull(x,y)
%acossfull: find output angle of arccos over 0<theta<2*pi
% find value of acos(z) in the correct quadrant over the full range [0 2pi];
% acosfull requires as input x and y coordinates (length of x and y side of a rectangular triangle).
if nargin<1 % then give examples
disp('A series of examples:');
disp('45° = acosfull(2,2) radians: '); acosfull(2,2)
disp('90° = acosfull(0,3) radians: '); acosfull(0,3)
disp('135° = acosfull(-2,2) radians: '); acosfull(-2,2)
disp('180° = acosfull(-2,0) radians: '); acosfull(-2,0)
disp('225° = acosfull(-3,-3) radians: '); acosfull(-3,-3)
disp('270° = acosfull(0,-4) radians: '); acosfull(0,-4)
disp('315° = acosfull(2,-2) radians: '); acosfull(2,-2)
disp('360° = acosfull(2,-0.001) radians: '); acosfull(2,-0.001)
disp('NaN = acosfull(0,0): '); acosfull(0,0)
acosfull([2 -2 -2 -2 0], [2 2 -2 -2 0])
end
if nargin<2 % then display warning (and attempt original acos)
warning('acosfull requires as input x and y coordinates (or length of x and y side of a rectangular triangle).');
if nargin>0, theta = acos(x); end
else
theta = acos(x./sqrt(x.*x+y.*y)) + (y<0)*pi;
end
  3 件のコメント
Sai Zhou
Sai Zhou 2021 年 1 月 13 日
Nither of the answers works....
Steven Lord
Steven Lord 2021 年 1 月 13 日
Can you show us the code you ran and describe specifically what about the answers didn't work?
  • Do you receive warning and/or error messages? If so the full and exact text of those messages (all the text displayed in orange and/or red in the Command Window) may be useful in determining what's going on and how to avoid the warning and/or error.
  • Does it do something different than what you expected? If so, what did it do and what did you expect it to do?
  • Did MATLAB crash? If so please send the crash log file (with a description of what you were running or doing in MATLAB when the crash occured) to Technical Support using the telephone icon in the upper-right corner of this page so we can investigate.

サインインしてコメントする。


Danay Rosh
Danay Rosh 2019 年 4 月 7 日
arccos(pi/2)
  4 件のコメント
madhan ravi
madhan ravi 2019 年 4 月 10 日
Noted with care sir Walter.
John D'Errico
John D'Errico 2021 年 1 月 13 日
I might also add that the number acos(pi/2) will not even be a real number, but imaginary.
acos(pi/2)
ans =
0 + 1.0232i
The set of inputs for which acos will produce REAL results is limited to the interval [-1,1].

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by