3191
|
1 ## Copyright (C) 1995, 1996, 1997 Kurt Hornik |
|
2 ## |
|
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. |
|
7 ## |
|
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 |
|
11 ## General Public License for more details. |
|
12 ## |
|
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 -*- |
|
18 ## @deftypefn {Function File} {} nper (@var{r}, @var{p}, @var{a} [, @var{l}] [, @var{method}]) |
3191
|
19 ## |
3407
|
20 ## Computes the number of regular payments of @var{p} necessary to amortize @var{a} |
|
21 ## loan of amount @var{a} and interest @var{r}. |
3191
|
22 ## |
3407
|
23 ## With the optional scalar argument @var{l}, one can specify an additional |
|
24 ## lump-sum payment of @var{l} made at the end of the amortization time. With |
3191
|
25 ## the optional string argument `method', one can specify whether |
|
26 ## payments are made at the end ("e", default) or at the beginning ("b") |
|
27 ## of each period. |
|
28 ## |
|
29 ## Note that the rate r is not specified in percent, i.e., one has to |
|
30 ## write 0.05 rather than 5 %. |
3407
|
31 ## @end deftypefn |
|
32 ## @seealso{pv, pmt, rate, npv} |
3191
|
33 |
|
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) |
|
38 |
|
39 if ((nargin < 3) || (nargin > 5)) |
|
40 usage ("nper (r, p, a [, l] [, method])"); |
|
41 endif |
|
42 |
|
43 if !(is_scalar (r) && (r > -1)) |
|
44 error ("nper: r must be a scalar > -1"); |
|
45 elseif !is_scalar (p) |
|
46 error ("nper: p must be a scalar"); |
|
47 elseif !is_scalar (a) |
|
48 error ("nper: a must be a scalar"); |
|
49 endif |
|
50 |
|
51 if (nargin == 5) |
|
52 if !isstr (m) |
|
53 error ("nper: `method' must be a string"); |
|
54 endif |
|
55 elseif (nargin == 4) |
|
56 if isstr (l) |
|
57 m = l; |
|
58 l = 0; |
|
59 else |
|
60 m = "e"; |
|
61 endif |
|
62 else |
|
63 m = "e"; |
|
64 l = 0; |
|
65 endif |
|
66 |
|
67 if strcmp (m, "b") |
|
68 p = p * (1 + r); |
|
69 endif |
|
70 |
|
71 q = (p - r * a) / (p - r * l); |
|
72 |
|
73 if (q > 0) |
|
74 n = - log (q) / log (1 + r); |
|
75 else |
|
76 n = Inf; |
|
77 endif |
|
78 |
|
79 endfunction |
|
80 |