1 ## Copyright (C) 2000-2012 Kai Habel
3 ## This file is part of Octave.
5 ## Octave is free software; you can redistribute it and/or modify it
6 ## under the terms of the GNU General Public License as published by
7 ## the Free Software Foundation; either version 3 of the License, or (at
8 ## your option) any later version.
10 ## Octave is distributed in the hope that it will be useful, but
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 ## General Public License for more details.
15 ## You should have received a copy of the GNU General Public License
16 ## along with Octave; see the file COPYING. If not, see
17 ## <http://www.gnu.org/licenses/>.
20 ## @deftypefn {Function File} {[@var{theta}, @var{r}] =} cart2pol (@var{x}, @var{y})
21 ## @deftypefnx {Function File} {[@var{theta}, @var{r}, @var{z}] =} cart2pol (@var{x}, @var{y}, @var{z})
22 ## @deftypefnx {Function File} {[@var{theta}, @var{r}] =} cart2pol (@var{c})
23 ## @deftypefnx {Function File} {[@var{theta}, @var{r}, @var{z}] =} cart2pol (@var{c})
24 ## @deftypefnx {Function File} {@var{p} =} cart2pol (@dots{})
26 ## Transform Cartesian to polar or cylindrical coordinates.
28 ## @var{theta} describes the angle relative to the positive x-axis.
29 ## @var{r} is the distance to the z-axis @w{(0, 0, z)}.
30 ## @var{x}, @var{y} (and @var{z}) must be the same shape, or scalar.
31 ## If called with a single matrix argument then each row of @var{c}
32 ## represents the Cartesian coordinate (@var{x}, @var{y} (, @var{z})).
34 ## If only a single return argument is requested then return a matrix
35 ## @var{p} where each row represents one polar/(cylindrical) coordinate
36 ## (@var{theta}, @var{phi} (, @var{z})).
37 ## @seealso{pol2cart, cart2sph, sph2cart}
40 ## Author: Kai Habel <kai.habel@gmx.de>
43 function [theta, r, z] = cart2pol (x, y, z)
45 if (nargin < 1 || nargin > 3)
50 if (ismatrix (x) && (columns (x) == 2 || columns (x) == 3))
59 error ("cart2pol: matrix input must have 2 or 3 columns [X, Y (, Z)]");
62 if (! ((ismatrix (x) && ismatrix (y))
63 && (size_equal (x, y) || isscalar (x) || isscalar (y))))
64 error ("cart2pol: arguments must be matrices of same size, or scalar");
67 if (! ((ismatrix (x) && ismatrix (y) && ismatrix (z))
68 && (size_equal (x, y) || isscalar (x) || isscalar (y))
69 && (size_equal (x, z) || isscalar (x) || isscalar (z))
70 && (size_equal (y, z) || isscalar (y) || isscalar (z))))
71 error ("cart2pol: arguments must be matrices of same size, or scalar");
76 r = sqrt (x .^ 2 + y .^ 2);
79 theta = [theta, r, z];
87 %! [t, r] = cart2pol (x, y);
88 %! assert (t, [0, 0, 0]);
94 %! [t, r] = cart2pol (x, y);
95 %! assert (t, [0, pi/4, pi/4], sqrt(eps));
96 %! assert (r, sqrt(2)*[0, 1, 2], sqrt(eps));
102 %! [t, r, z2] = cart2pol (x, y, z);
103 %! assert (t, [0, pi/4, pi/4], sqrt(eps));
104 %! assert (r, sqrt(2)*[0, 1, 2], sqrt(eps));
111 %! [t, r, z2] = cart2pol (x, y, z);
112 %! assert (t, [0, 0, 0], eps);
113 %! assert (r, x, eps);
120 %! [t, r, z2] = cart2pol (x, y, z);
121 %! assert (t, [0, 1, 1]*pi/2, eps);
122 %! assert (r, y, eps);
129 %! [t, r, z2] = cart2pol (x, y, z);
135 %! C = [0, 0; 1, 1; 2, 2];
136 %! P = [0, 0; pi/4, sqrt(2); pi/4, 2*sqrt(2)];
137 %! assert (cart2pol (C), P, sqrt(eps));
140 %! C = [0, 0, 0; 1, 1, 1; 2, 2, 2];
141 %! P = [0, 0, 0; pi/4, sqrt(2), 1; pi/4, 2*sqrt(2), 2];
142 %! assert (cart2pol (C), P, sqrt(eps));