]> Creatis software - CreaPhase.git/blob - octave_packages/financial-0.4.0/nper.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / financial-0.4.0 / nper.m
1 ## Copyright (C) 1995-1998, 2000, 2002, 2004-2007 Kurt Hornik <Kurt.Hornik@wu-wien.ac.at>
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {} nper (@var{r}, @var{p}, @var{a}, @var{l}, @var{method})
18 ## Return the number of regular payments of @var{p} necessary to
19 ## amortize @var{a} loan of amount @var{a} and interest @var{r}.
20 ##
21 ## The optional argument @var{l} may be used to specify an additional
22 ## lump-sum payment of @var{l} made at the end of the amortization time.
23 ##
24 ## The optional argument @var{method} may be used to specify whether
25 ## payments are made at the end (@var{"e"}, default) or at the beginning
26 ## (@var{"b"}) of each period.
27 ##
28 ## Note that the rate @var{r} is specified as a fraction (i.e., 0.05,
29 ## not 5 percent).
30 ## @seealso{pv, pmt, rate, npv}
31 ## @end deftypefn
32
33 function n = nper (r, p, a, l, m)
34
35   if (nargin < 3 || nargin > 5)
36     print_usage ();
37   endif
38
39   if (! (isscalar (r) && r > -1))
40     error ("nper: r must be a scalar > -1");
41   elseif (! isscalar (p))
42     error ("nper: p must be a scalar");
43   elseif (! isscalar (a))
44     error ("nper: a must be a scalar");
45   endif
46
47   if (nargin == 5)
48     if (! ischar (m))
49       error ("nper: `method' must be a string");
50     endif
51   elseif (nargin == 4)
52     if (ischar (l))
53       m = l;
54       l = 0;
55     else
56       m = "e";
57     endif
58   else
59     m = "e";
60     l = 0;
61   endif
62
63   if (strcmp (m, "b"))
64     p = p * (1 + r);
65   endif
66
67   q = (p - r * a) / (p - r * l);
68
69   if (q > 0)
70     n = - log (q) / log (1 + r);
71   else
72     n = Inf;
73   endif
74
75 endfunction