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 |
3191
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## 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 |
3922
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, USA. |
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). |
3407
|
34 ## @end deftypefn |
5053
|
35 ## |
3408
|
36 ## @seealso{pv, pmt, rate, and npv} |
3426
|
37 |
3458
|
38 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
39 ## Description: Number of payments needed for amortizing a loan |
3191
|
40 |
|
41 function n = nper (r, p, a, l, m) |
3426
|
42 |
3191
|
43 if ((nargin < 3) || (nargin > 5)) |
3456
|
44 usage ("nper (r, p, a, l, method)"); |
3191
|
45 endif |
3426
|
46 |
4030
|
47 if (! (isscalar (r) && (r > -1))) |
3458
|
48 error ("nper: r must be a scalar > -1"); |
4030
|
49 elseif (! isscalar (p)) |
3458
|
50 error ("nper: p must be a scalar"); |
4030
|
51 elseif (! isscalar (a)) |
3458
|
52 error ("nper: a must be a scalar"); |
3191
|
53 endif |
|
54 |
|
55 if (nargin == 5) |
3456
|
56 if (! isstr (m)) |
3458
|
57 error ("nper: `method' must be a string"); |
3191
|
58 endif |
|
59 elseif (nargin == 4) |
3456
|
60 if (isstr (l)) |
3191
|
61 m = l; |
|
62 l = 0; |
|
63 else |
|
64 m = "e"; |
|
65 endif |
|
66 else |
|
67 m = "e"; |
|
68 l = 0; |
|
69 endif |
3426
|
70 |
3456
|
71 if (strcmp (m, "b")) |
3191
|
72 p = p * (1 + r); |
|
73 endif |
3426
|
74 |
3191
|
75 q = (p - r * a) / (p - r * l); |
3426
|
76 |
3191
|
77 if (q > 0) |
|
78 n = - log (q) / log (1 + r); |
|
79 else |
|
80 n = Inf; |
|
81 endif |
3426
|
82 |
3191
|
83 endfunction |
|
84 |