scripts/general/cart2sph.m
| changeset 10289: |
4b124317dc38 |
| parent: | eb63fbe60fab |
| author: |
John W. Eaton <jwe@octave.org> |
| date: |
Tue Feb 09 20:58:55 2010 -0500 (50 minutes ago) |
| permissions: |
-rw-r--r-- |
| description: |
base_properties::set_children: account for hidden children |
1## Copyright (C) 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009 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## Transform Cartesian to spherical coordinates.
22## @var{x}, @var{y} and @var{z} must be the same shape, or scalar.
23## @var{theta} describes the angle relative to the positive x-axis.
24## @var{phi} is the angle relative to the xy-plane.
25## @var{r} is the distance to the origin (0, 0, 0).
26## @seealso{pol2cart, cart2pol, sph2cart}
29## Author: Kai Habel <kai.habel@gmx.de>
32function [theta, phi, r] = cart2sph (x, y, z)
38 if ((ismatrix (x) && ismatrix (y) && ismatrix (z))
39 && (size_equal (x, y) || isscalar (x) || isscalar (y))
40 && (size_equal (x, z) || isscalar (x) || isscalar (z))
41 && (size_equal (y, z) || isscalar (y) || isscalar (z)))
44 phi = atan2 (z, sqrt (x .^ 2 + y .^ 2));
45 r = sqrt (x .^ 2 + y .^ 2 + z .^ 2);
48 error ("cart2sph: arguments must be matrices of same size, or scalar");
57%! [t, p, r] = cart2sph (x, y, z);
58%! assert (t, [0, pi/4, pi/4], eps);
59%! assert (p, [0, 1, 1]*atan(sqrt(0.5)), eps);
60%! assert (r, [0, 1, 2]*sqrt(3), eps);
66%! [t, p, r] = cart2sph (x, y, z);
67%! assert (t, [0, 1, 1] * pi/2, eps);
68%! assert (p, [0, 1, 1] * pi/4, eps);
69%! assert (r, [0, 1, 2] * sqrt(2), eps);
75%! [t, p, r] = cart2sph (x, y, z);
76%! assert (t, [0, 0, 0]);
77%! assert (p, [0, 1, 1] * pi/4);
78%! assert (r, [0, 1, 2] * sqrt(2));
84%! [t, p, r] = cart2sph (x, y, z);
85%! assert (t, [0, 1, 1] * pi/4);
86%! assert (p, [0, 0, 0]);
87%! assert (r, [0, 1, 2] * sqrt(2));