]> Creatis software - CreaPhase.git/blob - octave_packages/financial-0.4.0/rate.m
Add a useful package (from Source forge) for octave
[CreaPhase.git] / octave_packages / financial-0.4.0 / rate.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} {@var{r} =} rate (@var{n}, @var{p}, @var{v})
18 ## @deftypefnx {Function File} {@var{r} =} rate (@var{n}, @var{p}, @var{v}, @var{l})
19 ## @deftypefnx {Function File} {@var{r} =} rate (@var{n}, @var{p}, @var{v}, @var{l}, @var{method})
20 ## @deftypefnx {Function File} {@var{r} =} rate (@var{n}, @var{p}, @var{v}, @var{method})
21 ## Return the rate of return @var{r} on an investment of present value @var{v}
22 ## which pays @var{p} in @var{n} consecutive periods.
23 ##
24 ## The optional argument @var{l} may be used to specify an additional
25 ## lump-sum payment made at the end of @var{n} periods.
26 ##
27 ## The optional string argument @var{method} may be used to specify
28 ## whether payments are made at the end (@code{"e"}, default) or at the
29 ## beginning (@code{"b"}) of each period.
30 ## @seealso{pv, pmt, nper, npv}
31 ## @end deftypefn
32
33 function r = rate (n, p, v, l = 0, m = "e")
34
35   if (nargin < 3 || nargin > 5)
36     print_usage ();
37   elseif (!isnumeric (n) || !isscalar (n) || n <= 0)
38     error ("number of consecutive periods `n' must be a positive scalar");
39   elseif (!isnumeric (p) || !isscalar (p))
40     error ("second argument `p' must be a numeric scalar");
41   elseif (!isnumeric (v) || !isscalar (v))
42     error ("present value `v' must be a numeric scalar");
43
44   ## the following checks is to allow using default value for `l' while specifying `m'
45   elseif (nargin == 5)
46     if (!isnumeric (l) || !isscalar (l))
47       error ("value of additional lump-sum payment `l' must be numeric scalar");
48     elseif (!ischar (m))
49       error ("`method' must be a string")
50     endif
51   elseif (nargin == 4)
52     if (ischar (l))
53       m = l;
54       l = 0;   # default value for `l' again
55     elseif (!isnumeric (l) || !isscalar (l))
56       error ("fourth argument must either be a numeric scalar for lump-sum payment `l' or a string for `method'");
57     endif
58   endif
59
60   if (!any (strcmpi (l, {"e","b"})))
61     error ("`method' must either be `e' or `b")
62   endif
63
64   f = @(x) pv (x, n, p, l, m) - v;
65   r = fsolve (f, 0);
66
67 endfunction