Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider the inverse (arc) cosine function cos^-1(z), where z = x/r, r = (x^2 +

ID: 3119490 • Letter: C

Question

Consider the inverse (arc) cosine function cos^-1(z), where z = x/r, r = (x^2 + y^2)^1/2 in the x-y Cartesian coordinate plane and -1 lessthanorequalto z lessthanorequalto +1. The Matlab inverse sine function acos(z) returns values only in the range 0 lessthanorequalto cos^-1(z) lessthanorequalto pi (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 lessthanorequalto cos^-1(z) lessthanorequalto 2 pi. 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 lessthanorequalto theta lessthanorequalto 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. Name your submitted file acosfull.m.

Explanation / Answer

function theta = acosfull(x,y)

if x=0

if y < 0

theta = 3*pi/2

elseif y>0

theta = pi/2

else

theta = 0

error('both x and y are zero')

end

elseif x>0

if y < 0

theta = 2*pi - acos(x/sqrt(x^2 + y^2))

elseif y >0

theta = acos(x/sqrt(x^2 + y^2))

else

theta = 0

end

else

if y < 0

theta = 2*pi - acos(x/sqrt(x^2 + y^2))

elseif y >0

theta = acos(x/sqrt(x^2 + y^2))

else

theta = pi

end

end

OR IN SIMPLER WAY

function theta = acosfull(x,y)

if y < 0

theta = 2*pi - acos(x/sqrt(x^2 + y^2))

elseif or(y >0, x~=0)

theta = acos(x/sqrt(x^2 + y^2))

else

theta = 0

error("Both vales are zero")

end

end