3191
|
1 ## Copyright (C) 1995, 1996, 1997 Kurt Hornik |
3426
|
2 ## |
3922
|
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. |
3426
|
9 ## |
3922
|
10 ## Octave is distributed in the hope that it will be useful, but |
3191
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
13 ## General Public License for more details. |
|
14 ## |
3191
|
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/>. |
3191
|
18 |
3407
|
19 ## -*- texinfo -*- |
3439
|
20 ## @deftypefn {Function File} {} nper (@var{r}, @var{p}, @var{a}, @var{l}, @var{method}) |
|
21 ## Return the number of regular payments of @var{p} necessary to |
|
22 ## amortize @var{a} loan of amount @var{a} and interest @var{r}. |
3191
|
23 ## |
3439
|
24 ## The optional argument @var{l} may be used to specify an additional |
|
25 ## lump-sum payment of @var{l} made at the end of the amortization time. |
3191
|
26 ## |
3439
|
27 ## The optional argument @var{method} may be used to specify whether |
|
28 ## payments are made at the end (@var{"e"}, default) or at the beginning |
|
29 ## (@var{"b"}) of each period. |
3191
|
30 ## |
3439
|
31 ## Note that the rate @var{r} is specified as a fraction (i.e., 0.05, |
|
32 ## not 5 percent). |
5642
|
33 ## @seealso{pv, pmt, rate, npv} |
3407
|
34 ## @end deftypefn |
3426
|
35 |
5428
|
36 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3458
|
37 ## Description: Number of payments needed for amortizing a loan |
3191
|
38 |
|
39 function n = nper (r, p, a, l, m) |
3426
|
40 |
6497
|
41 if (nargin < 3 || nargin > 5) |
6046
|
42 print_usage (); |
3191
|
43 endif |
3426
|
44 |
6497
|
45 if (! (isscalar (r) && r > -1)) |
3458
|
46 error ("nper: r must be a scalar > -1"); |
4030
|
47 elseif (! isscalar (p)) |
3458
|
48 error ("nper: p must be a scalar"); |
4030
|
49 elseif (! isscalar (a)) |
3458
|
50 error ("nper: a must be a scalar"); |
3191
|
51 endif |
|
52 |
|
53 if (nargin == 5) |
5443
|
54 if (! ischar (m)) |
3458
|
55 error ("nper: `method' must be a string"); |
3191
|
56 endif |
|
57 elseif (nargin == 4) |
5443
|
58 if (ischar (l)) |
3191
|
59 m = l; |
|
60 l = 0; |
|
61 else |
|
62 m = "e"; |
|
63 endif |
|
64 else |
|
65 m = "e"; |
|
66 l = 0; |
|
67 endif |
3426
|
68 |
3456
|
69 if (strcmp (m, "b")) |
3191
|
70 p = p * (1 + r); |
|
71 endif |
3426
|
72 |
3191
|
73 q = (p - r * a) / (p - r * l); |
3426
|
74 |
3191
|
75 if (q > 0) |
|
76 n = - log (q) / log (1 + r); |
|
77 else |
|
78 n = Inf; |
|
79 endif |
3426
|
80 |
3191
|
81 endfunction |
|
82 |