Mercurial > hg > octave-lyh
annotate scripts/general/cart2sph.m @ 9168:742cf6388a8f
Update section 17.7 (Coordinate Transformations) of arith.txi
author | Rik <rdrider0-list@yahoo.com> |
---|---|
date | Sat, 02 May 2009 07:20:35 -0700 |
parents | eb63fbe60fab |
children | a8ce6bdecce5 |
rev | line source |
---|---|
8920 | 1 ## Copyright (C) 2000, 2001, 2002, 2004, 2005, 2006, 2007, 2009 Kai Habel |
3803 | 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 | |
7016 | 7 ## the Free Software Foundation; either version 3 of the License, or (at |
8 ## your option) any later version. | |
3803 | 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 | |
7016 | 16 ## along with Octave; see the file COPYING. If not, see |
17 ## <http://www.gnu.org/licenses/>. | |
3803 | 18 |
19 ## -*- texinfo -*- | |
5610 | 20 ## @deftypefn {Function File} {[@var{theta}, @var{phi}, @var{r}] =} cart2sph (@var{x}, @var{y}, @var{z}) |
9168
742cf6388a8f
Update section 17.7 (Coordinate Transformations) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
21 ## Transform Cartesian to spherical coordinates. |
742cf6388a8f
Update section 17.7 (Coordinate Transformations) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
22 ## @var{x}, @var{y} and @var{z} must be the same shape, or scalar. |
742cf6388a8f
Update section 17.7 (Coordinate Transformations) of arith.txi
Rik <rdrider0-list@yahoo.com>
parents:
8920
diff
changeset
|
23 ## @var{theta} describes the angle relative to the positive x-axis. |
8491
aaff46fef256
[docs] fix hypenation: x - axis => x-axis, etc
Brian Gough <bjg@gnu.org>
parents:
7017
diff
changeset
|
24 ## @var{phi} is the angle relative to the xy-plane. |
3803 | 25 ## @var{r} is the distance to the origin (0, 0, 0). |
5642 | 26 ## @seealso{pol2cart, cart2pol, sph2cart} |
3803 | 27 ## @end deftypefn |
28 | |
29 ## Author: Kai Habel <kai.habel@gmx.de> | |
30 ## Adapted-by: jwe | |
31 | |
8533
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
32 function [theta, phi, r] = cart2sph (x, y, z) |
3803 | 33 |
34 if (nargin != 3) | |
6046 | 35 print_usage (); |
3803 | 36 endif |
37 | |
8533
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
38 if ((ismatrix (x) && ismatrix (y) && ismatrix (z)) |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
39 && (size_equal (x, y) || isscalar (x) || isscalar (y)) |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
40 && (size_equal (x, z) || isscalar (x) || isscalar (z)) |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
41 && (size_equal (y, z) || isscalar (y) || isscalar (z))) |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
42 |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
43 theta = atan2 (y, x); |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
44 phi = atan2 (z, sqrt (x .^ 2 + y .^ 2)); |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
45 r = sqrt (x .^ 2 + y .^ 2 + z .^ 2); |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
46 |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
47 else |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
48 error ("cart2sph: arguments must be matrices of same size, or scalar"); |
3803 | 49 endif |
50 | |
8533
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
51 endfunction |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
52 |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
53 %!test |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
54 %! x = [0, 1, 2]; |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
55 %! y = [0, 1, 2]; |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
56 %! z = [0, 1, 2]; |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
57 %! [t, p, r] = cart2sph (x, y, z); |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
58 %! assert (t, [0, pi/4, pi/4], eps); |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
59 %! assert (p, [0, 1, 1]*atan(sqrt(0.5)), eps); |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
60 %! assert (r, [0, 1, 2]*sqrt(3), eps); |
3803 | 61 |
8533
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
62 %!test |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
63 %! x = 0; |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
64 %! y = [0, 1, 2]; |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
65 %! z = [0, 1, 2]; |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
66 %! [t, p, r] = cart2sph (x, y, z); |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
67 %! assert (t, [0, 1, 1] * pi/2, eps); |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
68 %! assert (p, [0, 1, 1] * pi/4, eps); |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
69 %! assert (r, [0, 1, 2] * sqrt(2), eps); |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
70 |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
71 %!test |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
72 %! x = [0, 1, 2]; |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
73 %! y = 0; |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
74 %! z = [0, 1, 2]; |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
75 %! [t, p, r] = cart2sph (x, y, z); |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
76 %! assert (t, [0, 0, 0]); |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
77 %! assert (p, [0, 1, 1] * pi/4); |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
78 %! assert (r, [0, 1, 2] * sqrt(2)); |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
79 |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
80 %!test |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
81 %! x = [0, 1, 2]; |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
82 %! y = [0, 1, 2]; |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
83 %! z = 0; |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
84 %! [t, p, r] = cart2sph (x, y, z); |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
85 %! assert (t, [0, 1, 1] * pi/4); |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
86 %! assert (p, [0, 0, 0]); |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
87 %! assert (r, [0, 1, 2] * sqrt(2)); |
fb1b87ea4af9
Permit scalars when transforming coordinates.
Ben Abbott <bpabbott@mac.com>
parents:
8491
diff
changeset
|
88 |