3191
|
1 ## Copyright (C) 1995, 1996, 1997 Kurt Hornik |
3426
|
2 ## |
3922
|
3 ## This file is part of Octave. |
|
4 ## |
|
5 ## Octave is free software; you can redistribute it and/or modify it |
|
6 ## under the terms of the GNU General Public License as published by |
3191
|
7 ## the Free Software Foundation; either version 2, or (at your option) |
|
8 ## any later version. |
3426
|
9 ## |
3922
|
10 ## Octave is distributed in the hope that it will be useful, but |
3191
|
11 ## WITHOUT ANY WARRANTY; without even the implied warranty of |
|
12 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
3426
|
13 ## General Public License for more details. |
|
14 ## |
3191
|
15 ## You should have received a copy of the GNU General Public License |
3922
|
16 ## along with Octave; see the file COPYING. If not, write to the Free |
5307
|
17 ## Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
18 ## 02110-1301, USA. |
3191
|
19 |
3426
|
20 ## -*- texinfo -*- |
3439
|
21 ## @deftypefn {Function File} {} rate (@var{n}, @var{p}, @var{v}, @var{l}, @var{method}) |
|
22 ## Return the rate of return on an investment of present value @var{v} which |
3407
|
23 ## pays @var{p} in @var{n} consecutive periods. |
3191
|
24 ## |
3439
|
25 ## The optional argument @var{l} may be used to specify an additional |
|
26 ## lump-sum payment made at the end of @var{n} periods. |
|
27 ## |
|
28 ## The optional string argument @var{method} may be used to specify |
|
29 ## whether payments are made at the end (@code{"e"}, default) or at the |
|
30 ## beginning (@code{"b"}) of each period. |
5642
|
31 ## @seealso{pv, pmt, nper, npv} |
3407
|
32 ## @end deftypefn |
3426
|
33 |
5428
|
34 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3458
|
35 ## Description: Rate of return of an investment |
3426
|
36 |
3191
|
37 function r = rate (n, p, v, l, m) |
|
38 |
|
39 if ((nargin < 3) || (nargin > 5)) |
3456
|
40 usage ("rate (n, p, v, l, method)"); |
3191
|
41 endif |
3426
|
42 |
4030
|
43 if (! (isscalar (n) && (n > 0))) |
3458
|
44 error ("rate: n must be a positive scalar"); |
4030
|
45 elseif (! isscalar (p)) |
3458
|
46 error ("rate: p must be a scalar"); |
4030
|
47 elseif (! isscalar (v)) |
3458
|
48 error ("rate: p must be a scalar"); |
3191
|
49 endif |
|
50 |
|
51 if (nargin == 5) |
5443
|
52 if (! ischar (m)) |
3458
|
53 error ("rate: `method' must be a string"); |
3191
|
54 endif |
|
55 elseif (nargin == 4) |
5443
|
56 if (ischar (l)) |
3191
|
57 m = l; |
|
58 l = 0; |
|
59 else |
|
60 m = "e"; |
|
61 endif |
|
62 else |
|
63 l = 0; |
|
64 m = "e"; |
|
65 endif |
3426
|
66 |
4030
|
67 if (! isscalar (l)) |
3458
|
68 error ("rate: l must be a scalar"); |
3191
|
69 endif |
3426
|
70 |
3456
|
71 [r, info] = fsolve (sprintf ("pv (x, %g, %g, %g, \"%s\") - %g", |
|
72 n, p, l, m, v), 0); |
3426
|
73 |
3407
|
74 endfunction |