]> Creatis software - CreaPhase.git/blob - octave_packages/financial-0.4.0/fv.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / financial-0.4.0 / fv.m
1 ## Copyright (C) 1995-1998, 2000, 2002, 2005-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} {} fv (@var{r}, @var{n}, @var{p}, @var{l}, @var{method})
18 ## Return the future value at the end of period @var{n} of an investment
19 ## which consists of @var{n} payments of @var{p} in each period,
20 ## assuming an interest rate @var{r}.
21 ##
22 ## The optional argument @var{l} may be used to specify an
23 ## additional lump-sum payment.
24 ##
25 ## The optional argument @var{method} may be used to specify whether the
26 ## payments are made at the end (@code{"e"}, default) or at the
27 ## beginning (@code{"b"}) of each period.
28 ##
29 ## Note that the rate @var{r} is specified as a fraction (i.e., 0.05,
30 ## not 5 percent).
31 ## @end deftypefn
32
33 function v = fv (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 ("fv: r must be a scalar > -1");
41   elseif (! (isscalar (n) && n > 0))
42     error ("fv: n must be a positive scalar");
43   elseif (! isscalar (p))
44     error ("fv: p must be a scalar");
45   endif
46
47   if (r != 0)
48     v = p * ((1 + r)^n - 1) / r;
49   else
50     v = p * n;
51   endif
52
53   if (nargin > 3)
54     if (nargin == 5)
55       if (! ischar (m))
56         error ("fv: `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 + fvl (r, n, l);
69     else
70       error ("fv: l must be a scalar");
71     endif
72   endif
73
74 endfunction