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