1 ## Copyright (C) 1995-2000, 2002, 2004-2007 Kurt Hornik <Kurt.Hornik@wu-wien.ac.at>
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
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
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/>.
17 ## @deftypefn {Function File} {} npv (@var{r}, @var{p}, @var{i})
18 ## Net present value of a series of payments.
20 ## Returns the net present value of a series of irregular (i.e., not
21 ## necessarily identical) payments @var{p} which occur at the ends of @var{n}
22 ## consecutive periods. @var{r} specifies the one-period interest rates and
23 ## can either be a scalar (constant rates) or a vector of the same
26 ## The optional argument @var{i} may be used to specify an initial
29 ## Note that the rate @var{r} is specified as a fraction (i.e., 0.05,
34 function v = npv (r, p, i)
36 if (nargin < 2 || nargin > 3)
41 error ("npv: p has to be a vector");
44 p = reshape (p, 1, n);
47 if (any (any (r <= -1)))
48 error ("npv: all interest rates must be > -1");
51 d = 1 ./ (1 + r) .^ (0 : n);
52 elseif (isvector (r) && (length (r) == n))
53 d = [1, (1 ./ cumprod (reshape (1 + r, 1, n)))];
55 error ("npv: r must be a scalar or a vector of the same length as p");
60 error ("npv: I_0 must be a scalar");