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 |
|
17 ## Software Foundation, 59 Temple Place - Suite 330, Boston, MA |
|
18 ## 02111-1307, USA. |
3191
|
19 |
3439
|
20 ## -*- texinfo -*- |
|
21 ## @deftypefn {Function File} {} irr (@var{p}, @var{i}) |
|
22 ## Return the internal rate of return of a series of payments @var{p} |
|
23 ## from an initial investment @var{i} (i.e., the solution of |
|
24 ## @code{npv (r, p) = i}. If the second argument is omitted, a value of |
|
25 ## 0 is used. |
|
26 ## @end deftypefn |
5053
|
27 ## |
3439
|
28 ## @seealso{npv, pv, and rate} |
3426
|
29 |
3458
|
30 ## Author: KH <Kurt.Hornik@ci.tuwien.ac.at> |
|
31 ## Description: Internal rate of return of an investment |
3191
|
32 |
|
33 function r = irr (p, i) |
3426
|
34 |
3191
|
35 if (nargin == 1) |
|
36 i = 0; |
3456
|
37 elseif (! (nargin == 2)) |
|
38 usage ("irr (p, i)"); |
3191
|
39 endif |
3426
|
40 |
4030
|
41 if (! (isvector (p))) |
3458
|
42 error ("irr: p must be a vector"); |
3191
|
43 else |
3456
|
44 p_string = strcat ("[", sprintf ("%.15f, ", p), "]"); |
3191
|
45 endif |
|
46 |
4030
|
47 if (! isscalar (i)) |
3458
|
48 error ("irr: i must be a scalar"); |
3191
|
49 endif |
3426
|
50 |
3456
|
51 r = fsolve (sprintf ("npv (x, %s) - %g", p_string, i), 0.01); |
3426
|
52 |
3191
|
53 endfunction |