File: axes2ecc.m

package info (click to toggle)
octave-mapping 1.4.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,132 kB
  • sloc: objc: 1,860; python: 438; cpp: 219; makefile: 87; xml: 37; sh: 4
file content (93 lines) | stat: -rw-r--r-- 3,042 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
## Copyright (C) 2018-2022 Philip Nienhuis
##
## This program is free software; you can redistribute it and/or modify it
## under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 3 of the License, or (at
## your option) any later version.
##
## This program is distributed in the hope that it will be useful, but
## WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
## General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; see the file COPYING.  If not, see
## <http://www.gnu.org/licenses/>.

## -*- texinfo -*-
## @deftypefn {Function File} {} {@var{ecc} =} axes2ecc (@var{semimajor}, @var{semiminor})
## @deftypefnx {Function File} {} {@var{ecc} =} axes2ecc (@var{axes})
## Calculates the eccentricity from semimajor and semiminor axes.
##
## @var{semimajor} and @var{semiminor} are scalars or vectors of
## semimajor and semiminor axes, resp.  Alternatively, they can also be
## supplied as coordinate (row) vectors or a N2 matrix with each row
## consisting of a (semimajor, semiminor) pair.
##
## Output arg @var{ecc} is a scalar or column vector of eccentricities.
##
## Examples:
##
## Scalar input:
## @example
##    format long
##    axes2ecc (6378137, 6356752.314245)
##    => 0.0818191908429654
## @end example
##
## Row vector (semimajor, semiminor):
## @example
##  axes2ecc ([6378137, 6356752.314245])
##  =>  0.0818191908429654
## @end example
##
## Multivectors:
## @example
##    axes2ecc ([     71492, 66854; ...
##                  6378137, 6356752.314245])
##    ans =
##       0.3543163789650412
##       0.0818191908429654
## @end example
##
## @seealso{ecc2flat,flat2ecc}
## @end deftypefn

## Function supplied by anonymous contributor, see:
## https://savannah.gnu.org/patch/index.php?9492

function ecc = axes2ecc (semimajor, semiminor=[])

  if (nargin < 1)
    print_usage ();

  elseif (ischar (semimajor) || ischar (semiminor))
    error ("axes2ecc: input args must be numeric");

  elseif (nargin == 1)
    s = size (semimajor);
    if (s(2) != 2)
      error ("axes2ecc: Nx2 matrix expected for arg. #1");
    endif
    ecc = sqrt ((semimajor(:, 1) .^ 2 - semimajor(:, 2) .^ 2) ./ ...
                (semimajor(:, 1) .^ 2));
  else
    ecc = sqrt ((semimajor .^ 2 - semiminor .^ 2) ./ (semimajor .^ 2));
  endif

endfunction


%!test
%! semimajor = 6378137;
%! semiminor = 6356752.314245;
%! Earth = [ semimajor, semiminor ];
%! Jupiter = [ 71492 , 66854 ];
%! Planets = [ Jupiter ; Earth ];
%! assert (axes2ecc (semimajor, semiminor), 0.0818191908429654, 10e-12);
%! assert (axes2ecc (Earth), 0.0818191908429654, 10e-12);
%! assert (axes2ecc (Planets), [ 0.354316379; 0.081819190843 ], 10e-10);
%! assert (axes2ecc (Planets(:, 1), Planets(:, 2)), [ 0.354316379; 0.081819190843 ], 10e-10);

%!error <must be numeric> axes2ecc ("a", 1);
%!error <Nx2 matrix expected> axes2ecc ([1; 2]);