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{phi}, @var{r}] =} cart2sph (@var{x}, @var{y}, @var{z})
21 ## @deftypefnx {Function File} {[@var{theta}, @var{phi}, @var{r}] =} cart2sph (@var{C})
22 ## @deftypefnx {Function File} {@var{S} =} cart2sph (@dots{})
23 ## Transform Cartesian to spherical coordinates.
25 ## @var{theta} describes the angle relative to the positive x-axis.
26 ## @var{phi} is the angle relative to the xy-plane.
27 ## @var{r} is the distance to the origin @w{(0, 0, 0)}.
28 ## @var{x}, @var{y}, and @var{z} must be the same shape, or scalar.
29 ## If called with a single matrix argument then each row of @var{c}
30 ## represents the Cartesian coordinate (@var{x}, @var{y}, @var{z}).
32 ## If only a single return argument is requested then return a matrix
33 ## @var{s} where each row represents one spherical coordinate
34 ## (@var{theta}, @var{phi}, @var{r}).
35 ## @seealso{sph2cart, cart2pol, pol2cart}
38 ## Author: Kai Habel <kai.habel@gmx.de>
41 function [theta, phi, r] = cart2sph (x, y, z)
43 if (nargin != 1 && nargin != 3)
48 if (ismatrix (x) && columns (x) == 3)
53 error ("cart2sph: matrix input must have 3 columns [X, Y, Z]");
56 if (! ((ismatrix (x) && ismatrix (y) && ismatrix (z))
57 && (size_equal (x, y) || isscalar (x) || isscalar (y))
58 && (size_equal (x, z) || isscalar (x) || isscalar (z))
59 && (size_equal (y, z) || isscalar (y) || isscalar (z))))
60 error ("cart2sph: X, Y, Z must be matrices of the same size, or scalar");
65 phi = atan2 (z, sqrt (x .^ 2 + y .^ 2));
66 r = sqrt (x .^ 2 + y .^ 2 + z .^ 2);
69 theta = [theta, phi, r];
78 %! [t, p, r] = cart2sph (x, y, z);
79 %! assert (t, [0, pi/4, pi/4], eps);
80 %! assert (p, [0, 1, 1]*atan(sqrt(0.5)), eps);
81 %! assert (r, [0, 1, 2]*sqrt(3), eps);
87 %! [t, p, r] = cart2sph (x, y, z);
88 %! assert (t, [0, 1, 1] * pi/2, eps);
89 %! assert (p, [0, 1, 1] * pi/4, eps);
90 %! assert (r, [0, 1, 2] * sqrt(2), eps);
96 %! [t, p, r] = cart2sph (x, y, z);
97 %! assert (t, [0, 0, 0]);
98 %! assert (p, [0, 1, 1] * pi/4);
99 %! assert (r, [0, 1, 2] * sqrt(2));
105 %! [t, p, r] = cart2sph (x, y, z);
106 %! assert (t, [0, 1, 1] * pi/4);
107 %! assert (p, [0, 0, 0]);
108 %! assert (r, [0, 1, 2] * sqrt(2));
111 %! C = [0, 0, 0; 1, 0, 1; 2, 0, 2];
112 %! S = [0, 0, 0; 0, pi/4, sqrt(2); 0, pi/4, 2*sqrt(2)];
113 %! assert (cart2sph(C), S, eps);