Having problems with function theta = acosfull(x,y) command

1 visualizzazione (ultimi 30 giorni)
Consider the inverse (arc) cosine function cos−1(z), where z = x / r, r = (x2 + y2 )1/2 in the x-y Cartesian coordinate plane and −1≤ z ≤ +1 . The MATLAB inverse sine function acos(z) returns values only in the range 0 ≤ cos−1(z) <π (i.e., only in the first and second quadrants); however, we want to have the value in the correct quadrant over the full range 0 ≤ cos−1(z) < 2π . Write a MATLAB function m-file to solve this problem. Your function must be defined as follows:
function theta = acosfull(x,y)
in which the x and y coordinates are the input arguments and theta is the output angle in radians in the correct quadrant over the range 0 < theta < 2*pi. You may assume that the values of the input arguments passed to your function are double precision real numbers. Your program should include trapping for invalid computation cases (e.g., x=y=0), in which event it should assign a value of 0 to theta and display an appropriate error message on the screen.
(NOTE: You may use the MATLAB built-in acos function in your code.)
This is the code I have so far:
function [theta] = acosfull(x,y)
if r==sqrt(x^2 + y^2);
z=x/r;
acos(z);
else y == x && y == 0
theta=0
disp('Error.')
end
I keep receiving the error message:
Undefined function 'acosfull' for input
arguments of type 'double'.

Risposte (3)

Roger Stafford
Roger Stafford il 12 Set 2013
Modificato: Roger Stafford il 12 Set 2013
In your code as it stands the variable 'r' is never defined, but matlab is asked to make computations with it. No wonder it gets upset.
Consider what it is about your inputs x and y that distinguishes between the first or second quadrants on the one hand and the fourth and third quadrants on the other hand. What aspect of the variable 'y' allows you to make this distinction - hint, hint!
As a side remark, matlab has a function 'atan2' which almost does everything being asked for, but I would imagine your teacher would frown on its use in this problem. You are clearly expected to use 'acos' but to use it in an appropriate way.

Jan
Jan il 12 Set 2013
Modificato: Jan il 12 Set 2013
While there are problems inside the function also, as Roger has explained already, the message:
Undefined function 'acosfull' for input arguments of type 'double'
means, that the corresponding M-file is neither found in the folders contains in Matlab's path nor in the current folder. Where did you save the M-file and how is it called?
Btw., due to the limited numerical precision, it is very unlikely that the condition r==sqrt(x^2 + y^2) is ever met. Better use a certain interval, e.g.:
if abs(r - sqrt(x^2 + y^2)) < 10 * eps(r)

theodore panagos
theodore panagos il 28 Ott 2018
Modificato: Walter Roberson il 28 Ott 2018
For acos(x,y) you can use the formula :
f(x,y)=pi-pi/2*(1+sgn(x))*(1-sgn(y^2))-pi/4*(2+sgn(x))*sgn(y) -sgn(abs(x)-abs(y))*sgn(x*y)*acos((abs(x)+abs(y))/sqrt(2*x^2+2*y^2))
x=x2-x1 and y=y2-y1

Community Treasure Hunt

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

Start Hunting!

Translated by