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