You need to transform the interval (-pi/2, 0) into (pi/2, pi). For that purpose you can use mod
:
y = mod(atan(x), pi);
Another possibility: use atan2
. This lets you specify the sine and the cosine of the desired angle as separate inputs, and thus you can control the output interval. To obtain an output in (0, pi), the sine (first input of atan2
) should be positive. Therefore you can use abs(x)
as first input and move the sign of x
to the second input:
y = atan2(abs(x), sign(x));