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. |
3407
|
31 ## @end deftypefn |
5053
|
32 ## |
3408
|
33 ## @seealso{pv, pmt, nper, and npv} |
3426
|
34 |
5428
|
35 ## Author: KH <Kurt.Hornik@wu-wien.ac.at> |
3458
|
36 ## Description: Rate of return of an investment |
3426
|
37 |
3191
|
38 function r = rate (n, p, v, l, m) |
|
39 |
|
40 if ((nargin < 3) || (nargin > 5)) |
3456
|
41 usage ("rate (n, p, v, l, method)"); |
3191
|
42 endif |
3426
|
43 |
4030
|
44 if (! (isscalar (n) && (n > 0))) |
3458
|
45 error ("rate: n must be a positive scalar"); |
4030
|
46 elseif (! isscalar (p)) |
3458
|
47 error ("rate: p must be a scalar"); |
4030
|
48 elseif (! isscalar (v)) |
3458
|
49 error ("rate: p must be a scalar"); |
3191
|
50 endif |
|
51 |
|
52 if (nargin == 5) |
5443
|
53 if (! ischar (m)) |
3458
|
54 error ("rate: `method' must be a string"); |
3191
|
55 endif |
|
56 elseif (nargin == 4) |
5443
|
57 if (ischar (l)) |
3191
|
58 m = l; |
|
59 l = 0; |
|
60 else |
|
61 m = "e"; |
|
62 endif |
|
63 else |
|
64 l = 0; |
|
65 m = "e"; |
|
66 endif |
3426
|
67 |
4030
|
68 if (! isscalar (l)) |
3458
|
69 error ("rate: l must be a scalar"); |
3191
|
70 endif |
3426
|
71 |
3456
|
72 [r, info] = fsolve (sprintf ("pv (x, %g, %g, %g, \"%s\") - %g", |
|
73 n, p, l, m, v), 0); |
3426
|
74 |
3407
|
75 endfunction |