changelog shortlog tags changeset files revisions annotate raw

scripts/general/pol2cart.m

changeset 10289: 4b124317dc38
parent:eb63fbe60fab
author: John W. Eaton <jwe@octave.org>
date: Tue Feb 09 20:58:55 2010 -0500 (44 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{x}, @var{y}] =} pol2cart (@var{theta}, @var{r})
21## @deftypefnx {Function File} {[@var{x}, @var{y}, @var{z}] =} pol2cart (@var{theta}, @var{r}, @var{z})
22## Transform polar or cylindrical to Cartesian coordinates.
23## @var{theta}, @var{r} (and @var{z}) must be the same shape, or scalar.
24## @var{theta} describes the angle relative to the positive x-axis.
25## @var{r} is the distance to the z-axis (0, 0, z).
26## @seealso{cart2pol, cart2sph, sph2cart}
27## @end deftypefn
28
29## Author: Kai Habel <kai.habel@gmx.de>
30## Adapted-by: jwe
31
32function [x, y, z] = pol2cart (theta, r, z)
33
34 if (nargin < 2 || nargin > 3)
35 error ("pol2cart: number of arguments must be 2 or 3");
36 endif
37
38 if (nargin == 2 && nargout > 2)
39 error ("pol2cart: number of output arguments must not be greater than number of input arguments");
40 endif
41
42 if ((ismatrix (theta) && ismatrix (r) && (nargin == 2 || ismatrix (z)))
43 && (size_equal (theta, r) || isscalar (theta) || isscalar (r))
44 && (nargin == 2 || size_equal (theta, z) || isscalar (theta) || isscalar (z))
45 && (nargin == 2 || size_equal (r, z) || isscalar (r) || isscalar (z)))
46
47 x = cos (theta) .* r;
48 y = sin (theta) .* r;
49
50 else
51 error ("pol2cart: arguments must be matrices of same size, or scalar");
52 endif
53
54endfunction
55
56%!test
57%! t = [0, 0.5, 1] * pi;
58%! r = 1;
59%! [x, y] = pol2cart (t, r);
60%! assert (x, [1, 0, -1], sqrt(eps));
61%! assert (y, [0, 1, 0], sqrt(eps));
62
63%!test
64%! t = [0, 1, 1] * pi/4;
65%! r = sqrt(2) * [0, 1, 2];
66%! [x, y] = pol2cart (t, r);
67%! assert (x, [0, 1, 2], sqrt(eps));
68%! assert (y, [0, 1, 2], sqrt(eps));
69
70%!test
71%! t = [0, 1, 1] * pi/4;
72%! r = sqrt(2) * [0, 1, 2];
73%! z = [0, 1, 2];
74%! [x, y, z2] = pol2cart (t, r, z);
75%! assert (x, [0, 1, 2], sqrt(eps));
76%! assert (y, [0, 1, 2], sqrt(eps));
77%! assert (z, z2);
78
79%!test
80%! t = 0;
81%! r = [0, 1, 2];
82%! z = [0, 1, 2];
83%! [x, y, z2] = pol2cart (t, r, z);
84%! assert (x, [0, 1, 2], sqrt(eps));
85%! assert (y, [0, 0, 0], sqrt(eps));
86%! assert (z, z2);
87
88%!test
89%! t = [1, 1, 1]*pi/4;
90%! r = 1;
91%! z = [0, 1, 2];
92%! [x, y, z2] = pol2cart (t, r, z);
93%! assert (x, [1, 1, 1] / sqrt(2), eps);
94%! assert (y, [1, 1, 1] / sqrt(2), eps);
95%! assert (z, z2);
96
97%!test
98%! t = 0;
99%! r = [1, 2, 3];
100%! z = 1;
101%! [x, y, z2] = pol2cart (t, r, z);
102%! assert (x, [1, 2, 3], eps);
103%! assert (y, [0, 0, 0] / sqrt(2), eps);
104%! assert (z, z2);
105