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 |
7016
|
7 ## the Free Software Foundation; either version 3 of the License, or (at |
|
8 ## your option) 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 |
7016
|
16 ## along with Octave; see the file COPYING. If not, see |
|
17 ## <http://www.gnu.org/licenses/>. |
3191
|
18 |
3426
|
19 ## -*- texinfo -*- |
3439
|
20 ## @deftypefn {Function File} {} rate (@var{n}, @var{p}, @var{v}, @var{l}, @var{method}) |
|
21 ## Return the rate of return on an investment of present value @var{v} which |
3407
|
22 ## pays @var{p} in @var{n} consecutive periods. |
3191
|
23 ## |
3439
|
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. |
5642
|
30 ## @seealso{pv, pmt, nper, npv} |
3407
|
31 ## @end deftypefn |
3426
|
32 |
5428
|
33 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3458
|
34 ## Description: Rate of return of an investment |
3426
|
35 |
3191
|
36 function r = rate (n, p, v, l, m) |
|
37 |
6497
|
38 if (nargin < 3 || nargin > 5) |
6046
|
39 print_usage (); |
3191
|
40 endif |
3426
|
41 |
6497
|
42 if (! (isscalar (n) && n > 0)) |
3458
|
43 error ("rate: n must be a positive scalar"); |
4030
|
44 elseif (! isscalar (p)) |
3458
|
45 error ("rate: p must be a scalar"); |
4030
|
46 elseif (! isscalar (v)) |
3458
|
47 error ("rate: p must be a scalar"); |
3191
|
48 endif |
|
49 |
|
50 if (nargin == 5) |
5443
|
51 if (! ischar (m)) |
3458
|
52 error ("rate: `method' must be a string"); |
3191
|
53 endif |
|
54 elseif (nargin == 4) |
5443
|
55 if (ischar (l)) |
3191
|
56 m = l; |
|
57 l = 0; |
|
58 else |
|
59 m = "e"; |
|
60 endif |
|
61 else |
|
62 l = 0; |
|
63 m = "e"; |
|
64 endif |
3426
|
65 |
4030
|
66 if (! isscalar (l)) |
3458
|
67 error ("rate: l must be a scalar"); |
3191
|
68 endif |
3426
|
69 |
3456
|
70 [r, info] = fsolve (sprintf ("pv (x, %g, %g, %g, \"%s\") - %g", |
|
71 n, p, l, m, v), 0); |
3426
|
72 |
3407
|
73 endfunction |