]> Creatis software - CreaPhase.git/blob - octave_packages/financial-0.4.0/pv.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / financial-0.4.0 / pv.m
1 ## Copyright (C) 1995-1996, 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} {} pv (@var{r}, @var{n}, @var{p}, @var{l}, @var{method})
18 ## Returns the present value of an investment that will pay off @var{p} for @var{n}
19 ## consecutive periods, assuming an interest @var{r}.
20 ##
21 ## The optional argument @var{l} may be used to specify an additional
22 ## lump-sum payment made at the end of @var{n} periods.
23 ##
24 ## The optional argument @var{method} may be used to specify whether
25 ## payments are made at the end (@code{"e"}, default) or at the
26 ## beginning (@code{"b"}) of each period.
27 ##
28 ## Note that the rate @var{r} is specified as a fraction (i.e., 0.05,
29 ## not 5 percent).
30 ## @seealso{pmt, nper, rate, npv}
31 ## @end deftypefn
32
33 function v = pv (r, n, p, l, m)
34
35   if (nargin < 3 || nargin > 5)
36     print_usage ();
37   endif
38
39   if (! (isscalar (r) && r > -1))
40     error ("pv: r must be a scalar > -1");
41   elseif (! (isscalar (n) && n > 0))
42     error ("pv: n must be a positive scalar");
43   elseif (! isscalar (p))
44     error ("pv: p must be a scalar");
45   endif
46
47   if (r != 0)
48     v = p * (1 - (1 + r)^(-n)) / r;
49   else
50     v = p * n;
51   endif
52
53   if (nargin > 3)
54     if (nargin == 5)
55       if (! ischar (m))
56         error ("pv: `method' must be a string");
57       endif
58     elseif (ischar (l))
59       m = l;
60       l = 0;
61     else
62       m = "e";
63     endif
64     if (strcmp (m, "b"))
65       v = v * (1 + r);
66     endif
67     if (isscalar (l))
68       v = v + pvl (r, n, l);
69     else
70       error ("pv: l must be a scalar");
71     endif
72   endif
73
74 endfunction