changelog shortlog tags changeset files revisions annotate raw

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
2##
3## This file is part of Octave.
4##
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.
9##
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.
14##
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/>.
18
19## -*- texinfo -*-
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}
27## @end deftypefn
28
29## Author: Kai Habel <kai.habel@gmx.de>
30## Adapted-by: jwe
31
32function [theta, phi, r] = cart2sph (x, y, z)
33
34 if (nargin != 3)
35 print_usage ();
36 endif
37
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)))
42
43 theta = atan2 (y, x);
44 phi = atan2 (z, sqrt (x .^ 2 + y .^ 2));
45 r = sqrt (x .^ 2 + y .^ 2 + z .^ 2);
46
47 else
48 error ("cart2sph: arguments must be matrices of same size, or scalar");
49 endif
50
51endfunction
52
53%!test
54%! x = [0, 1, 2];
55%! y = [0, 1, 2];
56%! z = [0, 1, 2];
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);
61
62%!test
63%! x = 0;
64%! y = [0, 1, 2];
65%! z = [0, 1, 2];
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);
70
71%!test
72%! x = [0, 1, 2];
73%! y = 0;
74%! z = [0, 1, 2];
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));
79
80%!test
81%! x = [0, 1, 2];
82%! y = [0, 1, 2];
83%! z = 0;
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));
88