]> Creatis software - CreaPhase.git/blob - octave_packages/financial-0.4.0/pmt.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / financial-0.4.0 / pmt.m
1 ## Copyright (C) 1995-1998, 2000-2002, 2004-2007 Kurt Hornik <Kurt.Hornik@wu-wien.ac.at>
2 ##
3 ## This program is free software; you can redistribute it and/or modify it under
4 ## the terms of the GNU General Public License as published by the Free Software
5 ## Foundation; either version 3 of the License, or (at your option) any later
6 ## version.
7 ##
8 ## This program is distributed in the hope that it will be useful, but WITHOUT
9 ## ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 ## FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
11 ## details.
12 ##
13 ## You should have received a copy of the GNU General Public License along with
14 ## this program; if not, see <http://www.gnu.org/licenses/>.
15
16 ## -*- texinfo -*-
17 ## @deftypefn {Function File} {} pmt (@var{r}, @var{n}, @var{a}, @var{l}, @var{method})
18 ## Return the amount of periodic payment necessary to amortize a loan
19 ## of amount a with interest rate @var{r} in @var{n} periods.
20 ##
21 ## The optional argument @var{l} may be used to specify a terminal
22 ## lump-sum payment.
23 ##
24 ## The optional argument @var{method} may be used to specify whether
25 ## payments are made at the end (@var{"e"}, default) or at the beginning
26 ## (@var{"b"}) of each period.
27 ## @seealso{pv, nper, rate}
28 ## @end deftypefn
29
30 function p = pmt (r, n, a, l, m)
31
32   if (nargin < 3 || nargin > 5)
33     print_usage ();
34   endif
35
36   if (! (isscalar (r) && r > -1))
37     error ("pmt: rate must be a scalar > -1");
38   elseif (! (isscalar (n) && n > 0))
39     error ("pmt: n must be a positive scalar");
40   elseif (! (isscalar (a) && a > 0))
41     error ("pmt: a must be a positive scalar");
42   endif
43
44   if (nargin == 5)
45     if (! ischar (m))
46       error ("pmt: `method' must be a string");
47     endif
48   elseif (nargin == 4)
49     if (ischar (l))
50       m = l;
51       l = 0;
52     else
53       m = "e";
54     endif
55   else
56     l = 0;
57     m = "e";
58   endif
59
60   p = r * (a - l * (1 + r)^(-n)) / (1 - (1 + r)^(-n));
61
62   if (strcmp (m, "b"))
63     p = p / (1 + r);
64   endif
65
66 endfunction