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