1

I am trying to make a programme to convert cartesian coordinates to polar coordinates.

My calculator returns arctan(1) as pi/4.

MATLAB on the other hand returns atan(1) as 0.7854.

How can I get MATLAB to return numbers like this as expressions of pi?

Miriam Farber
  • 16,192
  • 11
  • 51
  • 69
  • 1
    Is there a particular reason why you want it to be expressed in terms of `pi`? `pi/4 = 0.7854`. If it is your goal to use `arctan(1)` in numerical calculations, then you shouldn't care about how it is represented numerically - whether it's on your personal calculator or MATLAB. The physical quantity is equivalent. If you want to display it on a graph or on a figure for example, then that's different. Please specify what the end goal of this would be. – rayryeng Aug 18 '17 at 14:27
  • Mainly that its easier for me to identify what quadrant my answer falls into – Cyrus Wyett Aug 18 '17 at 14:33
  • 1
    Quadrants are split up into multiples of `pi/2 = 1.57`. Therefore, a value that is between 0 and 1.57 would be the first quadrant, a value between 1.57 and 3.14 would be the fourth quadrant, a value between -1.57 to 0 would be the second quadrant and finally a value between -3.14 to -1.57 is the third quadrant. I would recommend you know what quadrant it belongs to using numerical ranges rather than relying on conversion, such as the answer provided below. Relying on the answer below will give you performance hits if you are planning to run the code over a long period of time. – rayryeng Aug 18 '17 at 14:34
  • thanks, ill commit to learning them – Cyrus Wyett Aug 18 '17 at 14:37

2 Answers2

2

As you've stated this is just for a quick visual check, I would just divide by pi:

n = 0.7854;
disp(['n in terms of pi: ', num2str(n/pi), '*pi']); 
>> n in terms of pi: 0.25*pi

If this was something you wanted to often do, I would define some function on your local path like so

function [val, str] = wrtpi(n)
% Returns the value of n with respect to pi
    val = n/pi;
    % Could include some rounding checks here if you wanted to complicate things
    % ... *checks* ...
    str = ['n in terms of pi: ', num2str(n/pi), '*pi']; 
end

Then

n = 0.7854;
[val, str] = wrtpi(n)
>> val = 0.25
   str = n in terms of pi: 0.25*pi

You also say this is just for identifying radian quadrants, aside from learning them, you could also just have a simple function

function q = quadrant(n)
    Qs = pi*[0, 1/2, 1, 3/2];               % Quadrants
    q = find(Qs <= mod(n,2*pi), 1, 'last'); % Index within the quadrants
    % You could make this accept vector inputs using:
    % q = arrayfun(@(x) find(Qs <= mod(x,2*pi), 1, 'last'), n)
end

Then

quadrant(2*pi - 0.0001) % >> 4
quadrant(0.2)           % >> 1
quadrant(1.6)           % >> 2  

Note that using the symbolic math toolbox for something as simple as this will likely cause more complications and slowdown than it helps!

Wolfie
  • 21,886
  • 7
  • 23
  • 50
  • Cheers, not sure if that's the most elegant way to find the quadrant but it was the first logic which sprang to mind! – Wolfie Aug 18 '17 at 14:57
1

For some particular values of pi you can use symbolic numbers, for example:

atan(sym(1))
ans =
pi/4

asin(sym(3^.5/2))
ans =
pi/3

Note that this requires the Symbolic Math Toolbox.

halfer
  • 18,701
  • 13
  • 79
  • 158
il_raffa
  • 4,964
  • 96
  • 28
  • 32